Algorithm of preorder traversal:-

PreorderTraversal(root)

Here root is a pointer variable that holds the address of the root node of the given tree. It use a stack s whose elements are pointers to type bnode.It uses a temporary pointer variable ptr to hold the address of current node.

Begin
CreateEmptyStack(s) //call procedure to create an empty stack
Push(s, NULL) //push NULL value as sentinel value
Set ptr = root
while (ptr != NULL) // set != to mathamatical not equal to in ur hardcopy
Print : ptr ->info
if (ptr->right != NULL) then
Push( s, ptr->right )
endif
if(ptr->left != NULL) then
Set ptr = ptr->left
else
Set ptr = Pop(s) //pop value from stack and assign to ptr
endif
endwhile
End

--------------------------------------------------------
Human do error, please email:- webmaster@piyadas-world.com if you find any. Please visit http://www.piyadas-world.com for more resource.

0 comments