Description
Problem Set 7
AVL Tree
- * Each node of a BST can be filled with a height value, which is the height of the subtree rooted at that node. The height of a node is the maximum of the height of its children, plus one. The height of an empty tree is -1. Here’s an example, with the value in parentheses indicating the height of the corresponding node:
P(3)
/Â Â \
M(1)Â V(2)
/Â Â Â Â /Â \
A(0)Â R(1) X(0)
\
S(0)
Complete the following recursive method to fill each node of a BST with its height value.
public class BSTNode<T extends Comparable> {
T data;
BSTNode<T> left, right;Â Â Â Â Â Â Â int height;
…
}
// Recursively fills height values at all nodes of a binary tree   public static <T extends Comparable>    void fillHeights(BSTNode<T> root) {       // COMPLETE THIS METHOD
…
}
- In the AVL tree shown below, the leaf “nodes” are actually subtrees whose heights are marked in parentheses:
—— D ——-
/Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â \
BÂ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â F
/Â Â Â Â \Â Â Â Â Â Â Â Â Â Â Â Â /Â Â Â Â Â Â \
AÂ Â Â Â Â Â Â Â CÂ Â Â Â Â Â Â Â Â EÂ Â Â Â Â Â Â Â G
/Â Â \Â Â Â Â /Â Â \Â Â Â Â Â /Â Â \Â Â Â Â /Â Â \
T1Â Â T2Â Â T3Â Â T4Â Â Â T5Â Â T6Â Â T7Â Â T8
(h-1) (h) (h-1) (h-1) (h+1) (h) (h)Â Â (h)
- Mark the heights of the subtrees at every node in the tree. What is the height of the tree?
- Mark the balance factor of each node.
- Given the following AVL tree:
—J—
/Â Â Â Â Â Â Â \
FÂ Â Â Â Â Â Â Â T
/Â Â \Â Â Â Â /Â Â Â \
CÂ Â Â HÂ Â Â Â NÂ Â Â Â X
/ \Â Â Â Â Â Â Â / \Â Â /
BÂ EÂ Â Â Â Â Â LÂ Â QÂ V
/ \
OÂ Â S
- Determine the height of the subtree rooted at each node in the tree.
- Determine the balance factor of each node in the tree.
- Show the resulting AVL tree after each insertion in the following sequence: (In all AVL trees you show, mark the balance factors next to the nodes.)
Insert Z
- Starting with an empty AVL tree, the following sequence of keys are inserted one at a time: 1, 2, 5, 3, 4
Assume that the tree allows the insertion of duplicate keys.
What is the total units of work performed to get to the final AVL tree, counting only key-to-key comparisons and pointer assignments? Assume each comparison is a unit of work and each pointer assignment is a unit of work. (Do not count pointer assignments used in traversing the tree. Count only assignments used in changing the tree structure.)
- * After an AVL tree insertion, when climbing back up toward the root, a node x is found to be unbalanced. Further, it is determined that x’s balance factor is the same as that of the root, r of its taller subtree (Case 1). Complete the following rotateCase1 method to perform the required rotation to rebalance the tree at node x. You may assume that x is not the root of the tree.
public class AVLTreeNode<T extends Comparable<T>> {Â Â Â Â Â Â Â public T data;
public AVLTreeNode<T> left, right;
public char balanceFactor;Â Â // ‘-‘ or ‘/’ or ‘\’Â Â Â Â Â Â Â public AVLTreeNode<T> parent; Â Â Â Â Â Â Â public int height;
}
public static <T extends Comparable<T>>Â Â Â Â void rotateCase1(AVLTreeNode<T> x) {
// COMPLETE THIS METHOD
}