Balanced Binary Search Trees

advertisement
Balanced Binary Search Trees
Drawback of a binary search tree
There are two basic ways to avoid having this happen:
1.
2.
Average time to build a BST with n keys and average time to find
an element in a BST.
The internal path length of a tree is
AVL trees
An AVL tree is
Balance factors:
-1 indicates
0 indicates
+1 indicates
Rebalancing is done by using rotations.
http://www.cse.hut.fi/en/research/SVG/TRAKLA2/exercises.shtml
web site for algorithm animations
Insertions in an AVL Tree
Insertions:
1.
2.
3.
Example
AVL tree Insertions--Example
Insert 10, 15, 5, 12, 20 into tree
Inserting 7 and then 6 requires a double rotation
Generalizing:
Deletions in AVL trees
Splay Trees
amortized running time
Strategy—every time a node is accessed, it must be moved.
A splay tree is an example of a self-adjusting tree.
Splaying
The starting node for a splay depends on the type of access.
1. find—
2. insert—
3. delete—
Let S be the start node of the splay
(1) If S is the root, STOP
(2) If S has a parent P, but no grandparent,
(3) S has both a parent P and a grandparent G.
Download