-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_counter_completely_done_by_me.cpp
executable file
·201 lines (191 loc) · 5.09 KB
/
node_counter_completely_done_by_me.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
#include <bits/stdc++.h>
using namespace std;
struct node
{
int val;
struct node *left=NULL;
struct node *right=NULL;
}*root=NULL;
void inorder(struct node *root)
{struct node *current=root;
if(current->left!=NULL)inorder(current->left);
cout<<current->val<<" ";
if(current->right!=NULL)inorder(current->right);
}
void preorder(struct node *root)
{struct node *current=root;
cout<<current->val<<" ";
if(current->left!=NULL)preorder(current->left);
if(current->right!=NULL)preorder(current->right);
}
struct node* createBST(queue<int>v)
{
struct node *current,*newnode,*prev;
if(root==NULL)
{
newnode=new struct node;
root=newnode;
current=root;
newnode->val=v.front();
//cout<<"node created "<<v.front()<<endl;(was used by me for debugging)
v.pop();
}
while(!v.empty())
{
int z;
z=v.front();
v.pop();
int j;
current=root;
while(current!=NULL)
{
prev=current;
if(z<current->val) current=current->left;
else if(z>current->val) current=current->right;
}
current=prev;
if(z<current->val)
{
current->left=new struct node;
current=current->left;
// cout<<"node created l "<<z<<endl;(was used by me for debugging)
current->val=z;
}
else
{
current->right=new struct node;
current=current->right;
// cout<<"node created r "<<z<<endl;(was used by me for debugging)
current->val=z;
}
}
return root;
}
struct node* insert(struct node*,int z)
{struct node *current,*newnode,*prev;
if(root==NULL)
{
newnode=new struct node;
root=newnode;
current=root;
newnode->val=z;
//cout<<"node created "<<v.front()<<endl;(was used by me for debugging)
}
else
{
current=root;
while(current!=NULL)
{
prev=current;
if(z<current->val) current=current->left;
else if(z>current->val) current=current->right;
}
current=prev;
if(z<current->val)
{ cout<<z<<" is Inserted at left of "<<current->val;
current->left=new struct node;
current=current->left;
// cout<<"node created l "<<z<<endl;(was used by me for debugging)
current->val=z;
}
else
{cout<<z<<" is Inserted at right of "<<current->val;
current->right=new struct node;
current=current->right;
// cout<<"node created r "<<z<<endl;(was used by me for debugging)
current->val=z;
}
}
return root;
}
int height(struct node *root)
{
struct node *current=root;
int retl=0,retr=0,fet=0;
if(current->left!=NULL)retl=1+height(current->left);
if(current->right!=NULL)retr=1+height(current->right);
if(current->left==NULL&¤t->right==NULL)
{
return 0;
}
return fet=retl>retr?retl:retr;
}
int leafcnt(struct node *root)
{
struct node *current=root;
int leaf=0;
if(current->left!=NULL)leaf=leaf+leafcnt(current->left);
if(current->right!=NULL)leaf=leaf+leafcnt(current->right);
if(current->left==NULL&¤t->right==NULL)
{
return 1+leaf;
}
return leaf;
}
int degreetwo(struct node *root)
{
struct node *current=root;
int leaf=0;
static int degree=0;
if(current->left!=NULL&¤t->right!=NULL)degree++;
if(current->left!=NULL)leaf=degreetwo(current->left);
if(current->right!=NULL)leaf=degreetwo(current->right);
if(current->left==NULL&¤t->right==NULL)
{
return leaf;
}
return degree;
}
int nodecnt(struct node *root)
{
struct node *current=root;
int nodelf=0;
if(current->left!=NULL)nodelf=nodelf+1+nodecnt(current->left);
if(current->right!=NULL)nodelf=nodelf+1+nodecnt(current->right);
if(current->left==NULL&¤t->right==NULL)
{
return nodelf;
}
return nodelf;
}
int main()
{
int n,i;
cout<<"Enter size of BST : ";
cin>>n;
cout<<endl;
queue<int>v;
cout<<"Enter the elements of BST : ";
for(i=0;i<n;i++)
{
int z;
cin>>z;
v.push(z);
}
struct node *root=createBST(v);
cout<<endl;
cout<<"Inoder traversal : ";
inorder(root);
cout<<endl;
cout<<"Preoder traversal : ";
preorder(root);
cout<<endl;
insert(root,19);//just use a loop here if u want to create of up to ur wish
insert(root,23);
root=insert(root,24);
root=insert(root,18);
int h=height(root);
cout<<"\n Height of BST = "<<h<<endl;
int lf=leafcnt(root);
cout<<"\n Leaf of BST = "<<lf<<endl;
int dg=degreetwo(root);
cout<<" Number of node with degree two "<<dg<<endl;
int tot=1+nodecnt(root);//one is added here becoz we r missing the root nod
cout<<" Total Number of node in BST "<<tot<<endl;
cout<<"Inoder traversal : ";
inorder(root);
cout<<endl;
cout<<"Preoder traversal : ";
preorder(root);
return 0;
}