-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbin_trees.cpp
144 lines (112 loc) · 2.14 KB
/
bin_trees.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
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
struct node{
int x;
node *left, *right;
};
void create(node *&r, ifstream &f){
int a;
f>>a;
if(a){
r = new node;
r->x = a;
create(r->left, f);
create(r->right, f);
}
else{
r = NULL;
}
}
int no_leaves(node *r){
if(r){
if(!r->left && !r->right){
return 1;
}
else{
return no_leaves(r->left) + no_leaves(r->right);
}
}
return 0;
}
int no_nodes(node *r){
if(r){
return 1 + no_nodes(r->left) + no_nodes(r->right);
}
return 0;
}
void RSD(node *r){
if(r){
cout<<r->x;
RSD(r->left);
RSD(r->right);
}
}
int even_nodes_sum(node *r){
if(r){
int t=0;
if(r->x % 2 == 0){
t = r->x;
}
return t + even_nodes_sum(r->left) + even_nodes_sum(r->right);
}
return 0;
}
bool is_prime(int x){
if(x <= 1){
return false;
}
for(int i=2; i<=sqrt(x); i++){
if(x % i == 0){
return false;
}
}
return true;
}
int max_of_primes(node *r){
int max = 0;
if(r){
if(is_prime(r->x)){
max = r->x;
}
int left_max = max_of_primes(r->left);
int right_max = max_of_primes(r->right);
if(max < left_max){
max = left_max;
}
if(max < right_max){
max = right_max;
}
}
return max;
}
int get_tree_height(node *r){
int h = 0;
if(r){
if(!r->left && !r->right){
return 0;
}
int left_h = get_tree_height(r->left);
int right_h = get_tree_height(r->right);
if(left_h > right_h){
h = left_h + 1;
}
else{
h = right_h + 1;
}
}
return h;
}
int main(){
node *r;
ifstream f("trees.in");
create(r, f);
RSD(r);
cout<<'\n'<<no_leaves(r)<<'\n';
cout<<no_nodes(r)<<'\n';
cout<<even_nodes_sum(r)<<'\n';
cout<<max_of_primes(r)<<'\n';
cout<<get_tree_height(r)<<'\n';
f.close();
}