-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtree_non_recursive.cpp
94 lines (73 loc) · 1.72 KB
/
tree_non_recursive.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include<bits/stdc++.h>
using namespace std;
struct node {
int roll;
struct node *left, *right;
};
class Stack{
int top;
node* arr[10];
public:
Stack(){
top = -1;
}
void push(node* p){
top++;
arr[top] = p;
}
node* pop(){
node* q;
q = arr[top];
top--;
return q;
}
bool isEmpty(){
if(top == -1){
return true;
}
else{
return false;
}
}
};
class Tree{
public:
node* newNode(int data){
node *temp = new node;
temp->roll = data;
temp->left = temp->right = NULL;
return temp;
}
void preOrder(node* root){
node* t = root;
Stack st;
while(t != NULL){
cout<<t->roll<<" ";
st.push(t);
t = t->left;
}
while(!st.isEmpty()){
t = st.pop();
t = t->right;
while(t!= NULL){
cout<<t->roll<<" ";
st.push(t);
t = t->left;
}
}
}
void postOrder(node* root){
node* t = root;
Stack st;
}
};
int main() {
Tree tt;
node* root = tt.newNode(1);
root->left = tt.newNode(2);
root->right = tt.newNode(3);
root->left->left = tt.newNode(4);
root->left->right = tt.newNode(5);
tt.preOrder(root);
return 0;
}