Binary Search Trees: Anila Yasmeen
Binary Search Trees: Anila Yasmeen
Anila Yasmeen
Binary Search Trees
A Binary Search Tree (BST) is . . .
A special kind of binary tree in which:
‘E’ ‘T’
‘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
};
TreeType ‘J’
~TreeType Private data:
IsEmpty
root
‘E’ ‘S’
InsertItem
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 >
bool TreeType<ItemType> :: IsEmpty( ) const
{
return ( root == NULL ) ;
}
template< class ItemType >
void TreeType<ItemType> :: InsertItem ( ItemType item )
{
InsertHelper ( root, item ) ;
}
‘J’
‘E’ ‘T’
}
}
Preorder Traversal: J E A H T M Y
Print first
tree
‘J’
‘E’ ‘T’
Print last
tree
‘J’
‘E’ ‘T’