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

Spread the post

Print Left view of a binary tree

Print Left view of Binary tree using queue.

For example Given binary tree are contain following nodes.

13624578

Left View : 1 2 4 7 8

13624578

Process:

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

View pointers and nodes of tree.

Stack Areamainhead(pointer) NULLroot(pointer)tail(pointer) NULLinorder_datatemp(pointer)inorder_datatemp(pointer)inorder_datatemp(pointer)inorder_datatemp(pointer)inorder_datatemp(pointer)inorder_datatemp(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)= NULL

Try it Yourself

C program for print left view of binary tree using level order traversal. Using queue.


Output

view process

Stack Areamainhead(pointer) NULLroot(pointer)tail(pointer) NULLleft_viewauxiliary(pointer)head1(pointer)head2(pointer)tail1(pointer)tail2(pointer)temp(pointer)enqueuehead (pointer) ? height (int) ? new_node (pointer) ? tail (pointer) ? temp (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)= NULLstruct Queueheight (int)= 0link (pointer) next (pointer) struct Queueheight (int)= 1link (pointer) next (pointer) struct Queueheight (int)= 2link (pointer) next (pointer)= NULLstruct Queueheight (int)= 3link (pointer) next (pointer)= NULL

Try it Yourself

Spread the post

Recommended Posts: