1 / 20

CS2420: Lecture 27

CS2420: Lecture 27. Vladimir Kulyukin Computer Science Department Utah State University. Outline. Balanced Binary Trees (AVL Trees) Section 4.4. Review: AVL Tree. An empty binary tree is an AVL tree. If T is a non-empty binary tree, then T is an AVL tree if and only if:

dominy
Télécharger la présentation

CS2420: Lecture 27

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. CS2420: Lecture 27 Vladimir Kulyukin Computer Science Department Utah State University

  2. Outline • Balanced Binary Trees (AVL Trees) • Section 4.4.

  3. Review: AVL Tree • An empty binary tree is an AVL tree. • If T is a non-empty binary tree, then T is an AVL tree if and only if: 1) Both sub-trees are AVL trees. 2) The heights of the sub-trees differ by at most 1 (AVL Tree Property). • The height of an empty tree is defined to be -1, whereas the height of a tree with exactly one node is 0.

  4. Review: Balance Factors • The balance factor (BF) of a node is the difference b/w the height of the node’s left sub-tree (HL) and the height of the node’s right sub-tree (HR). • BF = HL – HR. • By the AVL Tree Property, the balance factor of each node in an AVL tree must be -1, 0, or 1.

  5. Review : Balance Factors HL = height of the left sub-tree. HR = height of the right sub-tree. BF = balance factor = HL – HR. 12 BF = HL – HR = 0 – 0 = 0. 8 16 HL = 0 HR = 0

  6. Review: Balance Factors (BFs) HL = height of the left sub-tree. HR = height of the right sub-tree. BF = balance factor = HL – HR. 12 BF = HL – HR = 0 – (-1) = 1. 8 HL = 0 HR = -1

  7. Review: AVL Tree with BFs 12 BF = 1 8 16 BF = 1 BF = 1 15 10 14 BF = 0 BF = 0 BF = 0 25 6 BF = 0 BF = 0

  8. Example: AVL Tree with BF’s 50 BF = -1 17 BF = 0 67 BF = 1 61 BF = 0

  9. Definition: AVL Search Tree • An AVL Search Tree is a collection that satisfies the following two properties: 1) Binary Search Tree Property: An AVL Search Tree is a binary search tree. 2) AVL Tree Property: For every node in the tree, the height of the node’s left sub-tree and the height of the node’s right sub-tree differ by at most 1.

  10. AVL Search Tree Node • AVL Search Tree node has the same elements (member variables in C++) as the BST node plus the balance factor (integer) element. • If space is a concern, the BF can be implemented as three Boolean (1-bit) variables whose values, taken collectively, will encode -1, 0, and 1. For example, 000 for 0, 001 for 1, and 010 for -1. Thus, the space overhead per AVL node is 3 bits.

  11. AVL Tree Height • Let Nh be the minimum number of nodes in an AVL tree of height h. • Why are we concerned with the minimum and not the maximum? Because we already know that the answer for the maximum – it is the number of nodes in a complete binary search tree. • When h = 0, Nh = 1. • When h = 1, Nh = 2. • Nh = Nh-1 + Nh-2 + 1. Why? Because the minimum number of nodes in an AVL tree is the minimum number of nodes in the left sub-tree plus the minimum number of nodes in the right sub-tree plus 1 for the root.

  12. Recall Fibonacci (Pingala) Numbers

  13. AVL Tree Height • Recall the formula for the Fibonacci numbers: • Fn = Fn-1 + Fn-2, F0 = 0, F1 = 1. • Claim: Nh = Fh+3 – 1. • Proof: By induction on h. • Remember that Nh is the minimum number of nodes in an AVL Search Tree.

  14. Examples: Nh = Fh+3 - 1 h = 0 h = 1 N0 = 1 = F0+3 – 1 = 2 – 1 N1 = 2 = F1+3 – 1 = 3 – 1

  15. Examples: Nh = Fh+3 - 1 h = 2 N2 = 4 = F2+3 – 1

  16. Fibonacci Number Approximation

  17. AVL Tree’s Height

  18. AVL Tree’s Height

  19. AVL Tree’s Height

  20. AVL Search Tree: Find • Since AVL search trees are binary search trees, finding an element in an AVL search tree is the same as finding an element in a binary search tree. • The complexity is O(logN).

More Related