Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 213 – Large Scale Programming Lecture 18: Zen & the Art of O (log n ) Search.

Similar presentations


Presentation on theme: "CSC 213 – Large Scale Programming Lecture 18: Zen & the Art of O (log n ) Search."— Presentation transcript:

1 CSC 213 – Large Scale Programming Lecture 18: Zen & the Art of O (log n ) Search

2 Today’s Goal Discuss AVL Trees Relationship to BinaryTree and BST What an AVL Tree looks like and how they work Effects on big-Oh notation AVL Tree Limitations

3 Dictionary ADT Dictionary ADT maps key to 1 or more values Used wherever search is (e.g., everywhere) Implementation using Sequence takes O ( n ) time With good batch of hash, could get O (1) time But could still end up with O ( n ) time If data are random, BST takes O (log n ) time But for ordered data, BST still takes O ( n ) time There must be a better way!

4 Binary Search Trees May not be complete But faster when they are Use different ordering Lower keys in left subtree Higher keys in right subtree Equal keys not specified, but must be consistent 6 9 2 41 10

5 AVL Tree Definition Another type of BST Algorithm guarantees O (log n ) time Does not allow tree to become linked list Keeps tree in balance AVL Tree balanced when each Node’s children differ in height by at most 1 Maintains balance through trinode restructuring Node heights are shown in red 6 92 41 8 1 1 1 5 2 3 2 4

6 Trinode Restructuring Insertion & removal can unbalance tree Trinode restructuring restores tree’s tao Used when node’s children’s height differ Takes node, taller child, & taller grandchild Grandchild must be taller of taller child’s children Move median node to root of subtree Other 2 nodes in restructuring become its children

7 Trinode Restructuring Case 1: Single rotation (e.g., 3 in a row) Move child of 3 to root of subtree Subtrees become grandchildren of node No change in the order of subtrees b a c T0T0 T1T1 T2T2 T3T3 b a c T0T0 T1T1 T2T2 T3T3

8 Trinode Restructuring Case 2: Double rotation Taller child & grandchild go opposite ways Move grandchild up to the root Subtrees become grandchildren of node, but maintain order c b a T0T0 T1T1 T2T2 T3T3 b ca T0T0 T1T1 T2T2 T3T3

9 1 Insertion in an AVL Tree Begins like normal BST insertion Example: insert(5) 7 92 41 8 1 2 1 6 2 3 2 4 5 1

10 Insertion in an AVL Tree Travel up tree checking nodes for balance Must increase tao of unbalanced nodes 7 92 41 8 6 5

11 Insertion in an AVL Tree Must walk up tree starting at inserted Node Stop once we hit the root node Update height for each Node along path Check if Node’s children are balanced Perform rotations when balance needs restoration Not all insertions require rebalancing Will need at most 1 insertion per Node But some insertions need multiple rebalancings

12 1 Removal in an AVL Tree Start with normal BST removal But removal may cause drop in the TAO Example: remove(7) 7 92 51 8 6 4 1 1 1 2 3 2 4 1 8

13 1 Removal in an AVL Tree Start with normal BST removal But removal may cause drop in the TAO Example: remove(7) 7 92 51 6 4 1 1 2 3 4 1 8

14 Removal in an AVL Tree Start with normal BST removal But removal may cause drop in the TAO Example: remove(7) 7 82 41 6 5 9

15 Removal in an AVL Tree Again walk up tree checking for balance Update heights as we go Only examines Nodes along path Height of nodes not on path cannot be changed May need multiple restructuring operations Uses same restructuring as insert Use unbalanced node’s taller child & taller grandchild Does not matter if child & grandchild on path

16 Restructuring for Dummies Store the 7+1 Nodes in local variables Plus one records parent Node of this subtree Set all left, right, & parent from pattern Median node is root; subtrees maintain order c b a T0T0 T1T1 T2T2 T3T3 b ca T0T0 T1T1 T2T2 T3T3 b ca T0T0 T1T1 T2T2 T3T3 b a c T0T0 T1T1 T2T2 T3T3

17 In the Next Lecture Discuss even faster ways of searching No difference in big-Oh notation, however But provide significant speedup in real-life Learn new ways of amortizing costs Discover proper use of the term “splay” When and why we splay a tree What it means to splay Any other excuses I can think of to say splay


Download ppt "CSC 213 – Large Scale Programming Lecture 18: Zen & the Art of O (log n ) Search."

Similar presentations


Ads by Google