-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary_Tree_Iterative_Orders.cpp
More file actions
87 lines (82 loc) · 1.27 KB
/
Copy pathBinary_Tree_Iterative_Orders.cpp
File metadata and controls
87 lines (82 loc) · 1.27 KB
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
#include <bits/stdc++.h>
using namespace std;
struct btnode
{
btnode *lc;
char data;
btnode *rc;
};
typedef struct btnode *btptr;
void insert(btptr &T)
{
char z;
cin >> z;
if (z=='#')
{
return;
}
else
{
T = new btnode();
T->lc = NULL;
T->data = z;
T->rc = NULL;
insert(T->lc);
insert(T->rc);
}
}
void preorder(btptr T)
{
int count=0;
if (T == NULL)
return;
stack<btptr> s;
s.push(T);
while (!s.empty())
{
T = s.top();
s.pop();
cout << T->data;
if (T->rc != NULL)
{
s.push(T->rc);
count++;
}
if (T->lc != NULL)
{
s.push(T->lc);
count++;
}
}
cout<<endl;
cout<<count+1;
}
void inorder(btptr T)
{
int cnt=0;
stack<btptr> s;
btptr crr = T;
while (!s.empty() || crr != NULL)
{
while (crr != NULL)
{
s.push(crr);
cnt++;
crr = crr->lc;
}
crr = s.top();
s.pop();
cout << crr->data;
crr = crr->rc;
}
cout<<endl;
cout<<cnt;
}
int main()
{
btptr T2;
insert(T2);
preorder(T2);
cout<<endl;
inorder(T2);
}