Professional Documents
Culture Documents
dddobach
Trees
Generic Tree
A Tree
UNIX directory
Organizational Chart
Trees: Mathematical Definition
Definition:
A tree T is a finite, non-empty set of nodes ,
= ∪ ∪ … ,
with the following properties:
1. A designated node of the set, r, is called the root of the
tree; and
2. The remaining nodes are partitioned into
≥ 0
subsets, , , … , each of which is a tree
Trees: Mathematical Definition
Trees: Example
A minimal tree
Comprised of a single node
A
Trees: Example
When there is more than one node, the remaining
nodes are partitioned into subtrees.
= {, {}}
Trees: Example
When there is more than one node, the remaining
nodes are partitioned into subtrees.
= {, , , {, , , , , , }}
Trees: Terminology
Given a tree = , , … ,
≥ 0
The degree of a node is the number of subtrees
associated with that node. For example, the degree of
tree T is n
A node of degree zero has no subtrees. Such a node is
called a leaf .
Each root of subtree of tree T is called a child of r.
The term grandchild is defined in a similar manner.
Trees: Terminology
Given a tree = , , … ,
≥ 0
A Depth = 0
B D Depth = 1
C E H Depth = 2
F G I Depth = 3
A sample tree
Depth-first traversal
Preorder Traversal
1. Visit the root first; then
2. Do a preorder traversal each of the subtrees of the
root one-by-one in the order given.
C E H
F G I
Pre order: 8, , , , , , , ,
Depth-first traversal
Postorder Traversal
Visit the root last
1. Do a postorder traversal each of the subtrees of the root
one-by-one in the order given;
2. visit the root
In case the tree is a binary tree
1. Traverse the left subtree;
2. traverse the right subtree;
3. visit the root.
Depth-first traversal
Post order traversal?
A
B D
C E H
F G I
Postorder: , , , , , , , , 8
Depth-first traversal
Inorder Traversal
visits the root in between visiting the left and right
subtrees
C E H
F G I
Inorder: , , 8, , , , , ,
Breadth-First Traversal
a non-recursive traversal.
visits the nodes in the order of their depth in the
tree
At each depth the nodes are visited from left to
right
Breadth-First Traversal
A
B D
C E H
F G I
Breadth-first: 8, , , , , , , ,
Expression Trees
Leaves are operands (variables/constants)
Other nodes are operators