Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHAPTER 10 SEARCH TREES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND.

Similar presentations


Presentation on theme: "CHAPTER 10 SEARCH TREES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND."— Presentation transcript:

1 CHAPTER 10 SEARCH TREES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY 2004) AND SLIDES FROM NANCY M. AMATO 6 9 2 4 1 8   

2 A BRIEF NOTE ON ALGORITHM COMPLEXITY (ASYMPTOTIC COMPLEXITY)

3 DETERMINING ALGORITHM COMPLEXITY Count ALL operations Again….count ALL operations MAYDAY….count ALL operations Jokes aside, this is the easiest way. An operation will be expressed as a function of the input size (algorithm complexity)

4 BINARY SEARCH TREES An inorder traversal of a binary search trees visits the keys in increasing order 6 92 418

5 SEARCH 6 9 2 4 1 8   

6 INSERTION 6 9 2 4 18    w 6 92 418 5 w

7 EXERCISE BINARY SEARCH TREES Insert into an initially empty binary search tree items with the following keys (in this order). Draw the resulting binary search tree 30, 40, 24, 58, 48, 26, 11, 13

8 DELETION 6 9 2 5 18 6 9 2 4 18 5 v w  

9 DELETION (CONT.) 5 1 8 6 9 v 2 3 1 8 6 9 5 v w z 2

10 EXERCISE BINARY SEARCH TREES Insert into an initially empty binary search tree items with the following keys (in this order). Draw the resulting binary search tree 30, 40, 24, 58, 48, 26, 11, 13 Now, remove the item with key 30. Draw the resulting tree Now remove the item with key 48. Draw the resulting tree.

11 PERFORMANCE

12 AVL TREES 6 3 8 4 v z

13 AVL TREE DEFINITION An example of an AVL tree where the heights are shown next to the nodes:

14 HEIGHT OF AN AVL TREE 3 4 n(1) n(2)

15 INSERTION IN AN AVL TREE Insertion is as in a binary search tree Always done by expanding an external node. Example insert 54: Before Insertion 44 1778 325088 4862 After Insertion 44 17 78 32 50 88 48 62 54

16 TRINODE RESTRUCTURING b=y a=z c=x T0T0 T1T1 T2T2 T3T3 b=y a=z c=x T0T0 T1T1 T2T2 T3T3 c=y b=x a=z T0T0 T1T1 T2T2 T3T3 b=x c=ya=z T0T0 T1T1 T2T2 T3T3 case 1: single rotation (a left rotation about a) case 2: double rotation (a right rotation about c, then a left rotation about a) (other two cases are symmetrical)

17 INSERTION EXAMPLE, CONTINUED unbalanced...

18 RESTRUCTURING SINGLE ROTATIONS

19 RESTRUCTURING DOUBLE ROTATIONS

20 EXERCISE AVL TREES Insert into an initially empty AVL tree items with the following keys (in this order). Draw the resulting AVL tree 30, 40, 24, 58, 48, 26, 11, 13

21 REMOVAL IN AN AVL TREE 44 17 783250 88 48 62 54 44 17 7850 88 48 62 54 before deletion of 32 after deletion remove 32

22 REBALANCING AFTER A REMOVAL 44 17 7850 88 48 62 54 w c=x b=y a=z 44 17 78 5088 48 62 54

23 REBALANCING AFTER A REMOVAL 44 17 7850 88 48 62 54 w c=x b=y a=z 44 17 78 5088 48 62 54

24 EXERCISE AVL TREES Insert into an initially empty AVL tree items with the following keys (in this order). Draw the resulting AVL tree 30, 40, 24, 58, 48, 26, 11, 13 Now, remove the item with key 48. Draw the resulting tree Now, remove the item with key 58. Draw the resulting tree

25 RUNNING TIMES FOR AVL TREES

26 OTHER TYPES OF SELF-BALANCING TREES 9 10 14 2 5 7 9 15 4 62 12 7 21


Download ppt "CHAPTER 10 SEARCH TREES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND."

Similar presentations


Ads by Google