Week 05 Tutorial Notes
Balanced Trees, Graph Basics
Group Question
- Do rotations to bring the node with value 7 to the root of the tree. Draw the tree after each rotation.
- \(|L - R|\) is the difference in height between the left and right substrees. A tree is considered height balanced if \(|L - R| \leq 1 \) holds for every node. Which of the following trees are unbalanced?
- Investigate the following trees (A) and (B):
- Is the straight-line case height balanced? What rotation/s will fix it?
- Is the zig-zag case height balanced? What rotation/s will fix it? Draw the steps showing how to do this.
-
Your task is to draw out the solution for this problem:
- Is the AVL tree height balanced?
- Draw the AVL tree and Insert 5.
- Unwinding the recursion from inserting node 5, what is the first node that makes the AVL tree height unbalanced?
- From the unbalanced node in (c) is it a Straight-line case or Zigzag case?
- Draw the AVL tree that will fix the height resulting from the rotations done in (d).
- Is the final AVL tree height balanced?
-
Your task is to draw the solution for this problem:
struct node *rotateLeft(struct node *parent);
Redraw the diagram to answer the following questions:
- Label the "child" pointer for a left rotation on your diagram.
- Draw a second diagram that shows the resulting tree after the left rotation.
- Write the pseudocode implementation for
rotateLeft(parent). Such that diagram 1 is your input and diagram 2 is the output.Restriction: Do not create additional pointers. Only use the parent and child pointers.
- Unbalanced. A right rotation at the root will fix the height.
- Unbalanced. A left rotation at the left child first and then right rotation will fix the height.
- The AVL is height balanced. AVL trees are always height balanced by construction.
-
- The first unbalanced node is 3.
- This is a zig-zag case. See the red arrows in the above picture.
- See above picture.
- The AVL is still height balanced after we do the rotations.
Group Work Answers:
Click each section to reveal.
The unbalanced trees are [B, D, F]
struct node *rotateLeft(struct node *parent) {
struct node *child = parent->right;
parent->right = child->left;
child->left = parent;
return child;
}