-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
223 lines (168 loc) · 5.88 KB
/
main.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//===========================================================================
// Preorder Tree Construction
// Developed By: Leron Julian
// PROGRAM COMPLETED PER PROGRAM SPECIFICATION
//===========================================================================
#include <iostream>
#include <string>
#include "Queue.h"
using namespace std;
struct BinaryTreeNode
{
char Node;
BinaryTreeNode *LeftSubtree = NULL, *RightSubtree = NULL;
};
typedef BinaryTreeNode *BinaryTree;
void InOrder(BinaryTree T);
void PreOrder(BinaryTree T);
void PostOrder(BinaryTree T);
void From_PreOrder(BinaryTree root, int &index, string preorder);
int find_Max_BST(BinaryTree node, int min, int max);
int isBST(BinaryTree node);
void printLevelOrder(BinaryTree root);
void printGivenLevel(BinaryTree root, int level);
int height(BinaryTree node);
int main()
{
string preorderInput;
cout << "Enter a preorder expression -> ";
cin >> preorderInput;
BinaryTree root = new(BinaryTreeNode);
int index = 0;
From_PreOrder(root, index, preorderInput);
cout << endl;
cout << "Preorder: ";
PreOrder(root);
cout << endl;
cout << "InOrder: ";
InOrder(root);
cout << endl;
cout << "PostOrder: ";
PostOrder(root);
cout << endl;
cout << "Level-Order: ";
printLevelOrder(root);
cout << endl;
cout << endl;
if(isBST(root))
cout << "This Binary Tree is a Binary Search Tree" << endl;
else if (!isBST(root))
cout << "This Binary Tree is not a Binary Search Tree" << endl << endl;
return 0;
}
//===========================================================================
// This function prints the preorder traversal in Inorder
//===========================================================================
void InOrder(BinaryTree T)
{
if (T->LeftSubtree == NULL)
cout << "." ;
else
{
InOrder(T->LeftSubtree) ;
cout << T->Node ;
InOrder(T->RightSubtree) ;
}
}
//===========================================================================
// This function prints the preorder traversal in PreOrder
//===========================================================================
void PreOrder(BinaryTree T)
{
if (T->LeftSubtree == NULL)
cout << "." ;
else
{
cout << T->Node ;
PreOrder(T->LeftSubtree);
PreOrder(T->RightSubtree);
}
}
//===========================================================================
// This function prints the preorder traversal in PostOrder
//===========================================================================
void PostOrder( BinaryTree T )
{
if (T->LeftSubtree == NULL)
cout << "." ;
else
{
PostOrder( T->LeftSubtree ) ;
PostOrder( T->RightSubtree ) ;
cout << T->Node ;
}
}
//===========================================================================
// This function converts a preorder input to a binary tree
//===========================================================================
void From_PreOrder(BinaryTree root, int &index, string preorder)
{
char ch = preorder[index];
if (ch == '.')
return;
root->Node = ch;
root->LeftSubtree = new BinaryTreeNode;
From_PreOrder(root->LeftSubtree, ++index, preorder);
root->RightSubtree = new BinaryTreeNode;
From_PreOrder(root->RightSubtree, ++index, preorder);
}
//===========================================================================
// This function returns 0 if it is not a BST and 1 if it is
//===========================================================================
int isBST(BinaryTree node)
{
return(find_Max_BST(node, INT_MIN, INT_MAX));
}
//========================================================================================
// This function gets the maximum element in a binary tree to see if it is a binary tree
//========================================================================================
int find_Max_BST(BinaryTree node, int min, int max)
{
if (node->LeftSubtree == NULL)
return 1;
if (node->Node < min || node->Node > max)
return 0;
return(find_Max_BST(node->LeftSubtree, min, node->Node-1) && find_Max_BST(node->RightSubtree, node->Node+1, max));
}
//========================================================================================
// This function prints every prints the level order traversal of a binary tree
//========================================================================================
void printLevelOrder(BinaryTree root)
{
int h = height(root);
int i;
for (i = 1; i <= h; i++)
printGivenLevel(root, i);
}
//========================================================================================
// This function prints every level of a binary tree
//========================================================================================
void printGivenLevel(BinaryTree root, int level)
{
if (root == NULL)
return;
if (level == 1)
cout << root->Node << " ";
else if (level > 1)
{
printGivenLevel(root->LeftSubtree, level - 1);
printGivenLevel(root->RightSubtree, level - 1);
}
}
//========================================================================================
// This function gets the height of a binary tree
//========================================================================================
int height(BinaryTree node)
{
if (node == NULL)
return 0;
else
{
int left_Height = height(node->LeftSubtree);
int right_Height = height(node->RightSubtree);
if (left_Height > right_Height)
return(left_Height + 1);
else
return(right_Height + 1);
}
}