Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
SlidescopiedfromwebsiteofProf.D.W.Harder,Univ.ofWaterloo
Background
AVL trees and red black trees are binary AVLtreesandredblacktreesarebinary searchtreeswithlogarithmicheight
ThisensuresalloperationsareO(ln(n)) However, insertions and deletions are expensive , p
Conclusion:Singlerotationsdonothelp
Convertsalinkedlistintoanotherlinkedlist
Examples
With a little consideration, it becomes g g , obvious that inserting 1 through 10, in that order, will produce the splay tree
Example: Find 1
We begin with a zig-zig rotation zig zig
...and another
...and another
At this point, this requires a single zig p g operation to bring 1 to the root
Example: Find 3
The deepest node is now 3:
This node must be splayed to the root p y beginning with a zig-zag operation
Example: Find 9
Of th th the three deepest nodes, 9 requires a d t d i zig-zig operation, so will access it next
The zig-zig operation will push 6 and its left sub tree sub-tree down
Example: Find 5
Accessing the deepest node, 5, we must g g g p begin with a zig-zag operation
The height of the tree is 4; however, 7 of p the nodes form a perfect tree at the root
Example: Find 7
Accessing 7 will require two zig-zag p operations
Example: Find 2
Finally, accessing 2, we first require a zigg p zag operation
In this case, with 2 at the root 3 10 must case root, 3-10 be in the right sub-tree
The right sub-tree happens to be AVL balanced
Deletion
Removing a node also follows the pattern y of a binary search tree
Find the node to be deleted, which brings it to the root Delete the node We get two root. node. subtrees L and R. Find the minimum of the right sub tree R (or sub-tree the largest of the left sub-tree L) M k L th l ft subtree of R Make the left bt f
Examples
To remove a node, for example, 6, splay it to the root
First we require a zig-zag operation
Examples
At this point, we need a zig operation to move 6 to the root
Examples
Delete the node 6
Make L the left tree of R Thus we have removed 6 and the Thus, resulting tree is, again, reasonably balanced
X:CurrentNode L:TreethatstoresnodelessthanX L: Tree that stores node less than X R:TreethatstoresnodegreaterthanX Descendthetreetwolevelsatatime,we p encounterapairofnodes
Dependingonwhetherthenodesaresmalleror largerthanX,placetheminLorR larger than X place them in L or R
Thecurrentnodeisalwaystherootofthe middletree Whenthesearcheditemisfinallyreached attachLandRtothebottomofthemiddle attach L and R to the bottom of the middle tree PlacenodesinLandRandperform reattachment
Reattachement Re attachement
L,Randthemiddletreesarerearrangedto g getasingletree g
Example
Find19 Perform a simplified zigzag Performasimplifiedzig zag
Performazigzig
Performazig
Wehavereachedanulllink
Element19notfound
Reassemble
Finalsplaytree
Review
Showtheresultofinserting3,1,4,5,2,9,6 and8intoa
Bottomupsplaytree Top down splay tree Topdownsplaytree
Insert3,1,4,5,2,9,6and8
3 1
Zig
1 3
Insert3,1,4,5,2,9,6and8
1 3 1 4 4
Zig zig Zigzig
Insert3,1,4,5,2,9,6and8
4
Zig
5 5 3 1 4
3 1
Insert3,1,4,5,2,9,6and8
5 4 3 1 2
Zig zag Zigzag
5 2 1 3 4
Zig Zi
2 1 4 3 5
Insert3,1,4,5,2,9,6and8
L R L R
3 1
L
3
R
1 3
Insert3,1,4,5,2,9,6and8
L R L R
1 3 4 1
4 4 3 1
Insert3,1,4,5,2,9,6and8
L
4 3 5
4 3 1
5 4 3 1
Insert3,1,4,5,2,9,6and8
L
5 4 3
3 1
L
4 2 5
R
2 3
2 1 5 3 4 5
4 1