Professional Documents
Culture Documents
Date:2023-12-14
Aim:
Write a C program to implement Binary tree using Array.
Note : Driver code is already provided for you to run the test cases. 0
z
Source Code: Q)
O')
C1l
0...
JreeMain.c
#include <math.h>
#include
<stdio.h> #include
<stdlib.h> #include
<string.h>
0')
printf("No parent found\
n"); else
tree[(parentlndex*2)+1]=dataKey;
return 0;
} >.
O')
0
0
int addRight(char dataKey, int parentlndex, char *tree) { {
if (tree[parentlndex]=='\0')
printf("No parent found\
n"); f
else
tree[(parentlndex*2)+2]=dataKey; o
return 0;
r
}
(
int displayTree(char *tree, int arraySize)
i
C
nt i=0;i<arraySize;i++) ..c
(.)
0
Q)
:a
-
C
ai
0
(.9
ro
E
::::,
::.::
·ro
0:::
{
if(tree[i]!='\0')
printf("%c ",tree[i]);
}
return 0;
N
0
} z
Q)
O')
C1l
0...
int deleteNode(char *tree, char dataKey, int arraySize) {
int k,l;
char lastNode;
for (int i=0;i<arraySize;i++)
{
if(tree[i]==dataKey)
k=i;
if(tree[i]!='\0')
l=i;
}
tree[k]=tree[l];
tree[l]='\0';
}
// Driver Code
int main() {
char *tree;
char ch, choice, delNode;
printf("Enter the number of nodes: ");
int n, index;
scanf("%d", &n);
int height= round(log2(n) + 1);
printf("height: %d\n", height);
int arraySize = pow(2, height) - 1;
tree= (char *)malloc(arraySize * sizeof(char));
memset(tree, '\0', sizeof(char *) * arraySize);
printf("Enter the data key in root node: ");
scanf(" %c", &ch);
setRoot(ch, tree);
if (n > 1) {
printf("Enter rest of the nodes in the binary tree: \
n''); for (inti= 1; i < n; i++) {
printf("Enter R or r for right and Lor 1 for left side insertion: ");
>.
scanf(" %c", &choice); 0
O')
} else { aai C
}
}
printf("Binary tree is:\n");
displayTree(tree, arraySize);
printf("\nEnter the data element you want to update: ");
chars, t;
scanf(" %c", &s); C")
Test Case - 1
User Output
Enter the number of nodes: 5
height: 3A
Enter the data key in root node: A
Enter rest of the nodes in the binary tree: r
Enter R or r for right and Lor 1 for left side insertion: r
Enter datakey and parent index: B 0
Enter R or r for right and Lor 1 for left side insertion: 1
Enter datakey and parent index: C 0
Enter R or r for right and Lor 1 for left side insertion: r
Enter datakey and parent index: D 1
Enter R or r for right and Lor 1 for left side insertion: 1
Enter datakey and parent index: E 2
Binary tree is: B Z
C B D E B Z
Enter the data element you want to update: B Z
Binary tree is: C
C Z D E C
>.
Enter DeleteNode: C O')
0
Tree after deletion 0
C
..c
E Z D (.)
0
Q)
:a
-
C
ai Test Case - 2
0
User Output (.9
ro
Enter the number of nodes: 6 E::::,
height: 4a ::.::
·r
Enter the data key in root node: a
o0::
Enter rest of the nodes in the binary tree: r
Enter R or r for right and Lor 1 for left side r
insertion:
Enter datakey and parent index: b 0
Enter R or r for right and Lor 1 for left side insertion: 1
Enter datakey and parent index: C 0
Enter R or r for right and Lor 1 for left side insertion: r
Enter datakey and parent index: e 2
s:t Enter R or r for right and Lor 1 for left side insertion: r
0
Enter datakey and parent index: f 3
z No parent found r
Q)
O')
Enter R or r for right and Lor 1 for left side insertion: r 0...
C1l
>.
O')
0
0
C
..c
(.)
0
Q)
:a
-
C
ai
0
(.9
ro
E::::,
::.::
·ro
0::