DS1: Binary Trees
San Skulrattanakulchai
September 17, 2018
Binary Tree Definitions
- GT Ch 2.3.3–2.3.4
- A binary tree is either empty or nonempty, but not both.
- A nonempty binary tree B consists of three things:
- a node r called root
- a left binary tree L called left subtree
- a right binary tree R called right subtree
- We write (r, L, R) to denote the nonempty binary tree B with root r, left subtree L, and right subtree R.
- We assume no node is the root of more than one binary tree, and no binary tree is the subtree of itself.
Representing Binary Trees
- Each node in a binary tree B contains
- a key field,
- a left field pointing to the left subtree
- a right field pointing to the right subtree
- This allows traversing down the tree.
- If traversing up the tree is also desired, the node should have another field p pointing to the parent node.
More Definitions
- A node x is a right child of node y if some binary tree B has y as its root, and the right subtree of B has x as its root.
- We define left child, child, and parent in the obvious way.
- A node with no children is a leaf; a nonleaf node is called an internal node.
- Node x is an ancestor of node y if either x = y, or x is the parent of an ancestor of y.
- Node x is a proper ancestor of node y if x is an ancestor of y and x ≠ y.
- We define descendant, and proper descendant in the obvious way.
- A path is a sequence of nodes, each node after the first being the parent or child of the previous node. A path is simple if it contains no repeated node.
More Definitions, continued
- An edge is a pair of nodes (x, y) such that x is the parent of y or vice versa.
- The length of a path is the number of edges it contains.
- Consider a nonempty binary tree B = (r, L, R). Denote the depth of a node x in B by depth(x, B). We have \[
\mbox{depth}(x, B) =
\left\{
\begin{array}{ll}
0, & \mbox{if $x = r$} \\
1 + \mbox{depth}(x, L), & \mbox{if $x$ is in $L$} \\
1 + \mbox{depth}(x, R), & \mbox{if $x$ is in $R$}
\end{array}
\right.
\]
- The height of B equals the largest depth of a node. It also equals the length of a longest simple path from r to a leaf.
- All the nodes of depth \(\ell\) are collectively called level \(\ell\) of B.
- A binary tree is full if all of its internal nodes have two children.
Theorems
- Full Binary Tree Lemma: The number of internal nodes in any nonempty full binary tree is one less than the number of leaves in the same tree.
Proof: By induction or counting the same thing two ways.
- A binary tree is complete if it is full and all its leaves have the same depth.
- A complete binary tree of height h has \(1+2+\cdots+2^i+\cdots+2^h=2^{h+1}-1\) nodes.
- Binary Tree Height Lemma: Any binary tree of n nodes has height \(\ge \log (n+1)-1\).
Proof Sketch: This follows from \(n\le 2^{h+1}-1\).
Other Trees
- A rooted tree T is either empty or consists of a node called the root of T, and zero or more rooted trees called the subtrees of the root.
- An ordered tree is a rooted tree where the subtrees are presented in a specific order.
- All notions on binary trees (e.g., child, ancestor, depth, edge) extend to rooted trees.
- Every ordered tree can be represented as a binary tree by using the left-child right-sibling representation.
- For any integer \(k\ge3\), the \(k\)-ary trees generalize the binary trees. A ternary tree is 3-ary.