C++ Plus Data Structures: Nell Dale
C++ Plus Data Structures: Nell Dale
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
Implementing a Binary Tree
with Pointers and Dynamic Data
V
Q L
E T A
K S
15
Each node contains two pointers
template< class ItemType >
struct TreeNode {
ItemType info; // Data member
TreeNode<ItemType>* left; // Pointer to left child
TreeNode<ItemType>* right; // Pointer to right child
};
TreeType ‘J’
~TreeType Private data:
root
IsEmpty ‘E’ ‘S’
InsertItem
PrintTree
.
.
.
18
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.
19
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’
20
Inserting ‘E’ into the BST
‘J’
‘E’
21
Inserting ‘F’ into the BST
‘J’
‘E’
‘F’
22
Inserting ‘T’ into the BST
‘J’
‘E’ ‘T’
‘F’
23
Inserting ‘A’ into the BST
‘J’
‘E’ ‘T’
‘A’ ‘F’
24
What binary search tree . . .
is obtained by inserting
the elements ‘A’ ‘E’ ‘F’ ‘J’ ‘T’ in that order?
‘A’
25
Binary search tree . . .
obtained by inserting
the elements ‘A’ ‘E’ ‘F’ ‘J’ ‘T’ in that order.
‘A’
‘E’
‘F’
‘J’
‘T’
26
Another binary search tree
‘J’
‘E’ ‘T’
‘K’ ‘P’
27
Is ‘F’ in the binary search tree?
‘J’
‘E’ ‘T’
‘S’
28
// BINARY SEARCH TREE SPECIFICATION
template< class ItemType >
class TreeType {
public:
TreeType ( ) ; // constructor
~TreeType ( ) ; // destructor
bool IsEmpty ( ) const ;
bool IsFull ( ) const ;
int NumberOfNodes ( ) const ;
void InsertItem ( ItemType item ) ;
void DeleteItem (ItemType item ) ;
void RetrieveItem ( ItemType& item , bool& found ) ;
void PrintTree (ofstream& outFile) const ;
. . .
private:
TreeNode<ItemType>* root ;
}; 29
// SPECIFICATION (continued)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// RECURSIVE PARTNERS OF MEMBER FUNCTIONS
30
// BINARY SEARCH TREE IMPLEMENTATION
// OF MEMBER FUNCTIONS AND THEIR HELPER FUNCTIONS
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
template< class ItemType >
TreeType<ItemType> :: TreeType ( ) // constructor
{
root = NULL ;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
template< class ItemType >
bool TreeType<ItemType> :: IsEmpty( ) const
{
return ( root == NULL ) ;
}
31
template< class ItemType >
void TreeType<ItemType> :: RetrieveItem ( ItemType& item,
bool& found )
{
RetrieveHelper ( root, item, found ) ;
}
‘J’
‘E’ ‘T’
}
} 35
Preorder Traversal: J E A H T M Y
Print first
tree
‘J’
‘E’ ‘T’
‘J’
‘E’ ‘T’
38