Knowledge Base > Algorithm > Data Structure
Overview
A tree is a nonempty and recursively defined structure, consisted of vertices and
edges. A vertex is a object that contains either a value or a pointer to a subtree or NULL, if
it is a leaf node. An edge is a connection between two vertices, and a path is a list of
distinct vertices in which successive vertices are connected by edges in the
tree.
Each node in the tree can have zero (then is NULL) or more child nodes or descendent nodes. A
node that has a child is called the parent node . If two nodes have the same parent, they are
sibling nodes. The topmost node in a tree is called the root node, and which by definition,
does not have parents. The root node is where the operations on the tree
commonly begin. Nodes at the bottommost level of the tree are called leaf or external
nodes, and they will not have any child nodes. An internal node is any node of
a tree that has child nodes. A successor of a node x is the node with the smallest
key greater than x, and a predecessor is the node with the largest key smaller than x.
Every node in a tree can be seen as the root node of the subtree rooted at that
node.
The number of children of a node x is the tree is called the degree of x. The height of a node is
the length of the longest path to a leaf from that node. The depth of a node is the length
of the path to the root. The height of a tree is equal to the largest depth of any node
in the tree.
The idea of tree structure can help us understand and analyze recursive program
as well as advanced data structure. Usually, operations in tree follows the
divide-and-conquer mechanism (of method 3.7.3.1). There are many well-known
sorting and graph algorithms that are built on tree structure.
In Graph theory, there are many different kinds of tree, where their mathematical
properties may be slightly different.
Basic Operation
Binary Tree
A binary tree is defined to be an ordered tree, in which each internal node must contain two children. Here are the mathematical properties of a binary tree [2]:
Property 3.8.4.1. A binary tree with n internal nodes has n + 1 external (leaf)
nodes.
Proof> (Proof by Induction)
Assume the statement is true. When n = 0 (base case), the tree has 0
internal node, and 1 external node, which is the root. Thus, holds for the
base case.
Next, for any tree with n internal nodes has m internal nodes in left subtree
and n - m - 1 (n - m - root) internal nodes in right subtree. Hence, in
inductive hypothesis n, the tree would have m + 1 external nodes in left
subtree and n-m external nodes in right subtree, and adds up to be n+1
external nodes for the whole tree. Thus, proved.
Property 3.8.4.2. A binary tree with n internal nodes has 2n links: n - 1 links to
internal nodes and n + 1 links to external nodes.
Proof>
For any tree with n internal nodes, there are n - 1 internal nodes that
has parent node (except root node). Thus, the tree has n - 1 links among
internal nodes. From the previous property, a tree with n internal nodes
has n + 1 external nodes, which implies that the tree has n + 1 links from
external nodes to its parents (internal nodes). This adds up to be 2n links
for the whole true. Thus, proved.
Property 3.8.4.3. A complete binary tree of height h has at most 2h+1 - 1 or

nodes.
Proof>(Proof by Induction)
Assume the statement is true. when h = 0 ⇒ 21+0 - 1 = 1, is exactly the
root node. Thus, holds for the base case.
Next, we create a new complete binary tree of height h + 1, called it T3,
by adding a root node and two copies of subtree with height h, called them
T1 and T2. So, the number of nodes in the new complete binary tree is
T3 = T1 + T2 = 1 + 2 ⋅ (2h+1 - 1) = 2h+2 - 1. Thus, proved.

Property 3.8.4.4. A binary tree of height h has at least 2h nodes.
Proof>(Proof by Induction)
Assume the statement is true. When h = 0 ⇒ 20 = 1, is exactly the root
node. Thus, holds for the base case.
We call this incomplete binary tree of height h, T1. Then, we create a
new complete binary tree, T3 of height (h + 1) , by adding a root and a
right subtree to be the copy of T1 to (h - 1) levels, which is a complete
binary tree. We call this T2. Finally, we add the left subtree as T1, and the
number of nodes in T3 = 1+T1 +T2 = 1+2h+(2h-1) = 2⋅2h = 2(h+1).
Thus, proved.

Property 3.8.4.5. For a tree of height h, there are at most 2H nodes at height
level H, where H ≤ h.
Proof>
The number of nodes at a height level can be retrieved by subtracting the
minimum number of node at the height level (from property 3.8.4.3) with
the maximum number of nodes (from property 3.8.4.4). That is, at height
level H,H ≤ h, h is the height of the tree. Number of nodes at height level
H can at most be (2H+1 -1)-(2H)+1 = 2⋅2H-1-2H+1 = 2H. Thus,
proved.
Property 3.8.4.6. There are at most ⌈
⌉ nodes at height level H in an n-element
heap, where H ≤ h, h is the height of tree. Proof>
We can proof this by dividing the tree structure into 3 parts:
We sum up these nodes, which is at most maximum number of nodes. That is,


Property 3.8.4.7. The height of a binary tree with n internal nodes is at least lg n
and at most n + 1.
Proof>
The worst case is a linear tree with only one leaf, and thus has n + 1
nodes, that is h ≤ O(n+1). The best base is a binary tree, with 2k internal
nodes at each level, except for the bottom level (they are all leaf nodes).
However, the completion of nodes at the bottom level can effect the number
of external nodes from 2h-1 (the bottom level has only the left-most pair of
nodes) to 2h (a complete binary tree) external nodes, where h is the height
of the tree. Thus, we can have this inequality for external nodes for a true
with n internal nodes: 2h-1 < n + 1 ≤ 2h ⇒ h- 1 < lg (n + 1) ≤ h. Thus,
lg n ≤ h ≤ n + 1.
Grammar 3.8.4.1. Backus-Naur Form for Binary Tree
| <bintree> | ::= | <null> |
| ::= | < <symbol> <number> <bintree> <bintree> > |
Source Code
Here is the Binary Tree implementation, written in Scheme:
Related Topics