 # CS2420: Lecture 27 Vladimir Kulyukin Computer Science Department Utah State University.

## Presentation on theme: "CS2420: Lecture 27 Vladimir Kulyukin Computer Science Department Utah State University."— Presentation transcript:

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 12 168 HL = 0HR = 0 HL = height of the left sub-tree. HR = height of the right sub-tree. BF = balance factor = HL – HR. BF = HL – HR = 0 – 0 = 0.

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

Review: AVL Tree with BFs 12 8 16 141510 256 BF = 0 BF = 1

Example: AVL Tree with BF’s 50 17 61 67 BF = 0 BF = -1 BF = 1 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 N h 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, N h = 1. When h = 1, N h = 2. N h = N h-1 + N h-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.

Recall Fibonacci (Pingala) Numbers n0123456 FnFn 0112358

AVL Tree Height Recall the formula for the Fibonacci numbers: –F n = F n-1 + F n-2, F 0 = 0, F 1 = 1. Claim: N h = F h+3 – 1. –Proof: By induction on h. –Remember that N h is the minimum number of nodes in an AVL Search Tree.

Examples: N h = F h+3 - 1 h = 0 N 0 = 1 = F 0+3 – 1 = 2 – 1 h = 1 N 1 = 2 = F 1+3 – 1 = 3 – 1

Examples: N h = F h+3 - 1 h = 2 N 2 = 4 = F 2+3 – 1

Fibonacci Number Approximation

AVL Tree’s Height

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