Professional Documents
Culture Documents
Nell Dale
Chapter 8
Binary Search Trees
1
Jake’s Pizza Shop
Owner
Jake
Manager Chef
Brad Carol
2
A Tree Has a Root Node
Owner
ROOT NODE Jake
Manager Chef
Brad Carol
3
Leaf nodes have no children
Owner
Jake
Manager Chef
Brad Carol
LEAF NODES
4
A Tree Has Levels
Owner
LEVEL 0 Jake
Manager Chef
Brad Carol
5
Level One
Owner
Jake
Manager Chef
LEVEL 1 Brad Carol
6
Level Two
Owner
Jake
Manager Chef
Brad Carol
LEVEL 2
Waitress Waiter Cook Helper
Joyce Chris Max Len
7
A Subtree
Owner
Jake
Manager Chef
Brad Carol
8
Another Subtree
Owner
Jake
Manager Chef
Brad Carol
RIGHT SUBTREE
OF ROOT NODE
9
Binary Tree
10
A Binary Tree
Q L
E T A
K S
11
How many leaf nodes?
V
Q L
E T A
K S
12
How many descendants of Q?
V
Q L
E T A
K S
13
How many ancestors of K?
V
Q L
E T A
K S
14
Trees
● Hierarchical structures are called
trees
● Binary trees
■ Each node has no more than
two children
■ The beginning of the tree is a
unique starting node called
the root
■ The node to the left of a node,
if it exists, is called its left child
■ The node to the right of a node,
if it exists, is its right child
■ If a node in the tree has no
children, it is called a leaf node
Binary Search Trees
17
Binary Search Tree
18
Implementing a Binary Tree
with Pointers and Dynamic Data
V
Q L
E T A
K S
19
Each node contains two pointers
#include<stdlib.h>
#include<iostream.h>
#include<conio.h>
typedef int ElementType;
struct NodeType{
int info; // Data member
NodeType *right, *left; // Pointer to right and left children
};
typedef NodeType *TreeType;
21
Shape of a binary search tree . .
.
Depends on its key values and their order of insertion.
Insert the elements ‘J’ ‘E’ ‘F’ ‘T’ ‘A’ in that order.
‘J’
22
Inserting ‘E’ into the BST
‘J’
‘E’
23
Inserting ‘F’ into the BST
‘J’
‘E’
‘F’
24
Inserting ‘T’ into the BST
‘J’
‘E’ ‘T’
‘F’
25
Inserting ‘A’ into the BST
‘J’
‘E’ ‘T’
‘A’ ‘F’
26
What binary search tree . . .
is obtained by inserting
the elements ‘A’ ‘E’ ‘F’ ‘J’ ‘T’ in that order?
‘A’
27
Binary search tree . . .
obtained by inserting
the elements ‘A’ ‘E’ ‘F’ ‘J’ ‘T’ in that order.
‘A’
‘E’
‘F’
‘J’
‘T’
28
Another binary search tree
‘J’
‘E’ ‘T’
‘K’ ‘P’
29
Is ‘F’ in the binary search tree?
‘J’
‘E’ ‘T’
‘S’
30
Deleting ‘B’?
‘J’
‘E’ ‘T’
‘S’
31
Deleting ‘B’:
‘J’
‘E’ ‘T’
‘L’ ‘Q’
‘S’
32
Deleting ‘k’?
‘J’
‘E’ ‘T’
‘L’ ‘Q’
‘S’
33
Deleting ‘k’ :
‘J’
‘E’ ‘T’
‘Q’
‘S’
34
Deleting ‘T’?
‘J’
‘E’ ‘T’
‘Q’
‘S’
35
Deleting ‘T’?
‘J’
‘E’ ‘T’
‘Q’
‘S’
36
Deleting ‘T’ :
‘J’
‘E’ ‘S’
‘Q’
37
In order Traversal: A E H J M T Y
Print second
tree
‘J’
‘E’ ‘T’
‘J’
‘E’ ‘T’
‘J’
‘E’ ‘T’
int EmptyTree(TreeType t)
{
return (t==NULL);
}
int FullTree(TreeType t)
{
return (0);
}
41
// SPECIFICATION (continued)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Insert a node to the tree
void Insert (TreeType *t, ElementType x)
{
if((*t) != NULL)
if((*t) -> info > x)
Insert( &(*t) -> left , x);
else
Insert( &(*t) -> right , x);
else{
*t = new(NodeType);
(*t)->info = x;
(*t)->left = NULL;
(*t)->right = NULL;
}
} 42
NodeType *FindAndRemoveMax(TreeType *t)
{
if((*t)->right==NULL)
{
NodeType *p = *t;
*t = (*t) -> left;
return(p);
} else
return (FindAndRemoveMax(&(*t)->right));
}
43
void DeleteNode(TreeType *t,ElementType x){
if(*t! = NULL)
if ((*t)-> info > x)
DeleteNode (&(*t) -> left , x);
else
if ((*t) -> info < x)
DeleteNode ( &(*t) -> right , x );
else{
NodeType *p = *t;
if ((*t) -> right == NULL)
*t = (*t) -> left;
else
if ((*t) -> left == NULL)
*t=(*t) -> right;
else {
p = FindAndRemoveMax ( &(*t) -> left );
(*t) -> info = p -> info;
}
delete(p);
}
} 44
Destroying the Tree:
45
//Printing the tree in order
void PrintInOrder(TreeType t){
if(t!=NULL){
PrintInOrder(t->left);
cout<<" "<<t->info<<" ";
PrintInOrder(t->right);
}
}
//Printing the tree in preorder
void PrintPreOrder(TreeType t){
if(t!=NULL){
cout<<" "<<t->info<<" ";
PrintPreOrder (t->left);
PrintPreOrder (t->right);
}
}
46
//Printing the tree in postorder
void PrintPostOrder(TreeType t)
{
if(t!=NULL)
{
PrintPostOrder (t->left);
PrintPostOrder (t->right);
cout <<" "<< t -> info << " “ ;
}
}
47
void main() {
TreeType t;
CreateTree(&t);
Insert (&t,11);
Insert (&t,7);
Insert (&t,5);
Insert(&t,9);
Insert(&t,25);
Insert(&t,20);
Insert(&t,15);
Insert(&t,30);
Insert(&t,40);
Insert(&t,35);
Insert(&t,60);
Insert(&t,50);
Insert(&t,45);
Insert(&t,55);
Insert(&t,8);
PrintPreOrder(t);
cout<<"\n";
DeleteNode(&t,11); 48
PrintPreOrder(t); getch(); }