Algorithm for pre-order traversal, Data Structure & Algorithms

Hear is given a set of input representing the nodes of a binary tree, write a non recursive algorithm that must be able to give the output in three traversal orders. Write down an algorithm for checking validity of the input, i.e., the program must know if the input given is duplicated, disjoint and has a loop.                                                                                                             

The Pre-order Traversal

The process of doing a pre-order traversal iteratively then has the following steps (assuming that a stack is available to hold pointers to the proper nodes):

1. push NULL onto the stack

2. start at the root and begin execution

3. write the information in the node

4. if the pointer to the right is not NULL push

the pointer onto the stack

5. if the pointer to the left is not NULL move the pointer to the node on the left

6. if the pointer to the left is NULL pop the stack

7. repeat steps 3 to 7 until no nodes remain

Posted Date: 7/9/2012 10:08:36 PM | Location : United States







Related Discussions:- Algorithm for pre-order traversal, Assignment Help, Ask Question on Algorithm for pre-order traversal, Get Answer, Expert's Help, Algorithm for pre-order traversal Discussions

Write discussion on Algorithm for pre-order traversal
Your posts are moderated
Related Questions

Various graph traversal schemes Graph Traversal Scheme. In many problems we wish to investigate all the vertices in a graph in some systematic order. In graph we often do no

what is tree

Explain about Hidden-surface Hidden-line removal refers to wire-frame diagrams without surface rendering and polygonal surfaces with straight edges. Hidden-surface removal ref

Write a procedure (make-stack) that produces independent stack objects, using a message-passing style, e.g. (define stack1 (make-stack))  (define stack2 (make-stack)) W

Q. Develop a representation for a list where insertions and deletions can be done at either end. Such a structure is known as a Deque (Double ended queue). Write functions for inse

How does cpu''s part tming and controls generate and controls signls in computer?

Now, consider a function that calculates partial sum of an integer n. int psum(int n) { int i, partial_sum; partial_sum = 0;                                           /* L

Preconditions assertion A precondition is an assertion which should be true at the initiation of an operation. For instance, a square root operation can't accept a negative a

INSERT FUNCTION /*prototypes of insert & find functions */ list * insert_list(list *); list * find(list *, int); /*definition of  anyinsert function */ list * inser