T8 - Tutorial 8 AVL Tree Searching Finding min/max...

This preview shows pages 1–11. Sign up to view the full content.

Tutorial 8: AVL Tree Searching Finding min/max Insertion Deletion

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
AVL Tree AVL Tree is a binary search tree that satisfies For each node , the height of the left and right subtrees can differ by at most 1 Recall that the height of a node is defined as the length of the longest path from that node to a leaf node Define the height of an empty tree to be -1 for convenience Usually, the height of every node are stored in the implementation A tree satisfying this property can be proven height = O(log n) , since it is an almost balanced tree Proof can be found in Lecture note Fast operations (search, insert, delete) can be supported
AVL Tree Example Which of the following is an AVL tree? 10 6 1 8 15 18 13 no 10 6 1 8 15 13 18 no (this is not a BST) 13 6 1 8 15 18 10 yes

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
AVL Tree - Operations Operations we will consider in this tutorial Searching Finding minimum/maximum Insertion Deletion Searching and finding min/max are the same as BST But now it is guaranteed to finish in O(log n) time
Insertion in AVL Tree Basically, insertion can be done as in BST Eg. Insert 7 and 14 into the following tree 13 6 1 8 15 18 10 Insert 7 13 6 1 8 15 18 10 7

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Insertion in AVL Tree AVL Property may be violated after insertion We need to restore the AVL property 13 6 1 8 15 18 10 7 Insert 14 13 6 1 8 15 18 10 7 14
Which nodes may violate AVL property? Before talking about how to maintain the AVL property, how can we tell whether the AVL property is violated after insertion ? Checking the height of subtrees in every node Too slow (O(n) time to check all nodes) Note that all non-ancestors nodes remain OK AVL property on these nodes must still be satisfied No need to check them Checking the direct parent of the inserted node? No, the direct parent of the inserted node should not violate AVL property.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Which nodes may violate AVL property? Checking the grandparent of the inserted node? Yes In general, checking all ancestors of the inserted node After insertion, the height of subtrees of ancestors are updated 13 8 15 18 14 21 13 8 15 18 21 Insert 14
Which nodes may violate AVL property? Therefore, we need to check whether the AVL property holds for all ancestors of the inserted node (up to the root) Indeed, you may skip the checking of its parent and the inserted node itself If there is no ancestor violate AVL property We are done Otherwise, we need to restore the AVL property at that node

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Restore AVL property (Case 0) A node is inserted in T1 or T2 AVL property cannot be violated at node x The height of T1 and T2 can be increased by at most 1 after insertion AVL property can only be violated if the height of T1 and T2 differ by 1 originally, and an insertion is occurred at the taller subtree to make it even taller x T1 T2 h h ...
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern