-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTree.cpp
More file actions
165 lines (136 loc) · 2.67 KB
/
BinaryTree.cpp
File metadata and controls
165 lines (136 loc) · 2.67 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
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
// BinaryTrees
#include <iostream>
using namespace std;
class node{
public:
int data;
node* left;
node* right;
node(int d){
data = d;
left = right = NULL;
}
};
node* CreateTree(){
int data;
cin>>data;
if(data == -1){
return NULL;
}
node* root = new node(data);
root->left = CreateTree();
root->right = CreateTree();
return root;
}
void PreOrder(node* root){
if(root == NULL){
return;
}
cout<<root->data<<" ";
PreOrder(root->left);
PreOrder(root->right);
}
void InOrder(node* root){
if(root == NULL){
return;
}
InOrder(root->left);
cout<<root->data<<" ";
InOrder(root->right);
}
void PostOrder(node* root){
if(root == NULL){
return;
}
PostOrder(root->left);
PostOrder(root->right);
cout<<root->data<<" ";
}
int CountNodes(node* root){
if(root == NULL){
return 0;
}
return CountNodes(root->left)+CountNodes(root->right)+1;
}
int Height(node* root){
if(root == NULL){
return 0;
}
int left = Height(root->left);
int right = Height(root->right);
return max(left,right)+1;
}
int Diameter(node* root){
if(root == NULL){
return 0;
}
int op1 = Height(root->left) + Height(root->right);
int op2 = Diameter(root->left);
int op3 =
Diameter(root->right);
return max(op1,max(op2,op3));
}
class Pair{
public:
int height;
int diameter;
};
Pair FastDiameter(node* root){
Pair p;
if(root == NULL){
p.height = p.diameter = 0;
return p;
}
Pair left = FastDiameter(root->left);
Pair right= FastDiameter(root->right);
p.height = max(left.height,right.height)+1;
int op1 = left.height + right.height;
int op2 = left.diameter;
int op3 = right.diameter;
p.diameter = max(op1,max(op2,op3));
return p;
}
void kthLevelorder(node* root,int k)
{
if(root==NULL)
{
return;
}
if(k==1)
{
cout<<root->data<<" ";
return;
}
kthLevelorder(root->left,k-1);
kthLevelorder(root->right,k-1);
return;
}
void printAllLevel(node* root)
{
int h=Height(root);
for(int i=1;i<=h;i++)
{
kthLevelorder(root,i);
cout<<endl;
}
return;
}
int main(){
node* root = NULL;
root = CreateTree();
PreOrder(root);
cout<<endl;
InOrder(root);
cout<<endl;
PostOrder(root);
cout<<endl;
cout<<"Number of Nodes : "<<CountNodes(root)<<endl;
cout<<"Height of Tree : "<<Height(root)<<endl;
cout<<"Diameter of Tree : "<<Diameter(root)<<endl;
Pair p =FastDiameter(root);
cout<<"Height of Tree : "<<p.height<<endl;
cout<<"Diameter of Tree : "<<p.diameter<<endl;
//kthLevelorder(root,3);
printAllLevel(root);
return 0;
}