Trees and binary trees
Build a binary tree from Node objects, using the words root, leaf, depth and height, and compute its size and height recursively.
It opens on
Start at the top node, 8, and move down along the lines. How many steps does it take to reach the farthest node, 2, 3 or 4?
Why it matters
Search trees, heaps, expression trees and the call trees of recursion are all binary trees, and the cost of nearly every operation on them is measured by one number, the height.
Good to know first
- Singly linked lists: a Node has data and next, and None ends the list
- Recursion and the call stack: a recursive function has a base case and calls itself on something smaller, with one frame for each call
- Big-O, Big-Omega and Big-Theta: Θ(n) means about one step for each item
Step by step
The parts of a tree
Name the root, the parent, children and leaves of a tree, and give the depth and the height of a node and of the tree.
Every other node hangs below exactly one node, its parent, and the nodes just below a node are its children. The root 8 has the children 3 and 10. The node 3 has the children 1 and 6, so 3 is their parent, and 1 and 6, which share a parent, are siblings.
A common mix-up
All the leaves of a tree are at the same depth.
In fact: a leaf is any node with no children, and here the leaf 1 is at depth 2 while 4, 7 and 13 are at depth 3.
Nodes with a left and a right
Build the nine-node tree from Node objects with data, left and right, and reach any node by a chain of left and right.
root.data is 8, and root.left is the node 3, so root.left.data is 3. Then root.left.right is the node 6, and root.left.right.left.data is 4. The run printed 8, 3, 6, 4 for these four.
A common mix-up
Every node must have two children.
In fact: a child that is missing is None, and 10 has only a right child while 1, 4, 7 and 13 have none.
Size and height by recursion
Write and trace the recursive size and height of a binary tree, say that each takes Θ(n) time and Θ(h) stack, and bound the height of a tree of n nodes.
The call size(13) finds both children None, so it returns 1 + 0 + 0 = 1. Then size(14) is 1 + size(13) + size(None) = 1 + 1 + 0 = 2, since 14 has no right child, and size(10) is 1 + 0 + 2 = 3.
A common mix-up
The height of a tree is its number of nodes, or of levels.
In fact: The height counts lines, so a single node has height 0, the empty tree −1, and our tree of 9 nodes and 4 levels has height 3.
Try it
In the same tree, what are the depth and the height of the node 14?Answer
Depth 2 and height 1
Hint: 14 is below 10, which is below 8, and 13 is one line below 14
Which chain of left and right from root reaches the node 14, and what is root.left.left.left?Answer
Root.right.right, and None
Hint: 1 is a leaf, so 1.left is None
What are size and height of the subtree at 10 if 14 had no child at all?Answer
Size 2 and height 1
Hint: 10 would have the single child 14, a leaf
What is the depth of the node 7, and which node is its parent?Answer
Depth 3, and its parent is 6, by the path 8, 3, 6, 7.
What does root.right.right.left.data give?Answer
13, from root to 10 on the right, to 14 on the right, then to 13 on the left.
