-
Notifications
You must be signed in to change notification settings - Fork 18
/
friendsweare
132 lines (101 loc) · 2.48 KB
/
friendsweare
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
#include<stdio.h>
#include<stdlib.h>
struct node {
struct node *left;
int info;
struct node *right;
};
struct node *newNode(int data){
struct node *temp = (struct node *)malloc(sizeof(struct node ));
temp->info = data;
temp->left = NULL;
temp->right = NULL;
return temp;
}
struct node *createTree(){
int data;
char ch;
printf("\n Enter data : ");
scanf("%d",&data);
struct node *root = newNode(data);
fflush(stdin);
printf("\n do you want to enter left child of %d ",data);
scanf("%c",&ch);
if(ch=='Y'||ch=='y')
root->left = createTree();
else
root->left = NULL;
fflush(stdin);
printf("\n do you want to enter right child of %d ",data);
scanf("%c",&ch);
if(ch=='Y'||ch=='y')
root->right = createTree();
else
root->right = NULL;
return root;
}
int front = -1;
int back = -1;
void enqueue(struct node q[],struct node *ptr,int size){
front++;
if(front==size)
{
printf(" \n the queue is full ");
}
else
{
q[front] = *ptr;
}
}
struct node *dequeue(struct node q[],int size){
if(back==front)
{
printf("\n queue is empty ");
return NULL;
}
else
{
back++;
struct node *temp = (struct node *)malloc(sizeof(struct node ));
*temp = q[back];
return temp;
}
}
int isEmpty(int size){
if(front==back || front==size)
return 1;
else
return 0;
}
int maxSumofLevel(struct node *root,int size,int max){
struct node *Queue = (struct node *)malloc(sizeof(struct node *));
int count=0;
enqueue(Queue,root,size);
max = root->info;
while(!isEmpty(size)){
count = 0;
struct node *temp = (struct node *)malloc(sizeof(struct node ));
temp = dequeue(Queue,size);
if(temp->left!=NULL){
count += temp->left->info;
enqueue(Queue,temp->left,size);
}
if(temp->right!=NULL){
enqueue(Queue,temp->right,size);
count += temp->right->info;
}
if(count > max )
max = count ;
}
return max;
}
int main(){
struct node *root = (struct node *)malloc(sizeof(struct node ));
root = createTree();
int size;
printf("\n enter total no. of nodes ");
scanf("%d",&size);
int max = 0;
printf(" \n maximum sum of any level in BT is %d ", maxSumofLevel(root,size,max));
return 0;
}