Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Data Structures: Binary Trees and Binary Search Trees in C, Slides of Computer Engineering and Programming

An overview of binary trees and binary search trees, their jargon, shape, and some functions in c language. It includes examples of binary tree adt operations, binary tree type, size and height of a binary tree, tree traversal methods (preorder, inorder, postorder), and searching, insertion, and deletion in a binary search tree.

Typology: Slides

2012/2013

Uploaded on 04/25/2013

obesix
obesix 🇺🇸

4.2

(18)

239 documents

1 / 58

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Computer Programming Techniques
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a

Partial preview of the text

Download Data Structures: Binary Trees and Binary Search Trees in C and more Slides Computer Engineering and Programming in PDF only on Docsity!

Computer Programming Techniques

Overview

1. Tree Example

2. Binary Trees

3. Binary Trees in C

4. Tree Traversal

5. Binary Search Trees

6. Searching a BST

7. Insertion into a BST

8. Deletion from a BST

2. Binary Trees

 Each element (node) of the tree may have

0 ,1 or 2 successors.

a

c

d e f g

b

i j k

h

Binary Tree Jargon

 a is the root of the tree

 b, c are the children of a

 left subtree of a: the tree whose root is b

 right subtree of a: the tree whose root is c

 d, e, h, i, j are descendents of b

 every node is a descendent of a (the root)

 h, i, j, k, g are the leaves of the tree

Some Binary Tree ADT Operations

 create an empty tree

 build a new tree from a data value and two subtrees

 deletion

 insertion

 check to see if a tree is empty

 print out all the elements in a tree

3. Binary Trees in C

3 .1. Binary Tree Type

3 .2. Examples

3 .3. Some Functions

3 .4. Size of a Binary Tree

3 .5. Height of a Binary Tree

3 .2. Examples

TREE t;

The empty tree:

t = NULL;

A tree with a single element 2:

t t^ NULL t 2 N N

A more complicated tree:

t 2 4 7

N N N

N N

int value(TREE t) { if (isEmptyTree(t)) { printf(“Tree is empty\n”); return -1; } else return t->data; } TREE mkEmpty(void) /* return an empty tree */ { return NULL; }

TREE mkTree(int x, TREE leftT, TREE rightT) /* make a new tree from a data value x and two existing trees */ { TREE temp; temp = malloc(sizeof(TREENODE)); temp->data = x; temp->leftptr = leftT; temp->rightptr = rightT; return temp; }

3 .4. Size of a Binary Tree

 an empty tree has 0 size

 a non-empty tree has one node plus the number of

elements in the subtrees

n

tree

tree

size(tree) =

1 + size(tree1) + size(tree2)

C Version

int size(TREE t) { if (isEmptyTree(t)) return 0; else return (1 + size(lsTree(t))

  • size(rsTree(t)) ); }

3 .5. Height of a Binary Tree

 the height of an empty tree is 0

 the height of a non-empty tree is 1 plus the height

of the highest subtree

n

tree

tree

height of tree height of tree

height(tree) = 1 +

max(height(tree1),

height(tree2))

C Version

int height(TREE t) { if(isEmptyTree(t)) return 0; else return (1 + max(height(lsTree(t)), height(rsTree(t)))); }