forked from ReciHub/FunnyAlgorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
right_view.cpp
202 lines (174 loc) · 2.96 KB
/
right_view.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
#include<bits/stdc++.h>
using namespace std;
class node{
public:
int data;
node*right;
node*left;
node(int d)
{
data=d;
node*right=NULL;
node*left=NULL;
}
};
node* build_tree()
{
int d;
cin>>d;
if(d==-1)
return NULL;
node* root=new node(d);
root->right=build_tree();
root->left=build_tree();
return root;
}
void print(node*root)
{
if(root==NULL)
return;
cout<<root->data<<" ->";
print(root->right);
print(root->left);
}
void preorder(node*root)
{
if(root==NULL)
return;
//follow root left right
cout<<root->data<<" ";
preorder(root->left);
preorder(root->right);
}
void inorder(node*root)
{
if(root==NULL)
return;
//follow left root right
preorder(root->left);
cout<<root->data<<" ";
preorder(root->right);
}
void postorder(node*root)
{
if(root==NULL)
return;
//follow left right root
preorder(root->left);
preorder(root->right);
cout<<root->data<<" ";
}
int height(node*root)
{
if(root==NULL)
return 0;
int left_size=height(root->left);
int right_size=height(root->right);
return max(left_size,right_size)+1;
}
void levelorder(node*root,int k)
{
if(root==NULL)
return;
if (k==1)
{
cout<<root->data<<" ";
return;
}
levelorder(root->left,k-1);
levelorder(root->right,k-1);
}
void bfs(node*root)
{
queue<node*>q;
q.push(root->data);
while(!q.empty())
{
node*f=q.front();
cout<<f->data<<" ";
q.pop();
//last node condition
if(f->left)
q.push(f->left);
if(f->right)
q.push(f->right);
}
}
int no_nodes(node*root)
{
if(root==NULL)
return 0;
return (no_nodes(root->left)+no_nodes(root->right)+1);
}
int sum_nodes(node*root)
{
if(root==NULL)
return 0;
return (sum_nodes(root->left)+sum_nodes(root->right)+1);
}
int diameter(node*root)
{
if(root==NULL)
return 0;
int h1=height(root->left);
int h2=height(root->right);
int case1=h1+h2;
int case2=diameter(root->left);
int case3=diameter(root->right);
return max(case1,max(case2,case3));
}
node* array_tree(int *arr,int start,int end)
{
int mid=(start+end)/2;
if(start>end)
return NULL;
//making mid element of an array as a root
node* root=new node(arr[mid]);
root->left=array_tree(arr,start,mid-1);
root->right=array_tree(arr,start,mid+1);
return root;
}
//using preorder and inorder traversal
node* traversaltree(int *pre, int *in,int start,int end)
{
int i=0,index=-1;
if(start>end)
return NULL;
node*root=new node(pre[i]);
for(int j=start;j<end;j++)
{
if(in[j]==pre[i])
{
index=j;
break;
}
}
i++;
root->left=traversaltree(pre,in,start,index-1);
root->right=traversaltree(pre,in,index+1,end);
return root;
}
void right_view(node*root,int curr_level,int max_level)
{
if(root==NULL)
return;
if(max_level<curr_level)
{
cout<<root->data<<" ";
max_level=curr_level;
}
right_view(root->right,curr_level+1,max_level);
right_view(root->left,curr_level+1,max_level);
}
int main()
{
node*root=build_tree();
print(root);
inorder(root);
cout<<"\n";
postorder(root);
cout<<"\n";
height(root);
cout<<"\n";
return 0;
}