1 / 16

AVL Trees

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”. Balance Factor.

Télécharger la présentation

AVL Trees

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 1 0 1 0 0

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

  4. -2 2 -2 2 1 -1 -1 1 0 0 0 0 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: or or CASE 1 CASE 2 Imbalance caused by inserting node in right subtree of left child or left subtree of right child (i.e., insertion occurs on the “inside”) Imbalance caused by inserting node in left subtree of left child or right subtree of right child (i.e., insertion occurs on the “outside”)

  5. 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

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

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

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

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

  10. AVL Trees: Insert • Insert the node as in a BST • 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

  11. 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?

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

  13. 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)

  14. 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)

  15. 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)

More Related