BM - Unit 5 - Tree
BM - Unit 5 - Tree
1
Trees
• Trees are an important class of digraphs(called directed trees ).
• Trees are useful in describing any structure which involves hierarchy.
• Familiar examples of such structures are Family trees, the decimal
classification of books in a library, the hierarchy of positions in an
organization, an algebraic expression involving operation for which certain
rules of Precedence are prescribed, etc.
A directed tree : A directed tree is an acyclic diagraph which has one node
called its root with indegree 0, while all other nodes have indegree 1.
• Every directed tree must have at least one node.
• An isolated node is also a directed tree.
Terminal Node : In a directed tree any node which has outdegree 0 is called a
terminal node or a leaf; all other nodes are called branch nodes. The level of
any node is the length of its path from the root.
Fig. (b) shows the same tree drawn upside down
Figure (a ) shows a natural way of representation, viz, v0
the way a tree grows from its root up and ending in
leaves at different Levels. v1 v6
v4 v5 v8 v3
v2 v7 v10
v9
v2
v9
v v7 v10 v4 v5
v8 v0
3
(b)
v1
v6 v1
v0
(a) v3
v9 v7 v2
v10
v5 v4
v8
(c)
Figure- (c) differs from (b) in the order in which the nodes
appear at any level from left to right
• The level of the root of a directed tree is 0, while the level of any node is equal to its
distance from the root.
• All the paths in a directed tree are elementary, and the length of path from any node
to another node, if such a path exists, is the distance between the nodes, because a
directed tree is acyclic.
• An undirected graph G is said be a tree if it is both connected and acyclic.
• Ordered Tree : In a directed tree an ordering of the nodes at each level is prescribed.
Then such tree is called an ordered tree.
• Canonically Labelled Tree : If we label the nodes as 1,2,....or in some other way from
left to right in an ordered tree , then such a tree is said to be canonically labelled.
• Degree of a node : The no. of subtree of a node is called the degree of the node.
The degree of terminal/Leaf node is 0.
• Forest : A set of disjoint trees is called forest. (Subtrees immediately below a node
forms a forest)
Recursive definition of Tree : A tree contains one or more nodes such that one of the
nodes is called the root while all other nodes are partitioned into finite number of trees
called subtrees.
Representation Methods for Directed
tree:
1. Venn diagrams
2. Nesting parentheses (conversion of
nesting parenthesis)
3. Contents of books (Line Diagram)
Venn diagram:- The Tree {v0, v1,…v11}
is defined in terms of subtrees {v1,…v5
} and {v6,…v11 }, each of these subtrees
can be defined in terms of their
subtrees until we get trees with one
node each (terminal node). Venn
diagrams are used to show subtrees.
(as shown in Fig.)
Nesting parentheses : For the above tree the Nesting Parenthesis representation
is as follows
Original Tree
• Every node is reachable from a given node, say u, is called a descendent of u.
• The nodes which are reachable from u through a single edge are called the
sons of u.
Forest : A set of disjoint tree is called forest.
Forests therefore consist only of (possibly disconnected) trees, hence the
name "forest." Examples of forests include the singleton graph, empty graphs,
and all trees.
Subtree : A tree T' whose graph vertices and graph edges form
subsets of the graph vertices and graph edges of a given tree T is
called a subtree of tree T, i.e. A subtree of T is a subgraph of T that
is also a tree.
M-ary Tree : If in a directed tree the outdegree of every node is less than or
equal to m, then the tree is called an m-any tree.
Full/complete M-ary Tree : If the outdegree of every node is exactly to m
or 0, then the tree is called a full or complete M-ary tree.
Binary Tree: For m=2 the trees are called binary tree. i.e. if the outdegree
of every node in a directed tree is less than or equal to 2 then the tree is
called binary tree.
Full Binary Tree: If the outdegree of every node is exactly 2 or 0, then the
tree is called a full/ complete binary tree.
Node Outdegree
V1 2
V2 0 In given tree, for each node,
V3 3 outdegree is less than or equal
V4 0 to 3 (m=3) . Hence it is a 3 –
V5 2 ary tree.
V6 1
V7 0
V8 0
Null is used to indicate an empty tree, that is if TREE[1] = NULL then it indicates that the tree is
empty.
For this method of representation, a tree with depth d will require an array of size 2d+1 elements
45 22 77 11 30 - 90 - 14 25 - - - 87 -
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Here LPTR and RPTR denotes addresses or locations of the left and right
subtrees, respectively, of a particular root node.
Empty subtrees are represented by a pointer value of NULL.
INFO specifies the information associated with a node which can be simple
data item or grouped data item like records, array etc.
A
B D B D
C E G
C E G
C B F E G D A
A A
D C
B B
C E G D E F
F Preorder – ABDECF
Inorder - DBEFACF
Postorder – DEBFCA