Binary Trees
In questa pagina 4
Definition
A binary tree 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 with children (left) and (right), is the left subtree of and the right subtree of .
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 eWarning: proper is not perfect. Proper: all internal nodes have 2 children. Perfect: all leaves are at the same depth.
Interface BinaryTree
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: = number of nodes, = number of leaves ( in the textbook), = number of interior nodes (), = height.
| # | property | meaning |
|---|---|---|
| 1 | each internal node contributes exactly one additional leaf | |
| 2 | at least leaves (a linear tree, one per level), at most (a perfect tree) | |
| 3 | bounds on the interior nodes | |
| 4 | bounds on the total number of nodes | |
| 5 | 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:
Induction on the height .
- Base case : the tree is a single node, , , . True ().
- Inductive step: assume the property for all trees of height and take of height . Then has more than one node, so it has a root and, being proper, exactly 2 subtrees rooted at the children of , both proper with height . Let be the nodes and leaves of . Then (the is the root) and . By the inductive hypothesis on :
Proof of Property 2:
The two inequalities are proved separately.
(a) . Induction on .
- Base case : .
- Inductive step: assume it holds for all trees of height ; take of height with subtrees of heights and leaves . Then
(b) . Left as homework in the slides (hint: induction on ). A proof along the hint: base case , . Step: a tree of height has two subtrees, at least one of which, say , has height exactly , so by hypothesis; the other has (every non-empty tree has a leaf). Then .
Proof of Properties 3, 4, 5
Property 2 relates and ; Property 1 relates the interior nodes and .
Property 3. Substitute in Property 2:
Property 4. Sum Properties 2 and 3 (using ):
Property 5. From Property 4.
- Left side: gives .
- Right side: gives , so , i.e. , i.e. .
Together: .
Consequence for the height
In a proper binary tree the height grows at least logarithmically in , but in the worst case linearly. Without further assumptions (e.g. balance) the best bounds are
Extreme proper binary trees:
| skewed tree | balanced (perfect) tree | |
|---|---|---|
| leaves | ||
| nodes | ||
| height |
skewed (h = 2, n = 5) balanced (h = 2, n = 7)
o o
/ \ / \
o o o o
/ \ / \ / \
o o o o o oRelated: 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: nodes, leaves, interior nodes, height. For a non-empty proper binary tree:
Proofs:
- 1: induction on . Base : , . Step: root plus two proper subtrees; , ; apply the hypothesis to both and add.
- 2a (): induction on ; . 2b (): induction; one subtree has height exactly , so .
- 3: substitute in 2. 4: add 2 and 3. 5: from 4, gives ; gives .
- Hence and . Skewed: , . Perfect: , .
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 (all nodes) and (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 →.