Learn basic concept of c , c++ and python programming in regularcodes.com

Spread the post

Print Right view of a binary tree

Print right view of Binary tree using queue.

For example Given binary tree are contain following nodes.

13624578

Right View : 1 3 6 7 8

13624578

Process:

Insert all nodes of binary tree in a queue. store the element from right to left in level order traversal in binary tree. and print every level first node of binary tree. that is right view of binary tree.

View pointers and nodes of tree.

Stack Areamainhead(pointer) NULLroot(pointer)tail(pointer) NULLinorder_datatemp(pointer)inorder_datatemp(pointer) Heap Areastruct Treedata (int)= 1left (pointer) right (pointer) struct Treedata (int)= 2left (pointer) right (pointer) struct Treedata (int)= 3left (pointer)= NULLright (pointer) struct Treedata (int)= 4left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 5left (pointer) right (pointer)= NULLstruct Treedata (int)= 6left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 7left (pointer)= NULLright (pointer) struct Treedata (int)= 8left (pointer)= NULLright (pointer)= NULL

Try it Yourself

C program for Print Right view in binary tree from right to left. Using queue.


Output

view process

Stack Areamainhead(pointer) NULLroot(pointer)tail(pointer) NULLright_viewauxiliary(pointer)head1(pointer)head2(pointer)tail1(pointer)tail2(pointer)temp(pointer)dequeuehead(pointer)remove(pointer) NULL Heap Areastruct Treedata (int)= 1left (pointer) right (pointer) struct Treedata (int)= 2left (pointer) right (pointer) struct Treedata (int)= 3left (pointer)= NULLright (pointer) struct Treedata (int)= 4left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 5left (pointer) right (pointer)= NULLstruct Treedata (int)= 6left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 7left (pointer)= NULLright (pointer) struct Treedata (int)= 8left (pointer)= NULLright (pointer)= NULLstruct Queueheight (int)= 0parent (pointer)= ? link (pointer) next (pointer) struct Queueheight (int)= 1parent (pointer)= ? link (pointer) next (pointer) struct Queueheight (int)= 2parent (pointer)= ? link (pointer) next (pointer) struct Queueheight (int)= 2parent (pointer)= ? link (pointer) next (pointer)= NULLstruct Queueheight (int)= 2parent (pointer)= ? link (pointer) next (pointer)= NULL

Try it Yourself

Spread the post

Recommended Posts: