0% found this document useful (0 votes)
61 views29 pages

BM - Unit 5 - Tree

Uploaded by

Meet Bhatia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views29 pages

BM - Unit 5 - Tree

Uploaded by

Meet Bhatia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Trees

Trees: Introduction, Definition, Root, Branch Nodes, Leaf (Terminal Node);


Different Representations of Trees; Forests, Subtrees;
M-ary Tree, Full or Complete M-ary Tree; Binary Tree, Full (Complete) Binary
Tree;
Conversion of M-ary Tree to Binary Tree;
Linked Allocation Technique to Represent Binary Tree in Computer.
Traversal of Binary Tree: Pre-order, In-order, and Post-order Traversal;
Theorems and Exercises (without Proof)

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

Assumption for drawing Line diagram


• Roots are on the top
Contents of books (Line Diagram) • Edges are pointing downward
• All the nodes at any particular levels shown on a
horizontal line

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

In given tree, for each node,


outdegree is 0 or equal to 3 .
Hence it is a complete 3- ary
tree.
Positional Binary Tree :
• The position tree is a tree in which the children of a node are labelled with distinct
positive integers.
• In a positional binary tree every node is uniquely represented by a string over {0,1}
• The root is represented by empty string, any son of node u has a string which is prefix by
string of u.
• The string of terminal node is not prefixed to the string of any other node.
Converting M-ary tree to a binary tree:-
1) Delete all the branches
originating in every node except
the leftmost branch.
2) Choose its left & right sons. The
left son is the node which is
immediately below the given
node, and right son is the node on
the same horizontal line
3) Such a binary tree will not have
right subtree.
A general tree can be viewed as restricted graph but for many reasons, it is
convenient to define and manipulate binary trees instead of general trees. Each
general tree can be represented by an equivalent binary trees.
A binary tree T is defined as a finite set of elements, called nodes, such that
(a) T is empty (called the null tree or empty tree)
(b) T contains a distinguished node R , called the root of T, and remaining nodes
of T form an ordered pair of disjoint binary trees T1 and T2.
These definition of Binary Tree T is recursive since T is defined in terms of the
binary subtree T1 & T2 which are left & right subtree respectively of the root R of
tree T e.g
• Directed tree : - A directed tree is an acyclic diagraph which has one node called it
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).
• Leaf : - In a directed tree, any node which has outdegree 0 is called a terminal
node or a leaf. E.g in figure nodes V5, V6 , V10 – leaf.
• Branch : - A path ending in a leaf is called a branch. All other nodes except
terminal nodes or leaf are called branch nodes.
• Root : - The node with indegree 0 , in directed tree is called a root. E.g in above
fig V0 is a root.
• Level : - The level of the root of a directed tree 0 , while the level of any node is
equal to its distance from the root. (as shown in fig. V0 -> level 0) , (V1,V2 –> level
1 ) , (V2,V3,V4,V8,V9 -> level 2 ) (V5, V6, V10 -> level 3).
• Root – like node :- A node which has more arcs leaving than entering is called
root-like node.
• Leaf- like node :- Anode which has more arcs entering than leaving is called leaf-
like node.
Representation of binary tree:-
There are two ways for representing binary
tree in memory.
• Sequential Representation
• Linked Representation

Sequential Representation:- (sequential representation of Binary trees – or linear


representation of binary trees
For given tree T, the sequential representation can be obtained by using a linear array say TREE as
follows :
(a) The root R of the tree T is stored in TREE[1] (i.e as first element of array)
(b) If a node is at location N of the array, store its left child at TREE[2*N] i.e. at location (2*N) and store
its right child at location (2*N-1) i.e at TREE[2*N+1].

 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

e.g. If Depth = 3 , Array size required = 23+1 – 1 = 24 – 1 =15


Advantages of linear representation : -
• It is very simple.
• From any child node located at location N its parent node can be determined
easily which will be at location N/2 (integer division)
• Implementation is very easy.
Disadvantages : -
• Insertion and deletion of a node requires considerable data movement and it
takes excessive amount of processing time.
• If the tree is not complete binary or nearly complete binary tree there will be
wastage of memory.
• Liked representation of Binary Tree : -
The Binary trees can have at most two offspring, so they can be represented
using storage nodes as shown below fig.

LPTR INFO RPTR

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

Advantages of linked representation : -


It is most efficient.
Disadvantages : -
1. Approximately half of the entries in the pointer fields LPTR & RPTR will contain null
values , hence it is a wastage of memory space.
2. it is difficult to determine parent node form the given node.
3. its implementation is more difficult in older languages (e,g FORTARAN, BASIC) that
do not support dynamic storage technique.
Linked Representation:
• The linked representation uses three
parallel arrays, INFO,LEFT & RIGHT.
A pointer variable for ROOT.
• Each node N of tree T will
correspond to a location k such that:
1) INFO[k] contains the data at node N.
2) LEFT[k] contains the location of left
child of node N.
3) RIGHT[k] contains the location of right
child of node N.
• ROOT will contain location of root R
of T.
Traversal of Binary Tree: Pre-order, In-order, and Post-order Traversal

Traversal : - Traversal is a procedure by which each node in the tree is processed


exactly once in a systematic manner.
The meaning of “processed” depends on the nature of the application e.g if a tree is
representing an arithmetic expression then processing of a node which represents an
arithmetic operation would probably mean performing or executing that operation.
There are three main ways of traversing a binary tree : -
(1) Preorder (2) Inorder (3) Postorder

These traversals can be defined using following recursive methods.


1. The Preorder traversal of a binary tree .
• Process the root node.
• Traverse the left subtree in preorder.
• Traverse the right subtree in preorder.
2 . Inorder traversal of binary tree can be given by following steps : -

• Traverse the left subtree inorder.


• Process the root node.
• Traverse the right subtree in inorder.
For tree given in fig (a) inorder traversal results in following processing
order :
C B A E F D G

3. Postorder traversal of a binary tree can be defined as follows : -


• Traverse the left subtree in postorder.
• Traverse the right subtree in postorder
• Process the root node.

The postorder traversal for given tree fig(a) can be specified as

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

If the words “left” and “right” are


interchanged in the preceding
definition of traversals, we have
three new traversal methods called
converse preorder, converse inorder
and converse postorder.
The traversal algorithms can be designed either in recursive or non recursive manner.
Non-recursive algorithms using stack for traversal of binary tree is as follows.
Preorder Traversal: (TLR)
Algorithm: Given a binary tree whose root node is T. S 3. [End of chain ?]
is stack variable , Top is the top pointer of stack . P is a If P ≠ 𝑁𝑢𝑙𝑙
temporary variable for storing current position in tree
Then go to step 2
1. [Initialize]
4. [unstack a right branch address]
If T= Null If Top=0
Then exit ( the tree has no Root and hence is Then Exit;
not a proper binary tree); otherwise
otherwise set P← 𝑆[𝑇𝑜𝑝]
Set P← 𝑇 𝑎𝑛𝑑 𝑇𝑜𝑝 ← 0 Top ← 𝑇𝑜𝑝 − 1
2. [Visit node, stack right branch address , and go left ] Go to step 2.
Process node P
If RLink(P) ≠ 𝑁𝑢𝑙𝑙
Then set Top ← 𝑇𝑜𝑝 + 1
S[Top]← 𝑅𝐿𝑖𝑛𝑘(𝑃)
set P ← 𝐿𝐿𝑖𝑛𝑘(𝑃)
Here the rightmost element on the stack is element on Top of the stack, the notation “NE” for example
is used to denote address of the node E. Visit of a node means output of the label for that node.

Stack content P Visit P Output string


NA A A
NE NB B AB
NE ND NC C ABC
NE ND NULL
NE ND D ABCD
NE NULL
NE E ABCDE
NF NULL
NF F ABCDEF
NG G ABCDEFG
NH NULL
NH H ABCDEFGH
NULL
Postorder Traversal: (LRT)
4. [unstack a right branch address]
Algorithm: If Top=0
The variable are as defined earlier Then Exit;
1. [Initialize] Otherwise
If T= Null P← 𝑆[𝑇𝑜𝑝]
Then exit Top ← 𝑇𝑜𝑝 − 1
Otherwise 5. [Restack address if right subtree in
not traversed]
P← 𝑇
If P < 0
Top← 0
Then, go to step 6
2. [Visit node, stack right branch address , & go Otherwise,
left ]
Top ←Top +1,
Top ←Top +1 S[Top]← − 𝑃,
S[Top]← 𝑃 P← 𝑅𝐿𝑖𝑛𝑘 (𝑃), & 𝑔𝑜 𝑡𝑜 𝑠𝑡𝑒𝑝 3.
P ← 𝐿𝐿𝑖𝑛𝑘(𝑃) 6.[Visit node]
3. [End of chain ?] P← −𝑃
If P ≠ 𝑁𝑢𝑙𝑙 Process node P
Then go to step 2 Go to step 4.
Inorder traversal : (LTR)
• Create a Stack.
• Push the root into the stack and set the root = root.left continue till it
hits the NULL.
• If root is null and Stack is empty Then
• return, we are done.
Else
• Pop the top Node from the Stack and set it as, root =
popped_Node.
• print the root and go right, root = root.right.
• Go to step 2.
End If

You might also like