Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 4

Bhadja Divya Jayantibai

PES1PG22CA041
MCA(A)
A) Display the elements per level from left to right
1.start
2.Create a queue with type a node
3.push root node in queue
4. Repeat step 5 to 6 until queue is empty
5. Assign s to Number of element in queue
6. for i=0;i<s;i++
- Assign front of queue to top
- pop element from queue
- if top->left != NULL
- push top->left element in queue
-if top->right != NULL
- push top->right element in queue
7.end for loop
8.end
B) Given a tree find the height of a tree
1.start
2.create a function which calculate the height of tree and pass
parameter as root and call it recursively in following manner
3.if root == NULL
return 0;
4. return max(fun_name(root->left),fun_name(root->right))
5.print(fun_name(root))
6.end
c. Given a particular node find the depth at which it is present
in the tree

step 1 :- start
step 2 :- if root == NULL
return -1
step 3 :- if root == searchNode
return depth
step 4 :- assign findnodedepth(root->left, searchNode, depth+1)
to leftdepth
step 5 :- if leftdepth != -1
return leftdepth
step 6 :- assign findnodedepth(root->right, searchNode,
depth+1)to rightdepth
step 7 :- if rightdepth != -1
return rightdepth
step 8 :- return -1

d. Given two nodes find whether they have any common node
as ancestor

step 1 :- start
step 2 :- if root == NULL
return NULL
step 3 :- if root == node1 or root == node2
return root
step 4 :- assign findCommanParent( nod1, node2, root->left) to
left
step 5 :- assign findCommanParent( nod1, node2, root->right)
to right
step 6 :- if left and right
return root
step 7 :- if left then return left
else return right
step 8 :- end

You might also like