You are on page 1of 5

ASSIGNMENT-4

Q.1 Define the following terms:


Ans: 1) Graph: A graph is a non empty set of V (set of nodes) and a set of E (set of edges), and a mapping from the set
of edges E to set of pairs of elements of V.
We can also write graph G = (V,E)
2) Directed Graph (Diagraph): A graph in which every edge is directed is called directed graph
3) Mixed Graph: A graph in which some of the edges are directed and some of the edges are undirected then
the graph is called mixed graph
4) Parallel edge: if a pair of node is connected by more than one edge then such edges are called parallel edges
5) Multi graph: A graph which contains some parallel edges is called multi graph.
6) Weighted graph: A graph in which weights are assigned to every edge is called weighted graph
7) Null graph: A graph which contains only isolated nodes is called null graph
8) Path: A path is said to traverse through nodes appearing in sequence, starting from initial node of the first
edge and ending in the terminal node of the last edge in the sequence.
9) Out degree: in a directed graph, for any node V, the no of edges which have V as their initial node is called the
out degree of the node V.
10) In degree: in a directed graph, for any node V, the no of edges which have V as their terminal node is called
the in degree of the node V.
11) Tree: A tree is defined as a finite set of one or more node such that
- There is a special node called the root node R
- The remaining nodes are divided into n≥0 disjoint sets T1, T2,.., Tn, where each of these sets are the
tree. T1, T2,,,Tn are called the sub tree of the root
12) Forest: A forest is a set of n≥0 disjoint trees.
13) Leaf node: in a directed tree a node which has out degree 0 is called the leaf node or terminal node.
14) Root node: in a directed tree a node which has in degree 0 is called the root node.
15) Directed tree: a directed tree is acyclic diagraph which has one node called the root node with in degree 0
while all others nodes have in degree 1.
16) M-ary tree: if in a directed tree the out degree of every node is less than or equal to m then the tree is called
m-ary tree
17) Binary tree: if in a directed tree the out degree of every node is less than or equal to 2 then the tree is called
binary tree.
18) Complete binary tree: if out degree of every node is exactly equal to 2 or 0 and number of nodes at level i is
2i-1 then the tree is called full or complete binary tree.
19) Ordered tree: if in a directed tree, an ordering of the node at each level prescribed then such a tree is called
ordered tree.
20) Level of the tree: in a tree, the distance between two nodes are representing as level.
21) Sibling: the child of the same parent is known as sibling node.

Q.2 What is tree traversal? What are different methods of tree traversal? Write an
algorithm for PREORDER traversal and explain.
Ans: The traversal is a method of processing every node in the tree exactly once in a systematic manner.
Types of traversal are:
- Pre order traversal - In order traversal - Post order traversal
Algorithm: PREORDER (T)
- This function traverses the tree in pre-order
- T is a pointer which points to the root node of the tree
Step1. [process the root node]
If T != NULL
then write (DATA (T))
else write (‘EMPTY TREE’)
return
step2. [process the left sub tree]
if LPTR (T) != NULL
then call PREORDER (LPTR(T))
step3. [process the right sub tree]
if RPTR (T) != NULL
then call PREORDER (RPTR(T))
step4. [finished]
return

Q.3 Write an algorithm for IN ORDER and POST ORDER traversal


Ans: Algorithm: INORDER (T)
- This function traverses the tree in in-order
- T is a pointer which points to the root node of the tree
Step1. [check for empty tree]
If T = NULL
Then write (‘EMPTY TREE’)
return
step2. [process the left sub tree]
if LPTR (T) != NULL
then call INORDER (LPTR(T))
step3. [process the root node]
write (DATA (T))
step4. [process the right sub tree]
if RPTR (T) != NULL
then call INORDER (RPTR(T))
step5. [finished]
return
Algorithm: POSTORDER (T)
- This function traverses the tree in post-order
- T is a pointer which points to the root node of the tree
Step1. [check for empty tree]
If T = NULL
Then write (“EMPTY TREE”)
Return
Step2. [process the left sub tree]
if LPTR (T) != NULL
then call POSTORDER (LPTR(T))
step3. [process the right sub tree]
if RPTR (T) != NULL
then call POSTORDER (RPTR(T))
step4. [process the root node]
write (DATA (T))
step5. [finished]
return

Q.4 Write short-note on: Application of trees.


Ans: The main applications of tree are as follows:
- Manipulation of arithmetic expressions - Construction of symbol table - Syntax analysis
1) Manipulation of arithmetic expressions :
- Binary tree is used to represent formulas in prefix or postfix notations.
2) Construction of symbol table
- Symbol table is a dictionary which an important part of compiler containing names and their
associated values.
- Symbol table keeps track of scope and binding of information
- Tree is used in symbol table operations i.e. insertion and look up
- Binary tree is used is used because of two reasons: first reason – symbol table entries are in
lexicographic order then table becomes equivalent to binary search. Second – because of tree few
pointers are needed.
- Symbol table is implemented by linear list.
3) Syntax analysis
- Tree is used in grammars of syntax analysis or parsing.
Q.5 What is Binary Tree (BST)? List out Binary tree operations and explain Insertion and Deletion of Binary Tree
with example.
Ans: - A tree is called binary tree or Binary Search tree (BST) if each and every node can have most two branches.
- A binary tree is having a node called root node. And root node has left and right sub tree.

- In this figure of binary tree, node 1 is root node which has node 3 as left
sub tree and node 6 as right sub tree.
- Node 1, 3, 6 are non terminal nodes as they do not have out degree = 0
- Node 5, 9 and 8 have out degree = 0 so they are terminal nodes or leaf
nodes.

Operations on Binary Tree


- Traversal - Insertion - Deletion - Searching - Copying
Characteristics of Binary Search Tree
- All the nodes to the left of the root node have value less than the value of the root node and
- All the nodes to the right of the root node have value greater than the value of the root node
Suppose we want to construct the binary tree for the following data:
45 68 35 42 15
Insertion operation on Binary Tree

- First node value is 45 and it is inserted as a root node. (always in the


45
construction of the binary tree take the first node as a root node.
Now the binary tree is looking like:
- Second node value is 68 and it is greater than the root node value, so
it is inserted at the right of the root 45

68

45
- Now, if we want to insert the node 35, its value is less than root
value. So it is inserted at the right of the root, like: 35 68

45

- Now, do the same process for inserting all other remaining nodes as 35 68
describes above. Finally we get
15 42

Deletion operation on Binary Tree


- There are three possibilities when we want to delete a node from binary tree
1) If the node is to be deleted is terminal node or leaf node then delete it directly.
2) If the node which is to be deleted has only one left sub tree or one right sub tree.
For example in a tree shown below, the node which is to be deletes has one left sub tree. Like:
45 - Suppose in this tree, we want to delete the node 35, which has one left sub
tree.
35
68 - So, when we delete or remove node 35, set the left link of node 45 (parent
of node 35) to the left sub tree of node 35. ( OR link the left sub tree of the
15 node 35 to its parent node.

3) If the node which is to be deleted has left and right sub tree., then
we have to do following steps:
- Find in order successor of the deleted node
- Append the right sub tree of the in order successor to its grand
parent
- Replace the node to be deleted with its in order successor
node
- Suppose in this tree if we want to delete node 7 which has non
empty left and right sub tree. Then find out the in order
successor for the node 7 which is node 5
- Now replace node 7 with node 5 (remove the link of node 5)
and now pointer of node 2 is pointing to node 5.
Q.6 Explain list representation of binary tree with suitable example
Ans: We can represent binary tree in memory by two methods.
- Array representation - Linked implementation
1) Array representation
- In array representation of binary tree, consider two cases
1. Array implantation of complete binary tree
- A complete binary tree has one node at root level, two nodes at level two, four at
level 3 and so on.
Index 1 2 3 4 5 6 7
Info A B C D E F G
- In this type of implementation, location of left child of node I is given by 2*I and the
location of right child of node I is given by 2*I+1. i.e in this tree the left child of node
C (index 3) is 3 * 2 = 6, that is node F
- Similarly the location of the parent node j is given by TRUNC (j/2). i.e. parent of node
D (index 4) is 4/2=2 (that is node B)
2. Array implantation of incomplete binary tree
Index 1 2 3 4 5 6 7 8 9 10 11 12 13
Info F B G A D - I - - C E H -

- In this type of implementation, location of left child of node I is given by 2*I and the
location of right child of node I is given by 2*I+1. i.e in this tree the left child of node
B (index 2) is 2 * 2 = 4, that is node A
- But here, for node A (index 4) has left child 2*4=8, but index 8 is empty. So wastage
of memory is there in this case.
- Similarly the location of the parent node j is given by TRUNC (j/2). i.e. parent of node
D (index 4) is 4/2=2 (that is node B)
- Array implementation is not efficient for this method.
2) Linked representation
- in binary tree there is a node called root node, this root node again has two parts: left sub tree and
right sub tree.
- Thus a node in binary tree is represented as below
LPTR INFO RPTR
- INFO contain the actual value of the node
- LPTR is a pointer points to left child node
- RPTR is a pointer points to right child node
- If a node which do not have either of left or right child empty then LPTR or RPTR is NULL
- Binary tree using linked list is shown below
Binary Tree Linked representation
A

B C

D E F G

Q.7 Write Tree Order Traversal (In order, Preorder, Post order) of the given tree

(C) (D)
(A) (B)

For tree A:
- Pre order: G D B F G C M L P
- In order: BDEFGCLMP
- Post order: B E F D L P M C G
For tree B:
- Pre order: A B D G C E H I F
- In order: DGBAHEICF
- Post order: G D B H I E F C A
For tree C:
- Pre order: M E B A D L P N V T Z
- In order: ABDELMNPTVZ
- Post order: A D B L E N T Z V P M
For tree D:
- Pre order: A B C E I F J D G H K L
- In order: E I C J F B G D K H L A
- Post order: I E J F C G K L H D B A

You might also like