Answer to Question #240793 in C for FAJLA

Question #240793

Write a program to traverse a tree in Inorder sequence without recursion


1
Expert's answer
2021-09-22T23:44:00-0400
#include <iostream>

using namespace std;

struct node {
  int value;
  struct node *left;
  string node *right;
};

void inOrderTraversal(struct node *root) {
  if (!root) return;
  struct node *current = root;
  while (current) {
    if (!current->left) {
      cout << current->value << " ";
      current = current->right;
    } else {
      struct node *prev = current->left;
      while (prev->right && prev->right != current) {
        prev = prev->right;
      }
      if (!prev->right) {
        prev->right = current;
        current = current->left;
      } else {
        prev->right = NULL;
        cout << current->value << " ";
        current = current->right;
      }
    }
  }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS