You are on page 1of 48

C++ Plus Data Structures

Nell Dale
Chapter 8
Binary Search Trees

1
Jake’s Pizza Shop

Owner
Jake

Manager Chef
Brad Carol

Waitress Waiter Cook Helper


Joyce Chris Max Len

2
A Tree Has a Root Node

Owner
ROOT NODE Jake

Manager Chef
Brad Carol

Waitress Waiter Cook Helper


Joyce Chris Max Len

3
Leaf nodes have no children

Owner
Jake

Manager Chef
Brad Carol

Waitress Waiter Cook Helper


Joyce Chris Max Len

LEAF NODES

4
A Tree Has Levels

Owner
LEVEL 0 Jake

Manager Chef
Brad Carol

Waitress Waiter Cook Helper


Joyce Chris Max Len

5
Level One

Owner
Jake

Manager Chef
LEVEL 1 Brad Carol

Waitress Waiter Cook Helper


Joyce Chris Max Len

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

Waitress Waiter Cook Helper


Joyce Chris Max Len

LEFT SUBTREE OF ROOT NODE

8
Another Subtree

Owner
Jake

Manager Chef
Brad Carol

Waitress Waiter Cook Helper


Joyce Chris Max Len

RIGHT SUBTREE
OF ROOT NODE
9
Binary Tree

A binary tree is a structure in which:

Each node can have at most two children,


and in which a unique path exists from
the root to every other node.

The two children of a node are called the


left child and the right child, if they exist.

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

● A binary search tree has the shape


property of a binary tree
● In addition, a binary search tree has a
semantic property among the values in
the nodes in the tree:
■ Thevalue in any node is greater than the
value in any node in its left subtree and less
than the value in any node in its right
subtree
16
Binary Search Tree

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;

NULL ElementType 6000

. left . info . right


20
A Binary Search Tree (BST) is . . .
A special kind of binary tree in which:
1. Each node contains a distinct data value,
2. The key values in the tree can be compared using
“greater than” and “less than”, and
3. The key value of each node in the tree is
less than every key value in its right subtree, and
greater than every key value in its left subtree.

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.

The first value to be inserted is put into the root node.

‘J’

22
Inserting ‘E’ into the BST

There after, each value to be inserted begins by


comparing itself to the value in the root node,
moving left it is less, or moving right if it is greater.
This continues at each level until it can be inserted
as a new leaf.

‘J’

‘E’

23
Inserting ‘F’ into the BST

Begin by comparing ‘F’ to the value in the root node,


moving left it is less, or moving right if it is greater.
This continues until it can be inserted as a leaf.

‘J’

‘E’
‘F’

24
Inserting ‘T’ into the BST

Begin by comparing ‘T’ to the value in the root node,


moving left it is less, or moving right if it is greater.
This continues until it can be inserted as a leaf.

‘J’

‘E’ ‘T’

‘F’

25
Inserting ‘A’ into the BST

Begin by comparing ‘A’ to the value in the root node,


moving left it is less, or moving right if it is greater.
This continues until it can be inserted as a leaf.

‘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’

‘A’ ‘H’ ‘M’

‘K’ ‘P’

Add nodes containing these values in this order:

‘D’ ‘B’ ‘L’ ‘Q’ ‘S’ ‘V’ ‘Z’

29
Is ‘F’ in the binary search tree?
‘J’

‘E’ ‘T’

‘A’ ‘H’ ‘M’ ‘V’

‘D’ ‘K’ ‘P’ ‘Z’

‘B’ ‘L’ ‘Q’

‘S’
30
Deleting ‘B’?
‘J’

‘E’ ‘T’

‘A’ ‘H’ ‘M’ ‘V’

‘D’ ‘K’ ‘P’ ‘Z’

‘B’ ‘L’ ‘Q’

‘S’
31
Deleting ‘B’:
‘J’

‘E’ ‘T’

‘A’ ‘H’ ‘M’ ‘V’

‘D’ ‘K’ ‘P’ ‘Z’

‘L’ ‘Q’

‘S’
32
Deleting ‘k’?
‘J’

‘E’ ‘T’

‘A’ ‘H’ ‘M’ ‘V’

‘D’ ‘K’ ‘P’ ‘Z’

‘L’ ‘Q’

‘S’
33
Deleting ‘k’ :
‘J’

‘E’ ‘T’

‘A’ ‘H’ ‘M’ ‘V’

‘D’ ‘L’ ‘P’ ‘Z’

‘Q’

‘S’
34
Deleting ‘T’?
‘J’

‘E’ ‘T’

‘A’ ‘H’ ‘M’ ‘V’

‘D’ ‘L’ ‘P’ ‘Z’

‘Q’

‘S’
35
Deleting ‘T’?
‘J’

‘E’ ‘T’

‘A’ ‘H’ ‘M’ ‘V’

‘D’ ‘L’ ‘P’ ‘Z’

‘Q’

‘S’
36
Deleting ‘T’ :
‘J’

‘E’ ‘S’

‘A’ ‘H’ ‘M’ ‘V’

‘D’ ‘L’ ‘P’ ‘Z’

‘Q’

37
In order Traversal: A E H J M T Y
Print second
tree

‘J’

‘E’ ‘T’

‘A’ ‘H’ ‘M’ ‘Y’

Print left subtree first Print right subtree last


38
Preorder Traversal: J E A H T M Y
Print first
tree

‘J’

‘E’ ‘T’

‘A’ ‘H’ ‘M’ ‘Y’

Print left subtree second Print right subtree last


39
Postorder Traversal: A H E M Y T J
Print last
tree

‘J’

‘E’ ‘T’

‘A’ ‘H’ ‘M’ ‘Y’

Print left subtree first Print right subtree second


40
// BINARY SEARCH TREE SPECIFICATION

void CreateTree(TreeType *t)


{
*t=NULL;
}

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:

void DestroyTree(TreeType *t)


{
if ((*t)->left != NULL)
DestroyTree (&(*t) -> left);
if ((*t)->right != NULL)
DestroyTree (&(*t) -> right);
delete(*t);
*t=NULL;
}

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(); }

You might also like