-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathTree_Counting1DegreeNode.cpp
More file actions
84 lines (81 loc) · 1.65 KB
/
Tree_Counting1DegreeNode.cpp
File metadata and controls
84 lines (81 loc) · 1.65 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
#include<bits/stdc++.h>
using namespace std;
struct node{
node* lchild;
int data;
node* rchild;
};
struct Node{
node* data;
Node* next;
};
Node* front=NULL,*rare=NULL;
node* root=NULL;
void enqueue(node* x){
Node* p=NULL;
p=new Node;
if(p){
p->data=x;
p->next=NULL;
if(front==NULL){
front=rare=p;
}
else{
rare->next=p;
rare=p;
}
}
else{cout<<"Heap Overflow";}
}
node* dequeue(){
node* value;
value=front->data;
front=front->next;
return value;
}
//In similar fashion we can count total ,leaf,any degree node by just changing a little in the function
int count1degree(node* p){
if(p!=NULL){
int x=count1degree(p->lchild);
int y=count1degree(p->rchild);
if((p->lchild!=NULL)^(p->rchild!=NULL)){
return x+y+1;
}
return x+y;
}
return 0;
}
int main(){
cout<<"Input the root data : ";
root=new node;
cin>>root->data;
root->lchild=root->rchild=0;
enqueue(root);
while(front){
node* p;
p=dequeue();
cout<<"Input left child : ";
int x;
cin>>x;
if(x!=-1){
node* t;
t=new node;
t->data=x;
t->lchild=t->rchild=NULL;
p->lchild=t;
enqueue(t);
}
cout<<"Input right child : ";
cin>>x;
if(x!=-1){
node* t;
t=new node;
t->data=x;
t->lchild=t->rchild=NULL;
p->rchild=t;
enqueue(t);
}
}
cout<<count1degree(root)<<endl;
return 0;
}