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

Spread the post

Top view of binary tree without hashing

Top view of binary tree without hashing. Solution are Iterative we are using a queue.

For example Given binary tree are contain following nodes.

123456879

Top View : 1 2 3 8 9

C program for Print Top view of Binary tree using Queue.


Output

View Code execution process.

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

View Queue nodes and pointers.

Stack Areamainhead(pointer)root(pointer)tail(pointer)queue_recordauxiliary(pointer)head(pointer)root(pointer)tail(pointer) Heap Areastruct Treedata (int)= 1left (pointer) right (pointer) struct Treedata (int)= 2left (pointer)= NULLright (pointer) struct Treedata (int)= 3left (pointer) right (pointer)= NULLstruct Treedata (int)= 4left (pointer) right (pointer)= NULLstruct Treedata (int)= 5left (pointer)= NULLright (pointer) struct Treedata (int)= 6left (pointer) right (pointer)= NULLstruct Treedata (int)= 7left (pointer)= NULLright (pointer) struct Treedata (int)= 8left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 9left (pointer)= NULLright (pointer)= NULLstruct Queuedistance (int)= 0link (pointer) next (pointer) struct Queuedistance (int)= -1link (pointer) next (pointer) struct Queuedistance (int)= 1link (pointer) next (pointer) struct Queuedistance (int)= 0link (pointer) next (pointer) struct Queuedistance (int)= 0link (pointer) next (pointer) struct Queuedistance (int)= -1link (pointer) next (pointer)= NULL

Try it Yourself

Spread the post

Recommended Posts: