-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreOrderBinaryTree.cpp
More file actions
85 lines (78 loc) · 2.19 KB
/
preOrderBinaryTree.cpp
File metadata and controls
85 lines (78 loc) · 2.19 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
//Non-Recursive PreOrder Traversal
#include<bits/stdc++.h>
using namespace std;
struct BinaryNode{
struct BinaryNode* left;
int salary;
struct BinaryNode* right;
};
stack<int> preOrderTraversal(struct BinaryNode* root){
stack<int> list;
stack<struct BinaryNode*> Stack;
struct BinaryNode* temp=root;
Stack.push(temp);
while(!Stack.empty()){
struct BinaryNode* temp= Stack.top();
Stack.pop();
list.push(temp->salary);
if(temp->right != NULL){
Stack.push(temp->right);
}
if(temp->left != NULL){
Stack.push(temp->left);
}
}
return list;
}
int main()
{
struct BinaryNode* ceo=NULL;
struct BinaryNode* head_oficer_1=NULL;
struct BinaryNode* head_oficer_2=NULL;
struct BinaryNode* h1_manager_1=NULL;
struct BinaryNode* h1_manager_2=NULL;
struct BinaryNode* h2_manager_1=NULL;
struct BinaryNode* h2_manager_2=NULL;
ceo=(struct BinaryNode*) malloc(sizeof(struct BinaryNode));
head_oficer_1=(struct BinaryNode*) malloc(sizeof(struct BinaryNode));
head_oficer_2=(struct BinaryNode*) malloc(sizeof(struct BinaryNode));
h1_manager_1=(struct BinaryNode*) malloc(sizeof(struct BinaryNode));
h1_manager_2=(struct BinaryNode*) malloc(sizeof(struct BinaryNode));
h2_manager_1=(struct BinaryNode*) malloc(sizeof(struct BinaryNode));
h2_manager_2=(struct BinaryNode*) malloc(sizeof(struct BinaryNode));
ceo->salary=50000;
ceo->left=head_oficer_1;
ceo->right=head_oficer_2;
head_oficer_1->salary=40000;
head_oficer_1->left=h1_manager_1;
head_oficer_1->right=h1_manager_2;
h1_manager_1->salary=30000;
h1_manager_1->left=NULL;
h1_manager_1->right=NULL;
h1_manager_2->salary=35000;
h1_manager_2->left=NULL;
h1_manager_2->right=NULL;
head_oficer_2->salary=45000;
head_oficer_2->left=h2_manager_1;
head_oficer_2->right=h2_manager_2;
h2_manager_1->salary=32000;
h2_manager_1->left=NULL;
h2_manager_1->right=NULL;
h2_manager_2->salary=33000;
h2_manager_2->left=NULL;
h2_manager_2->right=NULL;
int count=0;
stack <int> pre_order_traversal=preOrderTraversal(ceo);
stack<int> temp;
while(!pre_order_traversal.empty()){
temp.push(pre_order_traversal.top());
pre_order_traversal.pop();
}
while(!temp.empty()){
int x= temp.top();
temp.pop();
cout<<x<<" ";
}
cout<<endl;
return 0;
}