6.01.binary Search Trees
6.01.binary Search Trees
6.01.binary Search Trees
ece.uwaterloo.ca
dwharder@alumni.uwaterloo.ca
Outline
Queries that may be made about data stored in a Sorted List ADT
include:
– Finding the smallest and largest values
– Finding the kth largest value
– Find the next larger and previous smaller objects of a given object which
may or may not be in the container
– Iterate through those objects that fall on an interval [a, b]
Binary search trees
5
6.1.1 Implementation
Recall that with a binary tree, we can dictate an order on the two
children
We will assume that in any binary tree, we are not storing duplicate
values unless otherwise stated
– In reality, it is seldom the case where duplicate values in a container
must be stored as separate entities
#include "Binary_node.h"
public:
Binary_search_node( Type const & );
void clear();
bool insert( Type const & );
bool erase( Type const &, Binary_search_node *& );
– If an empty node is reached, e.g., 36, the object is not in the tree:
Binary search trees
24
6.1.4.3 Find
Blackboard example:
– In the given order, insert these objects into an initially empty binary
search tree:
31 45 36 14 52 42 6 21 73 47 26 37 33 8
– What values could be placed:
• To the left of 21?
• To the right of 26?
• To the left of 47?
– How would we determine if 40 is in this binary search tree?
– Which values could be inserted to increase the height of the tree?
Binary search trees
38
6.1.4.5 Erase
If a node has only one child, we can simply promote the sub-tree
associated with the child
– Consider removing 8 which has one left child
Binary search trees
44
6.1.4.5 Erase
Note that after seven removals, the remaining tree is still correctly
sorted
Binary search trees
59
6.1.4.5 Erase
return true;
} else if ( obj < value() ) {
return left()->erase( obj, left_tree );
} else {
return right()->erase( obj, right_tree );
}
}
Binary search trees
63
6.1.4.5 Erase
Blackboard example:
– In the binary search tree generated previously:
• Erase 47
• Erase 21
• Erase 45
• Erase 31
• Erase 36
Binary search trees
64
6.1.5 Binary Search Tree
void clear();
bool insert( Type const &obj );
bool erase( Type const &obj );
};
Binary search trees
66
6.1.5 Constructor, Destructor, and Clear
Another operation on sorted lists may be finding the kth largest object
– Recall that k goes from 0 to n – 1
– If the left-sub-tree has ℓ = k values, return the current node,
– If the left sub-tree has ℓ < k values, return the kth value of the left sub-tree,
– Otherwise, the left sub-tree has ℓ > k values, so return the (k – ℓ – 1)th value
of the right sub-tree
18
7 10
1 5
0 1 2 3 4 5 6 7 8 9 10 11 12 13 141516 17
Binary search trees
77
6.1.6.2 Finding the kth Object
We will look at
– AVL trees
– B+ trees
both of which ensure that the height remains Q(ln(n))
Summary
Usage Notes
• These slides are made publicly available on the web for anyone to
use
• If you choose to use them, or a part thereof, for a course at another
institution, I ask only three things:
– that you inform me that you are using the slides,
– that you acknowledge my work, and
– that you alert me of any mistakes which I made or changes which you
make, and allow me the option of incorporating such changes (with an
acknowledgment) in my set of slides
Sincerely,
Douglas Wilhelm Harder,
MMath
dwharder@alumni.uwaterloo.ca