L5 Trees
L5 Trees
Hierarchies
Many ways to represent tree-like information:
A B D E F C G
linked hierarchy
I. A 1. B a. D i. E b. F 2. C a. G
outlines, indentations
A E D G C F B
! is a tree If T1, T2, ..., Tk are trees with roots r1, r2, ..., rk and r is a node ! any Ti, then the structure that consists of the Ti, node r, and edges (r, ri) is also a tree. r3 r2 r4 T3 r1 r2 r r3 r4
r1 T1
T4
T2
Terminology
r is the parent of its children r1, r2, ..., rk. r1, r2, ..., rk are siblings. root = distinguished node, usually drawn at top. Has no parent. If all children of a node are !, the node is a leaf. Otherwise, the node is a internal node. A path in the tree is a sequence of nodes u1, u2, ..., um such that each of the edges (u, ui+1) exists. A node u is an ancestor of v if there is a path from u to v. A node u is a descendant of v if there is a path from v to u.
w
leaves
3 2 0 0 1 0 0
A subtree rooted at u is the tree formed from u and all its descendants. A forest is a (possibly empty) set of trees. The set of subtrees rooted at the children of r form a forest. As weve dened them, trees are not a special case of graphs:
Our trees are oriented (there is a root which implicitly denes directions on the edges). A free tree is a connected graph with no cycles.
one element r " T is designated the root. the remaining nodes are partitioned into k ! 0 disjoint sets T1, T2, ..., Tk, each of which is a tree.
This denition emphasizes the partitioning aspect of trees: As we move down the were dividing the set of elements into more and more parts. Each part has a distinguished element (that can represent it).
Basic Properties
A tree with n nodes has n-1 edges (every node except the root has an edge to its parent).
There is exactly one path from the root to each node. (Suppose there were 2 paths, then some node along the 2 paths would have 2 parents.)
An ordered tree is a tree for which the order of the children of each node is considered important.
r r
r1
r2
r3
r4
"
r4
r3
r1
r2
A binary tree is an ordered tree such that each node has ! 2 children. Call these two children the left and right children.
Single node
Replace each missing child with external node Do you need a special ag to tell which nodes are external? Binary tree Every internal node has exactly 2 children. Every leaf (external node) has exactly 0 children. Each external node corresponds to one ! in the original tree lets us distinguish different instances of !. Extended binary tree
Alternative Proof
Thm. An extended binary tree with n internal nodes has n+1 external nodes.
Proof. Every node has 2 children pointers, for a total of 2n pointers. Every node except the root has a parent, for a total of n - 1 nodes with parents. These n - 1 parented nodes are all children, and each takes up 1 child pointer. (pointers) - (used child pointers) = (unused child pointers) 2n - (n-1) = n + 1 Thus, there are n + 1 null pointers. Every null pointer corresponds to one external node by construction.
full
complete
perfect
1
B
2
E F
3
G Mapping of nodes to integers
4
I J
A B C D E
1 2 3 4 5
F G H
6 7 8
I
9
J
10
K L M
11 12 13 14 15
0
_data _left_child _right_child _data _left_child _right_child
A children
Child next Child next Child 0
How would you implement an ordered general tree using a binary tree?