SoFunction
Updated on 2025-03-09

Iterative algorithm for binary tree traversal implementation in C language

This article describes the iterative algorithm that implements binary tree traversal in C language, which is a very classic type of algorithm in data structure algorithms. Share it for your reference.

The specific implementation method is as follows:

Iterative algorithm for order traversal in binary tree:

#include <iostream>
#include <stack>

using namespace std;

struct Node { 
 Node(int i, Node* l = NULL, Node* r = NULL) : item(i), left(l), right(r) {} 
 int item; 
 Node* left; 
 Node* right; 
}; 

Node* construct() { 
 Node* node6 = new Node(16); 
 Node* node5 = new Node(12); 
 Node* node4 = new Node(8); 
 Node* node3 = new Node(4); 
 Node* node2 = new Node(14, node5, node6); 
 Node* node1 = new Node(6, node3, node4); 
 Node* node0 = new Node(10, node1, node2); 

 return node0; 
}

//Recursive algorithmvoid inorder(Node *root)
{
 if (root == NULL)
 return;
 inorder(root->left);
 cout << root->item << " ";
 inorder(root->right);
}

void preorder(Node *root)
{
 if(root == NULL)
 return;

 cout << root->item << " ";
 preorder(root->left);
 preorder(root->right);
}

void postorder(Node *root)
{
 if (root == NULL)
 return;

 postorder(root->left);
 postorder(root->right);
 cout << root->item << " ";
}

void postorder2(Node *root)
{
 if (root == NULL)
 return;

 stack<Node *> nstack;
 Node *pre = NULL;
 (root);
 Node *node = NULL;

 while (!())
 {
 node = ();
 if (pre != node->left && pre != node->right)
 {
  if (node->right)
  (node->right);
  if (node->left)
  (node->left);
 }

 if (node->left == NULL && node->right == NULL 
  || pre == node->left || pre == node->right)
 {
  cout << node->item << " ";
  ();
 }
 pre = node;
 }
}

void preorder2(Node *root)
{
 if(root == NULL)
 return;

 stack<Node *> nstack;
 Node *node = root;

 while (node != NULL || !())
 {
 while(node != NULL)
 {
  cout << node->item << " ";
  (node);
  node = node->left;
 }
 node = ();
 ();
 node = node->right;
 }
}

void preorder3(Node *root)
{
 if (root == NULL)
 return;

 stack<Node *> nstack;
 (root);
 Node *node = NULL;

 while (!())
 {
 node = ();
 ();
 cout << node->item << " ";

 if (node->right)
  (node->right);
 if (node->left)
  (node->left);
 }
}

//Iteration algorithmvoid inorder2(Node *root)
{
 if(root == NULL)
 return;

 stack<Node *> nstack;
 (root);
 Node *next = root->left;

 while (next != NULL || !())
 {
 while (next != NULL)
 {
  (next);
  next = next->left;
 }
 next = ();
 ();

 cout << next->item << " ";
 next = next->right;
 }
}

int main()
{
 Node *root = construct();
 cout << "------------------------------------------------------------------------------------------------------------------------------ << endl;
 inorder(root);
 cout << endl;
 cout << "------------------------------------------------------------------------------------------------------------------------------ << endl;
 inorder2(root);
 cout << endl;
 cout << "---------------------------" << endl;
 preorder(root);
 cout << endl;
 cout << "------------------------------------------------------------------------------------------------------------------------------ << endl;
 preorder2(root);
 cout << endl;
 cout << "----------------------------------" << endl;
 preorder3(root);
 cout << endl;
 cout << "------------------------------------------------------------------------------------------------------------------------------ << endl;
 postorder(root);
 cout << endl;
 cout << "----------------------------------" << endl;
 postorder2(root);
}

Regarding preorder traversal, the algorithm written later is as follows for your reference:

void preOrderIterator(Node *root)
{
 if (root == NULL)
 return;

 stack<Node*> nstack;
 (root);

 while (!())
 {
 Node *top = ();
 while (top != NULL)
 {
  if (top->left)
  (top->left);
  cout << top->data << " ";
  top = top->left;
 }
 while (top == NULL && !())
 {
  top = ()->right;
  ();
 }

 if (top != NULL)
  (top);
 }
}

I believe that this article has certain reference value for learning C program algorithm design.