forked from krishnamanojpvr/DSCPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
53_BinaryTree_LinkedList.cpp
184 lines (176 loc) · 4.18 KB
/
53_BinaryTree_LinkedList.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
#include <iostream>
#include<climits>
using namespace std;
class Node
{
public:
int data;
Node *left, *right;
Node(int data)
{
this->data = data;
this->left = NULL;
this->right = NULL;
}
};
class BinaryTree
{
private:
void printLevel(Node *root, int level);
public:
int min = INT_MAX;
int smin = INT_MAX;
Node *root;
BinaryTree()
{
root = NULL;
}
Node *Create();
void PreOrderDisplay(Node *root);
void PostOrderDisplay(Node *root);
void InOrderDisplay(Node *root);
void LevelOrderDisplay(Node *root);
void DisplayRoot();
void secondmin(Node *curr);
int Height(Node *root);
};
Node *BinaryTree::Create()
{
int data;
cout << "Enter data (-1 for no data) : ";
cin >> data;
if (data == -1)
{
return NULL;
}
Node *newnode = new Node(data);
cout << "Enter LEFT Child of " << data << " :" << endl;
newnode->left = Create();
cout << "Enter RIGHT child of " << data << " :" << endl;
newnode->right = Create();
return newnode;
}
void BinaryTree::DisplayRoot()
{
if (root != NULL)
{
cout << "**************************" << endl;
cout << "The root of the tree is : " << root->data << endl;
cout << "**************************" << endl;
return;
}
cout << "No Tree created, Root node is NULL";
}
int BinaryTree::Height(Node *root)
{
if (root == NULL)
{
return 0;
}
int lh = Height(root->left);
int rh = Height(root->right);
if (lh > rh)
{
return (lh + 1);
}
else
{
return (rh + 1);
}
}
void BinaryTree::PreOrderDisplay(Node *root)
{
if (root == NULL)
{
return;
}
cout << root->data << " ";
PreOrderDisplay(root->left);
PreOrderDisplay(root->right);
}
void BinaryTree::PostOrderDisplay(Node *root)
{
if (root == NULL)
{
return;
}
PostOrderDisplay(root->left);
PostOrderDisplay(root->right);
cout << root->data << " ";
}
void BinaryTree::InOrderDisplay(Node *root)
{
if (root == NULL)
{
return;
}
InOrderDisplay(root->left);
cout << root->data << " ";
InOrderDisplay(root->right);
}
void BinaryTree::printLevel(Node *root, int level)
{
if (root == NULL)
{
return;
}
else if (level == 0)
{
cout << root->data << " ";
}
else if (level > 0)
{
printLevel(root->left, level - 1);
printLevel(root->right, level - 1);
}
else
{
return;
}
}
void BinaryTree::LevelOrderDisplay(Node *root)
{
int height = Height(root);
for (int i = 0; i < height; i++)
{
printLevel(root, i);
}
}
void BinaryTree::secondmin(Node *curr)
{
if (curr == NULL)
{
return;
}
else if (curr->data < min )
{
smin = min;
min = curr->data;
}
else if(curr->data > min && curr->data<smin){
smin = curr->data;
}
secondmin(curr->left);
secondmin(curr->right);
}
int main()
{
BinaryTree bt;
bt.root = bt.Create();
bt.DisplayRoot();
cout << "PreOrderDisplay: ";
bt.PreOrderDisplay(bt.root);
cout << endl;
cout << "PostOrderDisplay: ";
bt.PostOrderDisplay(bt.root);
cout << endl;
cout << "InOrderDisplay: ";
bt.InOrderDisplay(bt.root);
cout << endl;
cout << "LevelOrderDisplay: ";
bt.LevelOrderDisplay(bt.root);
cout << endl;
bt.secondmin(bt.root);
cout<<bt.min<<" "<<bt.smin<<endl;
return 0;
}