CSC 2500 Computer Organization

advertisement
CSC 2300
Data Structures & Algorithms
February 16, 2007
Chapter 4. Trees
Today – AVL and Splay Trees




AVL Trees
Double Rotation
Splay Trees
Animation:
http://webpages.ull.es/users/jriera/Docencia/AVL/AVL%20tree%20applet.htm
AVL Trees – Insert


Let α denote the node that must be rebalanced.
Since any node has at most two children and a
height imbalance requires that α’s two subtrees’
heights differ by two, we see that a violation may
occur in four cases:
1.
2.
3.
4.

An insertion into the left subtree of the left child of α.
An insertion into the right subtree of the left child of α.
An insertion into the left subtree of the right child of α.
An insertion into the right subtree of the right child of α.
Cases 1 and 4 are mirror image symmetries with
respect to α, as are cases 2 and 3.
Case 2

Single rotation fails to fix Case 2.
Case 2

Left-right double rotation fixes Case 2.
Case 3

Right-left double rotation fixes Case 3.
Example in Class

We will insert this sequence into an initially
empty AVL tree: 1, 2, …, 6, 7, 16, 15, …, 9, 8.
AVL Tree – Node Declaration
AVL Node – Height Computation
AVL Tree – Insertion
Single Rotation
Double Rotation
Splay Trees
Advantages of Splaying



It gives a good theoretical bound – any M consecutive
tree operations starting from an empty tree take at
most O(M logN) time.
It works well in practice – in many applications, when
a node is accessed, it is likely to be accessed again in
the near future. (What computer component uses this
idea?)
Splay trees do not require the maintenance of height
or balance information, thus saving space and
simplifying the code.
Splaying Operations


The following sequence of operations is repeated until the node containing
the accessed value (after find or insert) is at the root.
Let t point to the node, p point to its parent, and g point to its grandparent.
Zig-zag and Zig-zig
Example

Insert 1, 2, 3, …, N into an initially empty tree.

Why does tree look like this? That is, why is the root not 1?
No Splaying






Let us start accessing nodes.
Access node 1.
How much time does it take?
Assume no splaying.
Access node 2.
How much time does it take?
Splaying




Access node 1.
Splay at the node 1.
Access node 2.
How much time will it take?
Bigger Example

Splay at node 1.

How much time will be required to access node 2?
Bigger Example

Splay at node 2.

How much time will be required to access node 3?
Bigger Example

Splay at node 3.

How much time will be required to access node 4?
Bigger Example

Splay first at node 4, and then at node 5.
Bigger Example

Splay first at node 6, and then at node 7.
Bigger Example

Splay first at node 8, and then at node 9.
Observations





When access paths are long, thus leading to a longer-thannormal search time, the rotations tend to be good for future
operations.
When accesses are cheap, the rotations are not as good and
can be bad.
The extreme case is the initial tree formed by the insertions.
All the insertions are constant-time operations leading to a
bad initial tree.
At that point in time, we have a very bad tree, but we have a
good running time.
Then a couple of horrible accesses leave a nearly balanced
tree. The cost is to give back some of the time that has been
saved.
Download