Describe a sequence of steps to perform a preorder traversal

Describe a sequence of steps to perform a preorder traversal of a binary tree. Please use a stack and do not use recursion.

Solution

1) Start with root. and print it.

2) push right child first and left child next into stack.

3) pop fro stack and print and push its right child and left child.

4) if no childs, no need to push.

5) continue this process untill stack is empty.

6) the o/p we got is nothing but preorder.

Ex: a

b c

d e

is a tree.

print a and push c, next b.

pop and print b

then push right child e and left child d into stack. Stack has->dec

pop and print d

then push childs of d. As they are no childs.

pop and print e. e also has no childs.

pop and print c. c also has no childs.

stack is empty . if we could combine all prints abdec is preorder of above given tree

Describe a sequence of steps to perform a preorder traversal of a binary tree. Please use a stack and do not use recursion.Solution1) Start with root. and print

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site