Sub topic 1
Trees and Binary Tree Terminology
Prerequisites
This chapter assumes you can read basic Python, including classes and references. You should also be comfortable with recursion, introductory Big-O notation, and the difference between arrays and linked lists. Later chapters build binary search trees on the vocabulary introduced here.
What a tree is
A tree is a data structure made of nodes connected by edges. Each node holds a value and links to other nodes. A tree has no cycles, and every node except one has exactly one parent.
A linked list is a tree where each node has at most one child. A tree generalizes this by letting a node have several children, so the structure branches. This branching is what lets a well-shaped tree reach any of n items in about log n steps instead of n.
Core vocabulary
- Root: the single node with no parent. Every traversal starts here.
- Parent and child: if an edge goes from A down to B, A is the parent and B is the child.
- Siblings: nodes that share the same parent.
- Leaf: a node with no children.
- Internal node: a node with at least one child.
- Subtree: a node together with all of its descendants. Every node is the root of its own subtree.
- Depth of a node: the number of edges from the root down to that node. The root has depth 0.
- Height of a node: the number of edges on the longest path from that node down to a leaf. A leaf has height 0.
- Height of a tree: the height of its root.
Two of these terms are easy to confuse. Depth is measured from the top down to a node. Height is measured from a node down to its deepest leaf.
In this tutorial, an empty tree has height -1. A single node has height 0. This convention keeps the formula 1 + max(left height, right height) correct for every node.
Binary trees
A binary tree is a tree in which every node has at most two children. The children are named the left child and the right child. The two positions are distinct. A node with only a right child is a different shape from a node with only a left child.
Each child is the root of a subtree, so a binary tree is defined recursively: it is either empty, or a root with a left binary tree and a right binary tree. This definition is why most tree code is naturally recursive.
In this tree:
- 50 is the root, at depth 0.
- 30 and 70 are siblings at depth 1.
- 20, 40, 60, and 80 are leaves at depth 2.
- The height of the tree is 2. The height of the node 30 is 1.
Shapes of binary trees
Two shapes matter most for what follows.
- A perfect binary tree has every internal node with two children and all leaves at the same depth. The tree above is perfect.
- A degenerate tree has every internal node with exactly one child. It behaves like a linked list.
A perfect tree of height h holds 2^(h+1) - 1 nodes. Turned around, n nodes fit in a tree of height about log2 n. A degenerate tree with n nodes has height n - 1. Later chapters show that the cost of search, insertion, and deletion follows the height, so this gap between log n and n is the central issue in binary search trees.
Computing size and height in code
A tree is defined recursively, so its measurements are too. The functions below count nodes and compute height using the convention above.
class Node:
def __init__(self, key):
self.key = key
self.left = None
self.right = None
def size(node):
if node is None:
return 0
return 1 + size(node.left) + size(node.right)
def height(node):
if node is None:
return -1
return 1 + max(height(node.left), height(node.right))
root = Node(50)
root.left = Node(30)
root.right = Node(70)
root.left.left = Node(20)
root.left.right = Node(40)
print(size(root)) # 5
print(height(root)) # 2Both functions visit every node once, so each takes O(n) time. The empty tree is the base case. Choosing 0 for its size and -1 for its height makes the recursive formulas work without special cases for leaves.
Exercise
A binary tree has a root, one child of the root, and one child of that child, all on the left side. What are its height, the depth of its only leaf, and its number of nodes? What shape is it called?
Show answer
The height is 2, the depth of the leaf is 2, and it has 3 nodes. Each internal node has exactly one child, so it is a degenerate tree.
Exercise
What is the maximum number of nodes in a binary tree of height 3?
Show answer
A perfect tree of height 3 holds 2^(3+1) - 1 = 15 nodes. No binary tree of height 3 can hold more.