CSC 103 Lecture Notes Week 3
Trees, Part 1
a
b
c
d
e
f
g
Here each vertical level of the tree is shown by indenting to the right two
spaces, and (obviously) the edges are missing.
Figure 1: Tree terminology.
Figure 2: Example n-ary tree.
class BinaryTreeNode {
Object value; // the value field
BinaryTreeNode left; // pointer to the left subtree
BinaryTreeNode right; // pointer to the right subtree
}
class NaryTreeNode {
Object value; // the value field
NaryTreeNode children; // pointer to list of children
NaryTreeNode next; // pointer to next sibling
}
Figure 3: Concrete view of an n-ary tree..
For any subtree, including the full tree itself, all nodes to the left have a value less than the root and all nodes to the right have a value greater than the root.
Figure 4: Left-heavy and right-heavy binary search trees..
For any subtree, including the full tree itself, the height of the left and right subtrees can differ by at most 1.
Figure 5: AVL and non-AVL trees.