-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathHeight and sum of nodes in binary tree.cpp
189 lines (152 loc) · 3.28 KB
/
Height and sum of nodes in binary tree.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
#include<bits/stdc++.h>
using namespace std;
class Node{
public:
int data;
Node* lchild;
Node* rchild;
}; Node* base;
class Queue{
public:
int front;
int rear;
int size;
Node** arr;
bool isEmpty();
bool isFull();
void enqueue(Node* p);
Node* dequeue();
Queue(int size);
};
Queue::Queue(int size){
front = -1;
rear = -1;
this->size = size;
arr = new Node* [size];
}
bool Queue :: isEmpty(){
if(front==rear){
return true;
}
return false;
}
bool Queue :: isFull(){
if(rear==size-1){
return true;
}
return false;
}
void Queue :: enqueue(Node* p){
if(isFull()){
cout<<"Queue Overflow"<<endl;
}
else{
rear++;
arr[rear]=p;
}
}
Node* Queue :: dequeue(){
Node* x = nullptr;
if(isEmpty()){
cout<<"Queue underflow"<<endl;
}
else{
front++;
x = arr[front];
}
return x;
}
void createtree(){
base = new Node;
Node* t;
Node* p;
Queue q(10);
int value;
cout<<"Enter the value of base node: "<<endl;
cin>>value;
base->data = value;
base->lchild = nullptr;
base->rchild = nullptr;
q.enqueue(base);
while(!q.isEmpty()){
p = q.dequeue();
cout<<"Enter the value of Left child "<<p->data<<": ";
cin>>value;
if(value != -1){
t = new Node;
t->data = value;
t->lchild = nullptr;
t->rchild = nullptr;
p->lchild = t;
q.enqueue(t);
}
cout<<"Enter the value of right child "<<p->data<<": ";
cin>>value;
if(value!=-1){
t = new Node;
t->data = value;
t->lchild = nullptr;
t->rchild = nullptr;
p->rchild = t;
q.enqueue(t);
}
}
}
int countt=0;
int countnodes(Node* p){
if(p==nullptr){
return 0;
}
else{
countt++;
countnodes(p->lchild);
countnodes(p->rchild);
}
return countt;
}
int countnodes2(Node* p){
if(p==nullptr){
return 0;
}
return countnodes2(p->lchild)+countnodes2(p->rchild)+1;
}
int countdoublechildnodes(Node* p){
if(p==nullptr){
return 0;
}
if(p->lchild && p->rchild){
return countdoublechildnodes(p->lchild) + countdoublechildnodes(p->rchild)+1;
}
else return countdoublechildnodes(p->lchild) + countdoublechildnodes(p->rchild);
}
int sumofallnodes(Node * p){
if(p==nullptr){
return 0;
}
return sumofallnodes(p->lchild) + sumofallnodes(p->rchild) + p->data;
}
int heightcheck(Node* p){
if(p==nullptr){
return 0;
}
int x = heightcheck(p->lchild);
int y = heightcheck(p->rchild);
if(x>y){
return x+1;
}
else return y+1;
}
int main(){
createtree();
int x = countnodes(base);
cout<<"Number of nodes in the tree are: "<< x<<endl;
int y = countnodes2(base);
cout<<"Using method 2 number of nodes: "<< y<< endl;
int z= countdoublechildnodes(base);
cout<<"Number of nodes with double nodes are: "<< z << endl;
int k = sumofallnodes(base);
cout<<"Sum of all elements of the tree is: "<< k << endl;
int j = heightcheck(base);
cout<<"Height of the tree is: "<<j<<endl;
return 0;
}