Presentation is loading. Please wait.

Presentation is loading. Please wait.

AVL Trees AVL (Adel`son-Vel`skii and Landis) tree = – A BST – With the property: For every node, the heights of the left and right subtrees differ at most.

Similar presentations


Presentation on theme: "AVL Trees AVL (Adel`son-Vel`skii and Landis) tree = – A BST – With the property: For every node, the heights of the left and right subtrees differ at most."— Presentation transcript:

1 AVL Trees AVL (Adel`son-Vel`skii and Landis) tree = – A BST – With the property: For every node, the heights of the left and right subtrees differ at most by one – Each node contains a value (-1, 1, 0) indicating which subtree is "heavier”

2 Balance Factor – Each node is marked with a balance factor indicating which subtree is "heavier” – Balance Factor: height (right subtree) minus height (left subtree) – A balanced tree (-1, 1, 0) 1 1 0 01 0

3

4 AVL Implementation issues: – Insert and Delete are modified. They restructure the tree to make it balanced (if necessary)

5 Fixing Imbalances  An imbalance is detected when the height difference between two subtrees of a node becomes greater than 1 or smaller than -1  There are two cases of imbalances: 2 0 -2 1 0 2 1 0 0 or CASE 1CASE 2 Imbalance caused by inserting node in left subtree of left child or right subtree of right child (i.e., insertion occurs on the “outside”) Imbalance caused by inserting node in right subtree of left child or left subtree of right child (i.e., insertion occurs on the “inside”)

6 Fixing Imbalances (cont’d)  Fixing an imbalance is done by rotating the tree  There are two types of rotation: single rotation  for CASE 1 imbalances double rotation  for CASE 2 imbalances  consists of two single rotations  The rotations must always preserve the BST property

7 Fixing Imbalances: Case 1 6 4 2 6 4 2 node with imbalance A B C D ABCD This is a single right rotation. A single left rotation is symmetric. right rotate node 4 about 6

8 Fixing Imbalances: Case 1 (cont’d) left rotate node Q about P This is a single left rotation

9 Fixing Imbalances: Case 2 6 2 4 6 4 2 node with imbalance A B C D ABCD STEP 1: left rotate node 4 about 2 6 4 2 A B C D STEP2: right rotate node 4 about 6 node with imbalance

10 Fixing Imbalances: Case 2 (cont’d) STEP 1: Right rotate R about Q STEP 2: Left rotate R about P Note that P can be part of a larger AVL tree; it can be a child of some other node in the tree

11 AVL Trees: Insert 1.Insert the node as in a BST 2.Starting at the newly inserted node, travel up the tree (towards the root), updating the balances along the way The first node for which the balance factor becomes +/- 2 (if any) is the root P of a subtree that needs to be rebalanced EXAMPLE: Insert the items 4, 2, 1, 5, 6, 7, 8, 18, 17, 16, 13, 12 into an initially empty AVL tree

12 AVL Trees: Insert, rebalance locally – If a node is entered into the larger AVL tree and P becomes imbalanced, after restoring the balance of P, does extra work need to be done to the predecessor(s) of P? Fortunately not. The new height of P (after the rotation) is exactly the same as the original height of P prior to the insertion that caused P’s imbalance. Thus no further updating of heights on the path to the root is needed, and consequently no further rotations are needed Does it mean that the tree can no grow in the height at all?

13 AVL Trees: Delete 1.Delete the node as in a BST 2.Starting at the parent of the deleted node, travel up the tree (towards the root), updating the balances along the way a.If the tree becomes unbalanced, decide which rotation needs to be performed, rotate the tree and update the balances b.Keep traveling towards the root, checking the balances. You may need to rotate again

14 AVL Trees: Efficiency It can be shown that the worst case height of an AVL tree is at most 44% larger than the minimum possible for a BST (i.e. approximately 1.44lgn)

15 Time Complexity of Basic AVL Tree Operations Insert – Maximum possible number of rotations = 1 Delete – Maximum possible number of rotations = lg(n) Worst case times – Search: O(lgn) – Insert: O(lgn) – Delete: O(lgn)

16 Other Methods AVL trees maintain balance of BSTs while they are being created via insertions of data An alternative approach is to have trees that readjust themselves when data is accessed, making often accessed data items move to the top of the tree (splay trees)


Download ppt "AVL Trees AVL (Adel`son-Vel`skii and Landis) tree = – A BST – With the property: For every node, the heights of the left and right subtrees differ at most."

Similar presentations


Ads by Google