Binary Search Trees 19
Binary Search Trees 19
not full!
• Height of a tree h: #levels = L
(Warning: some books define h
as #levels-1).
2 0
21
2 2
2 3
What is the total #nodes N
of a full tree with height h?
0 1 h 1 h
N 2 2 ... 2 2 1
l=0 l=1 l=h-1
h
2 1 N
h
2 N 1
h log( N 1) O(log N )
Why is h important?
• Tree operations (e.g., insert, delete, retrieve
etc.) are typically expressed in terms of h.
No O(N)
Binary Search Trees (BSTs)
• Binary Search Tree Property:
The value stored at
a node is greater than
the value stored at its
left child and less than
the value stored at its
right child
Binary Search Trees (BSTs)
Where is the
smallest element?
Ans: leftmost element
template<class ItemType>
struct TreeNode<ItemType> {
ItemType info;
TreeNode<ItemType>* left;
TreeNode<ItemType>* right;
};
Binary Search Tree Specification
#include <fstream.h>
struct TreeNode<ItemType>;
template<class ItemType>
class TreeType {
public:
TreeType();
~TreeType();
TreeType(const TreeType<ItemType>&);
void operator=(const TreeType<ItemType>&);
void MakeEmpty();
bool IsEmpty() const;
bool IsFull() const;
int NumberOfNodes() const;
Binary Search Tree Specification
(cont.)
void RetrieveItem(ItemType&, bool& found);
void InsertItem(ItemType);
void DeleteItem(ItemType);
void ResetTree(OrderType);
void GetNextItem(ItemType&, OrderType, bool&);
void PrintTree(ofstream&) const;
private:
TreeNode<ItemType>* root;
};
Function NumberOfNodes
• Recursive implementation
#nodes in a tree =
#nodes in left subtree + #nodes in right subtree + 1
template<class ItemType>
int CountNodes(TreeNode<ItemType>* tree)
{
if (tree == NULL) Running Time?
return 0; O(N)
else
return CountNodes(tree->left) + CountNodes(tree->right) + 1;
}
Function Retrieve Item
Function Retrieve Item
• What is the size of the problem?
Number of nodes in the tree we are examining
• What is the base case(s)?
1) When the key is found
2) The tree is empty (key was not found)
• What is the general case?
Search in the left or right subtrees
Function Retrieve Item (cont.)
template <class ItemType>
void TreeType<ItemType>:: RetrieveItem(ItemType& item, bool& found)
{
Retrieve(root, item, found);
}
template<class ItemType>
void Retrieve(TreeNode<ItemType>* tree, ItemType& item, bool& found)
{
if (tree == NULL) // base case 2
found = false;
else if(item < tree->info) Running Time?
Retrieve(tree->left, item, found);
else if(item > tree->info) O(h)
Retrieve(tree->right, item, found);
else { // base case 1
item = tree->info;
found = true;
}
}
Function
Insert Item
• Use the
binary search
tree property
to insert the
new item at
the correct
place
Function Insert
Item
(cont.)
• Implementing
insertion
resursively
e.g., insert 11
Construct Binary Search tree using the following numbers
45, 56, 32, 14, 27, 18, 12, 50, 66, 49, 61, 7, 10, 76
Function InsertItem (cont.)
• What is the size of the problem?
Number of nodes in the tree we are examining
• What is the base case(s)?
The tree is empty
• What is the general case?
Choose the left or right subtree
Function InsertItem (cont.)
template<class ItemType>
void TreeType<ItemType>::InsertItem(ItemType item)
{
Insert(root, item);
}
template<class ItemType>
void Insert(TreeNode<ItemType>*& tree, ItemType item)
{
if(tree == NULL) { // base case
tree = new TreeNode<ItemType>;
tree->right = NULL;
tree->left = NULL;
tree->info = item;
} Running Time?
else if(item < tree->info)
Insert(tree->left, item);
else
O(h)
Insert(tree->right, item);
}
Function InsertItem (cont.)
Insert 11
Does the order of inserting
elements into a tree matter?
• Yes, certain orders might produce very
unbalanced trees!
Does the
order of
inserting
elements into
a tree matter?
(cont.)
Does the order of inserting
elements into a tree matter?
(cont’d)
• Unbalanced trees are not desirable because
search time increases!
• Advanced tree structures, such as red-black
trees, guarantee balanced trees.
Function DeleteItem
45, 56, 32, 14, 27, 18, 12, 50, 66, 49, 61, 7, 10, 76
Delete 10
Delete 50
Delete 14
Function DeleteItem (cont.)
template<class ItemType>
void TreeType<ItmeType>::DeleteItem(ItemType item)
{
Delete(root, item);
}
template<class ItemType>
void Delete(TreeNode<ItemType>*& tree, ItemType item)
{
if(item < tree->info)
Delete(tree->left, item);
else if(item > tree->info)
Delete(tree->right, item);
else
DeleteNode(tree);
}
Function DeleteItem (cont.)
template <class ItemType>
void DeleteNode(TreeNode<ItemType>*& tree)
{
ItemType item;
TreeNode<ItemType>* tempPtr;
tempPtr = tree;
if(tree->left == NULL) { // right child
tree = tree->right; 0 children or
delete tempPtr;
} 1 child
else if(tree->right == NULL) { // left child
tree = tree->left;
delete tempPtr; 0 children or
} 1 child
else {
GetPredecessor(tree->left, item);
tree->info = item;
Delete(tree->left, item); 2 children
}
}
Function DeleteItem (cont.)
template<class ItemType>
void GetPredecessor(TreeNode<ItemType>* tree, ItemType& item)
{
while(tree->right != NULL)
tree = tree->right;
item = tree->info;
}
Comparing Binary Search Trees to Linear Lists
Big-O Comparison
Binary Array- Linked
Operation
Search Tree based List List
Constructor O(1) O(1) O(1)
Destructor O(N) O(1) O(N)
IsFull O(1) O(1) O(1)
IsEmpty O(1) O(1) O(1)
RetrieveItem O(logN)* O(logN) O(N)
InsertItem O(logN)* O(N) O(N)
DeleteItem O(logN)* O(N) O(N)
*assuming h=O(logN)
Finding the Minimum and
Maximum
In the Binary Search Tree the minimum is the left most element
and the maximum is the right most element
Exercises 37-41 (p. 539)
Exercise 17 (p. 537)
Exercise 18 (p. 537)