Logo 
Search:

C++ Programming FAQ

Submit Interview FAQ
Home » Interview FAQ » C++ ProgrammingRSS Feeds

Iterative algorithm for traversing a binary tree in inorder in dfs (data file structure).

  Shared By: Luise Fischer    Date: Feb 02    Category: C++ Programming    Views: 1512

Answer:

PROCEDURE INORDER(T)
[Given a binary tree whose root node address is given by the pointer variable T, this algorithm traverses the tree in inorder in an iterative manner].

1. [Initialize]

If T = NULL
then write (“Empty Tree”)
return
else
TOP <-- 0.

2. [Process each stack branch address]

Repeat step 3 while TOP > 0.

3. [Get stored address and branch left]

flag <-- 0
while T != NULL
while flag != 1
call push(S,TOP,T)
T <-- LPTR(T)
if T = NULL
flag <-- 1.

4. [Traverse the right side]

D <-- pop(S,TOP)
write DATA(D)
T <-- RPTR(D)
flag <-- 0
while T = NULL
D <-- POP(S,TOP)
write DATA(D)
if TOP = 0
return
else
T <-- RPTR(D).

5. [FINISH]
return.

Share: 
 



Your Comment
  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].


Tagged: