-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarytreeusingarray.c
More file actions
133 lines (123 loc) · 3.07 KB
/
Binarytreeusingarray.c
File metadata and controls
133 lines (123 loc) · 3.07 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
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
#include <stdio.h>
#define MAXSIZE 20
#define left 2*j
#define right 2*j+1
int Binary_Tree[MAXSIZE],j=1,num,num1;
int left_element,right_element,n,count=0,count1=0;
int root,parent,pos,left_child,right_child;
void leftnode(int left_element){
if(left_element==0){
printf("Inside func %d",j);
count=count+3;
int k=count;
Binary_Tree[k*2]=0;
Binary_Tree[k*2+1]=0;
Binary_Tree[k]=0;
}
else if(count!=n && left_element!=0){
Binary_Tree[left]=left_element;
count++;
}
else{printf("\nCannot be added\n");}
}
void rightnode(int right_element){
if(right_element==0){
printf("Inside func %d",j);
count=count+3;
int k=count;
Binary_Tree[k*2]=0;
Binary_Tree[k*2+1]=0;
Binary_Tree[k]=0;
}
else if(count!=n && right_element!=0){
Binary_Tree[right]=right_element;
count++;
}
else{printf("\nCannot be added\n");}
}
void display(){
j=1;
int k=0;
printf("The Binary tree\n");
while(count>=0){
printf("for j=%d\n",j);
if(Binary_Tree[j]==0){
count--;
printf("No node\n");
j++;
continue;
//j++;
//continue;
}
else{
count--;
printf("root=%d\n",Binary_Tree[j]);
}
if(Binary_Tree[left]==0){
count--;
printf("No Left Node\n");
}
else{
count--;
printf("Child of %d = %d\n",Binary_Tree[j],Binary_Tree[left]);
}
if(Binary_Tree[right]==0){
count--;
printf("No right node\n");
}
else{
count--;
printf("Child of %d = %d\n",Binary_Tree[j],Binary_Tree[right]);
}
j++;
}
}
int main(){
int k=0;
printf("enter the number of nodes\n");
scanf("%d",&n);
int i=0;
printf("Enter the root node:\n");
scanf("%d",&Binary_Tree[j]);
count++;
while(count<n){
k++;
printf("For i=%d\n",k);
printf("Enter the left node(if no node, then enter 0)\n");
scanf("%d",&left_element);
if(Binary_Tree[j]==0){
j++;
}
leftnode(left_element);
printf("Enter the right node(if no node, then enter 0)\n");
scanf("%d",&right_element);
if(Binary_Tree[j]==0){
j++;
}
rightnode(right_element);
j++;
}
display();
printf("Enter the pos to find the parent and children");
scanf("%d",&pos);
num1=Binary_Tree[pos];
if(pos%2==0)
parent=pos/2;
else{
parent=(pos-1)/2;
}
right_child=2*pos+1;
left_child=2*pos;
printf("Parent of the given node %d=%d\n",Binary_Tree[pos],Binary_Tree[parent]);
if(left_child==0){
printf("No left child\n");
}
else
printf("Left Child== %d\n",Binary_Tree[left_child]);
if(right_child==0){
printf("No right child\n");
}
else
printf("right Child== %d",Binary_Tree[right_child]);
return 0;
}