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

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

View Full Document Right Arrow Icon
    Tutorial 8: AVL Tree Searching Finding min/max Insertion Deletion
Background image of page 1

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

View Full DocumentRight Arrow Icon
    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
Background image of page 2
    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
Background image of page 3

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

View Full DocumentRight Arrow Icon
    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
Background image of page 4
    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
Background image of page 5

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

View Full DocumentRight Arrow Icon
    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
Background image of page 6
    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.
Background image of page 7

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

View Full DocumentRight Arrow Icon
    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
Background image of page 8
    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
Background image of page 9

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

View Full DocumentRight Arrow Icon
    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 ...
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/22/2008 for the course CS 105 taught by Professor Woo during the Spring '08 term at HKUST.

Page1 / 42

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

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online