0% found this document useful (0 votes)
39 views23 pages

Binary Search Trees: Anila Yasmeen

A binary search tree (BST) is a binary tree where each node contains a key value that is greater than all key values in its left subtree and less than all key values in its right subtree. BSTs allow for efficient sorting, searching, insertion, and deletion operations. Key values are inserted by comparing them to the root node and placing them in the left or right subtree accordingly. Traversals like inorder, preorder, and postorder allow printing the tree in sorted order or accessing nodes in different orders.

Uploaded by

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

Binary Search Trees: Anila Yasmeen

A binary search tree (BST) is a binary tree where each node contains a key value that is greater than all key values in its left subtree and less than all key values in its right subtree. BSTs allow for efficient sorting, searching, insertion, and deletion operations. Key values are inserted by comparing them to the root node and placing them in the left or right subtree accordingly. Traversals like inorder, preorder, and postorder allow printing the tree in sorted order or accessing nodes in different orders.

Uploaded by

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

Binary Search Trees

Anila Yasmeen
Binary Search Trees
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.
Binary Search Trees

A binary search tree Not a binary search tree


BST Example
Sorting using BST
15, 6, 18, 3, 7, 17, 20, 2, 4, 13, 9

Inorder: 2, 3, 4, 6, 7, 9, 13, 15, 17, 18, 20


Sorting using Binary SearchTrees
• Read the numbers and insert them into a
binary tree using the following rules:
– The first number is placed in the root.
– Each successive number is compared to the root:
– If it is smaller than the root, place it in the left
subtree.
– If it is equal or greater than the root, place it in
the right subtree.
• If this tree is traversed in inorder (left, root,
right) the numbers are printed in ascending
order.
Searching BST
• If we are searching for 15, then we are
• done.
If we are searching for a key < 15, then we
• should search in the left subtree.
If we are searching for a key > 15, then we
should search in the right subtree.
Is ‘F’ in the binary search tree?
‘J’

‘E’ ‘T’

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

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

‘B’ ‘L’ ‘Q’

‘S’
Implementation of
Binary Search Trees
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
TreeType<char> CharBST;

TreeType ‘J’
~TreeType Private data:

IsEmpty
root
‘E’ ‘S’
InsertItem

RetrieveItem ‘A’ ‘H’

PrintTree
.
.
.
// 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& );
void item , bool&
PrintTree found outFile) const ;
(ofstream&
. . .
private:
TreeNode<ItemType>* root ;
};
// 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 ) ;
// 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 ) ;
}
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 ) ;
}
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- , item, found ) ;
>left // GO RIGHT
elseRetrieveHelper(
if ( item > ptr->info )
ptr->right , item, found ) ;
else
{ item = ptr->info ;
found = true ;
}
}
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


// 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

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


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


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

You might also like