Skip to content

Commit

Permalink
Add iterative versions of tree traversals
Browse files Browse the repository at this point in the history
  • Loading branch information
nandwalritik committed May 12, 2021
1 parent b16c702 commit 05d1608
Showing 1 changed file with 107 additions and 0 deletions.
107 changes: 107 additions & 0 deletions trees/iterativetraversalsTree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int data;
Node *left;
Node *right;
Node(int data)
{
this->data = data;
this->left = this->right = nullptr;
}
};

void postOrder(Node *root)
{
stack<Node *> st;
stack<int> out;
st.push(root);
while (!st.empty())
{
Node *tp = st.top();
st.pop();
out.push(tp->data);
if (tp->left)
{
st.push(tp->left);
}
if (tp->right)
{
st.push(tp->right);
}
}
while (!out.empty())
{
cout << out.top() << " ";
out.pop();
}
cout << endl;
}

void inOrder(Node *root)
{
if (root == nullptr)
return;
stack<Node *> st;
Node *curr = root;
while (!st.empty() || curr != nullptr)
{
if (curr != nullptr)
{
st.push(curr);
curr = curr->left;
}
else
{
curr = st.top();
st.pop();
cout << curr->data << " ";
curr = curr->right;
}
}
cout << endl;
}

void preOrder(Node *root)
{
if (root == nullptr)
return;
stack<Node *> st;
st.push(root);
while (!st.empty())
{
Node *curr = st.top();
st.pop();
cout << curr->data << " ";
if (curr->right)
{
st.push(curr->right);
}
if (curr->left)
{
st.push(curr->left);
}
}
cout << endl;
}

int main()
{
Node *root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);
root->left->left = new Node(4);
root->right->left = new Node(5);
root->right->right = new Node(6);
root->right->left->left = new Node(7);
root->right->left->right = new Node(8);
cout << "-------------Post Order------------\n";
postOrder(root);
cout << "-------------In Order------------\n";
inOrder(root);
cout << "-------------Pre Order------------\n";
preOrder(root);

return 0;
}

0 comments on commit 05d1608

Please sign in to comment.