200 likes | 377 Views
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:
E N D
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: 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.
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.
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
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
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
Example: AVL Tree with BF’s 50 BF = -1 17 BF = 0 67 BF = 1 61 BF = 0
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.
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.
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.
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.
Examples: Nh = Fh+3 - 1 h = 0 h = 1 N0 = 1 = F0+3 – 1 = 2 – 1 N1 = 2 = F1+3 – 1 = 3 – 1
Examples: Nh = Fh+3 - 1 h = 2 N2 = 4 = F2+3 – 1
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).