below is the code for traversing a binary tree without using recursion but the problem with the code is we require array of pointers to store all the nodes address.
in below program the parameter nodes in the function push
is an array of pointers to store all the node addresses and poped once NULL is found.
but the method space complexity is O(n)
can any one suggest me how to do it with out cosuming much space.
void intrav(BST_t * bst)
{
BST_t *tmp;
tmp=bst;
do {
while( tmp != NULL ) {
push(nodes,tmp);
tmp = tmp->left;
}
if(!empty()){
tmp=pop(nodes);
printf(" %d ",tmp -> data );
tmp = tmp -> right ;
}
}while( !empty() || tmp != NULL );
}