Salta al contenuto
Note per Studenti Binary Trees Report

Binary Trees

In questa pagina 4

Definition

A binary tree TT is an ordered tree in which:

  • each internal node has at most two children;
  • each non-root node is labelled as either the left or the right child of its parent;
  • the left child precedes the right child in the ordering of a node's children.

Unless specified otherwise, a node with a single child has it as the left child.

The arity of a tree is the maximum number of children of an internal node. For a binary tree the arity is 2.

Terminology: for a node vv with children uu (left) and ww (right), TuT_u is the left subtree of vv and TwT_w the right subtree of vv.

      v
     / \
    u   w          u: left child of v,  w: right child of v
   / \   \         T_u: left subtree of v, T_w: right subtree of v
  .   .   .

Proper binary tree

A binary tree is proper (also called full) if every internal node has exactly two children.

  binary tree          proper binary tree
       a                     a
      / \                   / \
     b   c                 b   c
    /     \               / \
   d       e             d   e

Warning: proper is not perfect. Proper: all internal nodes have 2 children. Perfect: all leaves are at the same depth.

Interface BinaryTree

java
public interface BinaryTree<E> extends Tree<E> {
    /** Returns the Position of p's left child (or null if it doesn't exist) */
    Position<E> left(Position<E> p);
    /** Returns the Position of p's right child (or null if it doesn't exist) */
    Position<E> right(Position<E> p);
    /** Returns the Position of p's sibling (or null if no sibling exists) */
    Position<E> sibling(Position<E> p);
}

BinaryTree<E> specialises Tree<E>: it inherits all its methods (see Trees - Definitions and PropertiesA rooted tree is a hierarchy of nodes under a single root; depth is the distance from the root, height the distance from the farthest leaf, and the height of a tree equals the depth of its deepest leaf.Trees - Definitions and Properties →) and adds these, which make sense only because each internal node has at most two children.

Properties of a non-empty proper binary tree

Notation: nn = number of nodes, mm = number of leaves (nEn_E in the textbook), n−mn - m = number of interior nodes (nIn_I), hh = height.

# property meaning
1 m=(n−m)+1m = (n - m) + 1 each internal node contributes exactly one additional leaf
2 h+1≤m≤2hh + 1 \le m \le 2^h at least h+1h + 1 leaves (a linear tree, one per level), at most 2h2^h (a perfect tree)
3 h≤n−m≤2h−1h \le n - m \le 2^h - 1 bounds on the interior nodes
4 2h+1≤n≤2h+1−12h + 1 \le n \le 2^{h+1} - 1 bounds on the total number of nodes
5 log⁡2(n+1)−1≤h≤n−12\log_2(n + 1) - 1 \le h \le \frac{n - 1}{2} the height varies from logarithmic to linear, depending on the balance of the tree

Properties 3, 4, 5 are corollaries of 1 and 2. In the textbook: Property 1 is Proposition 8.8, Properties 2-5 are Proposition 8.7 (which also covers non-proper trees).

Proof of Property 1: m=n−m+1m = n - m + 1

Induction on the height h≥0h \ge 0.

  • Base case h=0h = 0: the tree is a single node, m=1m = 1, n−m=0n - m = 0, n=1n = 1. True (1=0+11 = 0 + 1).
  • Inductive step: assume the property for all trees of height h′≤hh' \le h and take TT of height h+1≥1h + 1 \ge 1. Then TT has more than one node, so it has a root rr and, being proper, exactly 2 subtrees T1,T2T_1, T_2 rooted at the children of rr, both proper with height ≤h\le h. Let ni,min_i, m_i be the nodes and leaves of TiT_i. Then n=n1+n2+1n = n_1 + n_2 + 1 (the +1+1 is the root) and m=m1+m2m = m_1 + m_2. By the inductive hypothesis on T1,T2T_1, T_2: m=m1+m2=(n1−m1+1)+(n2−m2+1)=(n1+n2+1)−(m1+m2)+1=n−m+1. ■m = m_1 + m_2 = (n_1 - m_1 + 1) + (n_2 - m_2 + 1) = (n_1 + n_2 + 1) - (m_1 + m_2) + 1 = n - m + 1. \ \blacksquare

Proof of Property 2: h+1≤m≤2hh + 1 \le m \le 2^h

The two inequalities are proved separately.

(a) m≤2hm \le 2^h. Induction on h≥0h \ge 0.

  • Base case h=0h = 0: m=1≤20=1m = 1 \le 2^0 = 1.
  • Inductive step: assume it holds for all trees of height ≤h\le h; take TT of height h+1h + 1 with subtrees T1,T2T_1, T_2 of heights h1,h2≤hh_1, h_2 \le h and leaves m1,m2m_1, m_2. Then mT=m1+m2≤2h1+2h2≤2h+2h=2⋅2h=2h+1=2hT.m_T = m_1 + m_2 \le 2^{h_1} + 2^{h_2} \le 2^h + 2^h = 2 \cdot 2^h = 2^{h+1} = 2^{h_T}.

(b) m≥h+1m \ge h + 1. Left as homework in the slides (hint: induction on hh). A proof along the hint: base case h=0h = 0, m=1≥1m = 1 \ge 1. Step: a tree TT of height h+1h + 1 has two subtrees, at least one of which, say T1T_1, has height exactly hh, so m1≥h+1m_1 \ge h + 1 by hypothesis; the other has m2≥1m_2 \ge 1 (every non-empty tree has a leaf). Then m=m1+m2≥h+2=(h+1)+1m = m_1 + m_2 \ge h + 2 = (h + 1) + 1. ■\blacksquare

Proof of Properties 3, 4, 5

Property 2 relates mm and hh; Property 1 relates the interior nodes n−mn - m and mm.

Property 3. Substitute m=n−m+1m = n - m + 1 in Property 2: h+1≤n−m+1≤2h ⟹ h≤n−m≤2h−1.h + 1 \le n - m + 1 \le 2^h \ \Longrightarrow\ h \le n - m \le 2^h - 1.

Property 4. Sum Properties 2 and 3 (using m+(n−m)=nm + (n - m) = n): h+1+h≤m+(n−m)≤2h+2h−1 ⟹ 2h+1≤n≤2h+1−1.h + 1 + h \le m + (n - m) \le 2^h + 2^h - 1 \ \Longrightarrow\ 2h + 1 \le n \le 2^{h+1} - 1.

Property 5. From Property 4.

  • Left side: n≥2h+1n \ge 2h + 1 gives h≤n−12h \le \frac{n-1}{2}.
  • Right side: n≤2h+1−1n \le 2^{h+1} - 1 gives 2h+1≥n+12^{h+1} \ge n + 1, so log⁡22h+1≥log⁡2(n+1)\log_2 2^{h+1} \ge \log_2(n + 1), i.e. h+1≥log⁡2(n+1)h + 1 \ge \log_2(n + 1), i.e. h≥log⁡2(n+1)−1h \ge \log_2(n + 1) - 1.

Together: log⁡2(n+1)−1≤h≤n−12\log_2(n + 1) - 1 \le h \le \frac{n-1}{2}. ■\blacksquare

Consequence for the height

In a proper binary tree the height grows at least logarithmically in nn, but in the worst case linearly. Without further assumptions (e.g. balance) the best bounds are h∈Ω(log⁡n),h∈O(n).h \in \Omega(\log n), \qquad h \in O(n).

Extreme proper binary trees:

skewed tree balanced (perfect) tree
leaves m=h+1m = h + 1 m=2hm = 2^h
nodes n=2h+1n = 2h + 1 n=2h+1−1n = 2^{h+1} - 1
height h=(n−1)/2h = (n - 1)/2 h=log⁡2(n+1)−1h = \log_2(n + 1) - 1
 skewed (h = 2, n = 5)      balanced (h = 2, n = 7)
        o                          o
       / \                       /   \
      o   o                     o     o
     / \                       / \   / \
    o   o                     o   o o   o

Related: Binary Tree Traversals and Expression TreesInorder visits the left subtree, the node, then the right subtree; on the parse tree of an arithmetic expression postorder evaluates the expression and inorder rebuilds the fully parenthesized infix form, both in linear time.Binary Tree Traversals and Expression Trees →, Tree TraversalsPreorder visits a node before its subtrees (top-down), postorder after them (bottom-up); both run in time proportional to the number of nodes plus the cost of the visits, and are design patterns for algorithms on trees.Tree Traversals →.

Versione ripasso

Binary tree: ordered tree, each node has at most 2 children, each non-root node is the left or right child of its parent, left precedes right; a single child is the left one by default. Arity = maximum number of children of an internal node (2 here).

Proper (full) binary tree: every internal node has exactly 2 children. Proper is not perfect (perfect: all leaves at the same depth).

BinaryTree<E> extends Tree<E> adds left(p), right(p), sibling(p) (null if absent).

Notation: nn nodes, mm leaves, n−mn - m interior nodes, hh height. For a non-empty proper binary tree:

  1. m=n−m+1m = n - m + 1
  2. h+1≤m≤2hh + 1 \le m \le 2^h
  3. h≤n−m≤2h−1h \le n - m \le 2^h - 1
  4. 2h+1≤n≤2h+1−12h + 1 \le n \le 2^{h+1} - 1
  5. log⁡2(n+1)−1≤h≤n−12\log_2(n + 1) - 1 \le h \le \frac{n - 1}{2}

Proofs:

  • 1: induction on hh. Base h=0h = 0: m=1m = 1, n−m=0n - m = 0. Step: root plus two proper subtrees; n=n1+n2+1n = n_1 + n_2 + 1, m=m1+m2m = m_1 + m_2; apply the hypothesis to both and add.
  • 2a (m≤2hm \le 2^h): induction on hh; m=m1+m2≤2h1+2h2≤2⋅2h=2h+1m = m_1 + m_2 \le 2^{h_1} + 2^{h_2} \le 2 \cdot 2^h = 2^{h+1}. 2b (m≥h+1m \ge h + 1): induction; one subtree has height exactly hh, so m≥(h+1)+1m \ge (h + 1) + 1.
  • 3: substitute m=n−m+1m = n - m + 1 in 2. 4: add 2 and 3. 5: from 4, n≥2h+1n \ge 2h + 1 gives h≤(n−1)/2h \le (n - 1)/2; n+1≤2h+1n + 1 \le 2^{h+1} gives h≥log⁡2(n+1)−1h \ge \log_2(n + 1) - 1.
  • Hence h∈Ω(log⁡n)h \in \Omega(\log n) and h∈O(n)h \in O(n). Skewed: m=h+1m = h + 1, n=2h+1n = 2h + 1. Perfect: m=2hm = 2^h, n=2h+1−1n = 2^{h+1} - 1.

Typical mistakes: confusing proper with perfect; forgetting that properties 1-5 need a proper tree (every internal node with exactly 2 children); mixing up the roles of nn (all nodes) and n−mn - m (interior nodes).

See Binary Tree Traversals and Expression TreesInorder visits the left subtree, the node, then the right subtree; on the parse tree of an arithmetic expression postorder evaluates the expression and inorder rebuilds the fully parenthesized infix form, both in linear time.Binary Tree Traversals and Expression Trees → and Trees - Definitions and PropertiesA rooted tree is a hierarchy of nodes under a single root; depth is the distance from the root, height the distance from the farthest leaf, and the height of a tree equals the depth of its deepest leaf.Trees - Definitions and Properties →.

Teoria collegata