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

Spread the post

Print all ancestors of a node in binary tree

Print all ancestors of a node in binary tree using stack.

For example Given binary tree are contain following nodes.

132578946

Output

View Code execution process.

Stack Areamainroot(pointer)top(pointer) NULL Heap Areastruct Treedata (int)= 1left (pointer) right (pointer) struct Treedata (int)= 2left (pointer) right (pointer) struct Treedata (int)= 3left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 4left (pointer)= NULLright (pointer) struct Treedata (int)= 5left (pointer) right (pointer) struct Treedata (int)= 6left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 7left (pointer)= NULLright (pointer) struct Treedata (int)= 8left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 9left (pointer)= NULLright (pointer)= NULL Stack Areamainroot(pointer)top(pointer)all_ancestorfind_node (int) =6status (int) =1store(pointer) NULLtemp(pointer)top(pointer)poptop(pointer) Heap Areastruct Treedata (int)= 1left (pointer) right (pointer) struct Treedata (int)= 2left (pointer) right (pointer) struct Treedata (int)= 3left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 4left (pointer)= NULLright (pointer) struct Treedata (int)= 5left (pointer) right (pointer) struct Treedata (int)= 6left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 7left (pointer)= NULLright (pointer) struct Treedata (int)= 8left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 9left (pointer)= NULLright (pointer)= NULLstruct Stackvisit (int)= 1link (pointer) next (pointer)= NULLstruct Stackvisit (int)= 1link (pointer) next (pointer) struct Stackvisit (int)= 2link (pointer) next (pointer)

Try it Yourself

C program for Print all ancestor of given node key in binary tree.


Output

Spread the post

Recommended Posts: