CSE�326:�Data�Structures Lecture�#10 Amazingly�Vexing�Letters Bart Niswonger Summer�Quarter�2001 Today’s�Outline • AVL�Trees – Deletion – buildTree – Thinking�about�AVL�trees • Splay�Trees 1
Deletion� (Really�Easy�Case) 3 10 Delete(17) 2 2 5 15 1 0 0 1 2 9 12 20 0 0 0 3 17 30 Deletion� (Pretty�Easy�Case) 3 10 Delete(15) 2 2 5 15 1 0 0 1 2 9 12 20 0 0 0 3 17 30 2
Deletion� (Pretty�Easy�Case� cont .) 3 10 Delete(15) 2 2 5 17 1 0 0 1 2 9 12 20 0 0 3 30 Deletion� (Hard�Case�#1) 3 Delete(12) 10 2 2 5 17 1 0 0 1 2 9 12 20 0 0 3 30 3
Single�Rotation�on�Deletion 3 3 10 10 2 2 2 1 5 17 5 20 1 1 0 0 1 0 0 2 9 20 2 9 17 30 0 0 0 3 30 3 What�is�different about� deletion�than�insertion? Deletion� (Hard�Case) 4 Delete(9) 10 2 3 5 17 1 0 2 2 2 9 12 12 20 20 0 0 1 0 1 3 30 30 11 15 15 18 0 0 0 0 0 33 33 13 13 4
Double�Rotation�on�Deletion 4 4 10 10 2 3 1 3 5 17 3 17 1 0 2 2 0 2 2 2 2 12 20 2 5 12 20 0 0 1 0 1 0 1 0 1 3 30 30 11 15 18 11 15 18 0 0 0 0 0 33 33 13 13 �✂✁☎✄✝✆✟✞✡✠☎✞✡☛✌☞☎✍☎✎☎✏ Deletion�with�Propagation 4 10 What�rotation�do�we�apply? 1 3 3 17 0 0 2 2 2 5 12 20 0 1 0 1 11 15 18 30 0 0 13 33 5
Propagated�Single�Rotation 4 4 10 17 3 2 1 3 3 17 10 20 1 2 0 1 0 0 2 2 12 18 2 5 20 3 12 30 0 1 0 1 0 0 0 1 0 11 15 18 30 2 5 11 15 33 0 0 0 33 13 13 Propagated�Double�Rotation 4 4 10 12 2 3 1 3 3 17 10 17 1 0 1 2 0 0 2 2 12 15 20 2 5 20 3 11 0 1 0 1 0 1 0 0 0 11 15 18 30 2 5 13 18 30 0 0 0 33 33 13 6
✂ � ✁ � AVL�Deletion�Algorithm Recursive Iterative 1.If�at�node,�delete� 1.�Search�downward�for it node,�stacking 2.Otherwise�recurse� parent�nodes to�find�it 2.�Delete�node 3.�Correct�heights 3.�Unwind�stack, a.�If�imbalance�#1, correcting�heights single�rotate a.�If�imbalance�#1, b.�If�imbalance�#2� single�rotate� (or�don’t�care), b.�If�imbalance�#2� double�rotate (or�don’t�care) double�rotate Fun�with�AVL�Trees To�Insert�a�sequence�of�n�keys�(unordered) 19��3��4��18��7 into�initially�empty�AVL�tree�takes n n i n O n n l og log ( lo g ) i i 1 1 If�we�then�print�using�inorder�traversal�taking O(n) what�do�we�get? 7
What�can�we�improve? Printing�every�node�is�O(n),�nothing�to�do What�about�building�a�tree? – Can�we�do�it�in�less�than�O(n�log�n) • What�if�the�input�is�sorted? 3��4��7��18��19 If�it�is�sorted,�why�bother? We’ll�see�in�a�moment! AVL�buildTree 5 8 10 15 17 20 30 35 40 Divide�&�Conquer 17 – Divide�the�problem�into�parts – Solve�each�part�recursively – Merge�the�parts�into�a� general�solution 5 8 1015 20303540 How�long�does� divide�&�conquer�take? 8
BuildTree�Example 5 8 10 15 17 20 30 35 40 3 17 5 8 10 15 20 30 35 40 2 2 10 35 20 30 5 8 1 0 0 1 30 40 8 15 0 0 5 20 BuildTree�Analysis� (Approximate) T(1)�=�1 T(n)�=�2T(n/2)�+�1 9
✒ ✟ ✗ ✘ ✔ ✎ ✎ ✟ ✌ ✟ ✎ ✔ ✏ ✑ ✡ ✠ ✟ ✏ ✒ ✓ ✒ ✔✕ � ✝ � ✔ ✏ ✖ ✒ ✂ ✁ � � BuildTree�Analysis� (Exact) Precise�Analysis:� T(0)��=�b n n 1 ✂✞✂✆☎ 1 T(n)��=�T(���)�+�T(���)�+�c ✂✄✆☎ 2 2 By�induction�on� n:� T(n)��=�(b+c)n�+�b Base�case :� T(0)��=�b��=�(b+c)0�+�b Induction�step : n T(n)��=�(b+c)����+�b�+� 1 n n ✡☛✆☞ 1 1 n 2 1 n 2 2 (b+c)����+�b�+�c ✡✍✡✆☞ 1 2 =�(b+c)n�+�b ✎ (n) QED :�T(n)�=�(b+c)n�+�b�=� Application:�Batch�Deletion • Suppose�we�are�using�lazy deletion • When�there�are�lots of�deleted�nodes� (n/2),�need�to�flush them�all�out • Batch�deletion: – Print�non-deleted�nodes�into�an�array How? – Divide�&�conquer�AVL�Treebuild – Total�time: Why�we�cared! 10
Thinking�About�AVL • Observations +�Worst�case�height�of�an�AVL�tree�is�about�1.44�log�n +�Insert,�Find,�Delete�in�worst�case�O(log�n) +�Only�one�(single�or�double)�rotation�needed�on� insertion - O(log�n)�rotations�needed�on�deletion +�Compatible�with�lazy�deletion - Height�fields�must�be�maintained�(or�2-bit�balance) Alternatives�to�AVL�Trees • Change�the�balance�criteria: – Weight�balanced�trees • keep�about�the�same�number�of�nodes�in�each�subtree • not�nearly�as�nice • Change�the�maintenance�procedure: – Splay�trees • “blind”�adjusting�version�of�AVL�trees – no�height�information�maintained!� • insert/find�always�rotates�node� to�the�root ! • worst�case�time�is�O(n) • amortized�time�for�all�operations�is�O(log�n) • mysterious,�but�often�faster�than�AVL�trees�in�practice� (better�low-order�terms) 11
Splay�Trees • “blind”�rebalancing – no�height�or�balance�information�stored • amortized�time�for�all�operations�is�O(log�n) • worst�case�time�is�O(n) • insert/find�always�rotates�node� to�the�root ! – Good�locality • most�common�keys�move�high�in�tree Idea 10 You’re�forced�to�make� 17 a�really�deep�access: Since�you’re�down�there�anyway, fix�up�a�lot�of�deep�nodes! 5 2 9 3 12
Splay�Operations:�Find • Find(x) 1. do�a�normal�BST�search�to�find�n�such� that n->key�=�x 2. move�n�to�root�by�series�of�zig-zag and� zig-zig rotations,�followed�by�a�final�zig if� necessary Zig-Zag * Helped g Unchanged n Hurt p X g p n W X Y Z W Y Z * This�is�just�a�double�rotation 13
Zig-Zig n g p p W Z n g X Y Y Z W X Zig root root n p n p Z X X Y Y Z 14
� Why�Splaying�Helps • Node�n�and�its�children�are�always�helped�(raised) • Except�for�final�zig,�nodes�that�are�hurt�by�a�zig- zag�or�zig-zig�are�later�helped�by�a�rotation�higher� up�the�tree! • Result:� – shallow�(zig)�nodes�may�increase�depth�by�one�or�two – helped�nodes�may�decrease�depth�by�a�large�amount • If�a�node� n on�the�access�path�is�at�depth� d before� the�splay,�it’s�at�about�depth� d/2 after�the�splay – Exceptions�are�the�root,�the�child�of�the�root,�and�the� node�splayed Locality • Assume� m n access�in�a�tree�of�size� n – Total�amortized�time�O( m log� n ) – O(log� n )�per�access�on�average • Gets�better�when�you�only�access� k distinct�items�in�the�m�accesses. – Exercise. 15
Splaying�Example 1 1 2 2 zig-zig 3 3 Find(6) 4 6 5 5 6 4 Still�Splaying�6 1 1 2 6 zig-zig 3 3 6 2 5 5 4 4 16
Almost�There,�Stay�on�Target 1 6 6 1 zig 3 3 2 5 2 5 4 4 Splay�Again 6 6 1 1 zig-zag 3 4 Find(4) 2 5 3 5 4 2 17
Example�Splayed�Out 6 4 1 6 1 zig-zag 3 5 4 2 3 5 2 Splay�Tree�Summary • All�operations�are�in�amortized�O(log�n)�time • Splaying�can�be�done�top-down;�better� because: – only�one�pass – no�recursion�or�parent�pointers�necessary • Invented�by�Sleator�and�Tarjan�(1985),�now� widely�used�in�place�of�AVL�trees • Splay�trees�are� very effective�search�trees – relatively�simple – no�extra�fields�required – excellent� locality properties:�frequently�accessed� keys�are�cheap�to�find 18
To�Do • Study�for�midterm! • Read�through�section�4.7�in�the�book • Comments�&�Feedback • Homework�IV�(studying) • Project�II�– part�B Coming�Up • Midterm�next�Wednesday • A� Huge Search�Tree�Data�Structure� (not�on�the�midterm) 19
Recommend
More recommend