0% found this document useful (0 votes)
128 views38 pages

C++ Plus Data Structures: Nell Dale

This document contains slides describing binary search trees. It begins with an example of a tree representing an organizational chart. It then discusses key properties of binary trees including that each node can have at most two children, there is a unique path from the root to each node, leaf nodes have no children, and levels in the tree. It provides examples of subtrees, ancestors, and descendants. It describes implementing a binary search tree using pointers and dynamic data structures. It defines the properties of a binary search tree that each node's key value is less than its right child and greater than its left child. Examples are given of inserting values into an empty tree to form the shape and structure of the binary search tree.

Uploaded by

ahmed ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
128 views38 pages

C++ Plus Data Structures: Nell Dale

This document contains slides describing binary search trees. It begins with an example of a tree representing an organizational chart. It then discusses key properties of binary trees including that each node can have at most two children, there is a unique path from the root to each node, leaf nodes have no children, and levels in the tree. It provides examples of subtrees, ancestors, and descendants. It describes implementing a binary search tree using pointers and dynamic data structures. It defines the properties of a binary search tree that each node's key value is less than its right child and greater than its left child. Examples are given of inserting values into an empty tree to form the shape and structure of the binary search tree.

Uploaded by

ahmed ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

C++ Plus Data Structures

Nell Dale
Chapter 8
Binary Search Trees

Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus

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

NULL ‘A’ 6000


. left . info . right
16
// 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;
}; 17
TreeType<char> CharBST;

TreeType ‘J’
~TreeType Private data:
root
IsEmpty ‘E’ ‘S’
InsertItem

RetrieveItem ‘A’ ‘H’

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.

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

‘J’

20
Inserting ‘E’ into the BST

Thereafter, 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’

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

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

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

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’

‘A’ ‘H’ ‘M’

‘K’ ‘P’

Add nodes containing these values in this order:

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

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

‘E’ ‘T’

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

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

‘B’ ‘L’ ‘Q’

‘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

template< class ItemType >


void PrintHelper ( TreeNode<ItemType>* ptr,
ofstream& outFile ) ;

template< class ItemType >


void InsertHelper ( TreeNode<ItemType>*& ptr,
ItemType item ) ;

template< class ItemType >


void RetrieveHelper ( TreeNode<ItemType>* ptr,
ItemType& item, bool& found ) ;

template< class ItemType >


void DestroyHelper ( TreeNode<ItemType>* ptr ) ;

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

template< class ItemType >


void RetrieveHelper ( TreeNode<ItemType>* ptr, ItemType& item,
bool& found)
{ if ( ptr == NULL )
found = false ;
else if ( item < ptr->info ) // GO LEFT
RetrieveHelper( ptr->left , item, found ) ;
else if ( item > ptr->info ) // GO RIGHT
RetrieveHelper( ptr->right , item, found ) ;
else
{ item = ptr->info ;
found = true ;
}
32
}
template< class ItemType >
void TreeType<ItemType> :: InsertItem ( ItemType item )
{
InsertHelper ( root, item ) ;
}

template< class ItemType >


void InsertHelper ( TreeNode<ItemType>*& ptr, ItemType item )
{ if ( ptr == NULL )
{ // INSERT item HERE AS LEAF
ptr = new TreeNode<ItemType> ;
ptr->right = NULL ;
ptr->left = NULL ;
ptr->info = item ;
}
else if ( item < ptr->info ) // GO LEFT
InsertHelper( ptr->left , item ) ;
else if ( item > ptr->info ) // GO RIGHT
InsertHelper( ptr->right , item ) ;
} 33
Inorder 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


34
// INORDER TRAVERSAL
template< class ItemType >
void TreeType<ItemType> :: PrintTree ( ofstream& outFile ) const
{
PrintHelper ( root, outFile ) ;
}

template< class ItemType >


void PrintHelper ( TreeNode<ItemType>* ptr, ofstream& outFile )
{ if ( ptr != NULL )
{
PrintHelper( ptr->left , outFile ) ; // Print left subtree

outFile << ptr->info ;

PrintHelper( ptr->right, outFile ) ; // Print right subtree

}
} 35
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


36
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


37
template< class ItemType >
TreeType<ItemType> :: ~TreeType ( ) // DESTRUCTOR
{
DestroyHelper ( root ) ;
}

template< class ItemType >


void DestroyHelper ( TreeNode<ItemType>* ptr )
// Post: All nodes of the tree pointed to by ptr are deallocated.
{ if ( ptr != NULL )
{
DestroyHelper ( ptr->left ) ;
DestroyHelper ( ptr->right ) ;
delete ptr ;
}
}

38

You might also like