Binary Tree Iterative Preorder Traversal Code Example


Example: preorder without recursion

void iterativePreorder(node *root)  {      // Base Case      if (root == NULL)         return;         // Create an empty stack and push root to it      stack<node *> nodeStack;      nodeStack.push(root);      while (nodeStack.empty() == false)      {          // Pop the top item from stack and print it          struct node *node = nodeStack.top();          printf ("%d ", node->data);          nodeStack.pop();             // Push right and left children of the popped node to stack          if (node->right)              nodeStack.push(node->right);          if (node->left)              nodeStack.push(node->left);      }  }

Comments

Popular posts from this blog

Are Regular VACUUM ANALYZE Still Recommended Under 9.1?

Can Feynman Diagrams Be Used To Represent Any Perturbation Theory?