Spread the post

Print binary tree levels in sorted order

print binary tree levels in sorted order of given level without using recursion.

For example Given Binary tree are contain following nodes.

13475926108

Result

Hint:User priority queue.

View pointers and nodes of tree.

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

Try it Yourself

C program for print sorted level of binary tree using queue.


Output

View process

Stack Areamainhead(pointer)root(pointer)tail(pointer)sorted_levelhead2(pointer)level (int) =3temp(pointer) NULLdequeuehead(pointer)remove(pointer) Heap Areastruct Treedata (int)= 1left (pointer) right (pointer) struct Treedata (int)= 3left (pointer) right (pointer) struct Treedata (int)= 2left (pointer) right (pointer) struct Treedata (int)= 5left (pointer) right (pointer)= NULLstruct Treedata (int)= 4left (pointer) right (pointer)= NULLstruct Treedata (int)= 8left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 7left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 6left (pointer)= NULLright (pointer) struct Treedata (int)= 10left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 9left (pointer)= NULLright (pointer)= NULLstruct Queuelevel (int)= 0link (pointer) next (pointer) struct Queuelevel (int)= 1link (pointer) next (pointer) struct Queuelevel (int)= 1link (pointer) next (pointer) struct Queuelevel (int)= 2link (pointer) next (pointer) struct Queuelevel (int)= 2link (pointer) next (pointer) struct Queuelevel (int)= 2link (pointer) next (pointer) struct Queuelevel (int)= 2link (pointer) next (pointer) struct Queuelevel (int)= 3link (pointer) next (pointer) struct Queuelevel (int)= 3link (pointer) next (pointer) struct Queuelevel (int)= 3link (pointer) next (pointer)= NULLstruct Queuelevel (int)= 0link (pointer) next (pointer) struct Queuelevel (int)= 0link (pointer) next (pointer) struct Queuelevel (int)= 0link (pointer) next (pointer)= NULL

Try it Yourself

Spread the post

Recommended Posts: