Week 05 Tutorial Questions
AVL Trees, 2-3-4 Trees, Graph Basics
AVL Trees
AVL trees use the following node definition:
typedef struct Node *Link; // Links are pointers to nodes
typedef struct Node *Tree; // a Tree is a pointer to its root node
struct Node { int data; int height; Link left; Link right; };
You may find these resources on AVL trees useful:
- AVL tree visualization
- VisuAlgo's AVL tree visualizer (click on AVL tree)
-
(AVL trees)
For the following AVL tree, annotate each node with the height of its subtree.
Now starting from the initial tree each time, insert the following integers:
0 2 10 17
-
(AVL trees)
Show how an AVL tree would be constructed if the following values were inserted into an initially empty tree in the order given:
12 10 8 6 4 2
-
(Algorithms and complexity)
Using an AVL tree, design an algorithm to determine if an array contains two elements that sum to a given value.
- Write the algorithm in pseudocode.
- Analyse the time complexity of your algorithm.
2-3-4 Trees
2-3-4 trees use the following node definition:
typedef struct Node *Link; // Links are pointers to nodes
typedef struct Node *Tree; // a Tree is a pointer to its root node
struct Node { int order; int data[3]; Link child[4]; };
You may find these resources on 2-3-4 trees useful:
- 2-3-4 tree visualization (select Max. Degree = 4)
Note: This visualizer uses a different strategy from the lecture slides for promoting a value from a full node. The lecture slides promotes the middle of the three values that were already in the node, whereas the visualizer inserts the new value first, and then promotes the smaller of the two middle values.
-
(2-3-4 trees)
Show how a 2-3-4 tree would be constructed if the following values were inserted into an initially empty tree in the order given:
1 2 3 4 5 8 6 7 9 10
Once you have built the tree, count the number of comparisons needed to search for each of the following values in the tree:
1 7 9 13
Graph Representation
-
For each of the following graphs:
Show the concrete data structures if the graph was implemented via:
- adjacency matrix representation (assume a full \( V \times V \) matrix)
- adjacency list representation (if non-directional, include both \( (v, w) \) and \( (w, v) \))
-
Facebook could be considered as a giant "social graph"
- What are the vertices?
- What are the edges?
- Are edges directional?
- What does the degree of each vertex represent?
- What kind of graph algorithm could suggest potential friends?