You are on page 1of 5

S.No: 15 Exp. Name: To implement binary tree using Arrays.

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>

void updateElement(char *tree, int arraySize, chars, chart)


{ for (inti= 0; i < arraySize; i++) {
if (tree[i] s)
{ tree[i] = t;
}
}
}
int setRoot(int dataKey, char *tree) {
if (tree[0]!='\0')
printf("Root is already available\n");
else
tree[0]=dataKey;
return 0;

int addleft(char dataKey, int parentlndex, char *tree) { if(tree[parentlndex]=='\

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')

if (choice == 'R' 11 choice 'r') { 0


C
..c
printf("Enter datakey and parent index: "); (.)

scanf(" %c", &ch);


0
scanf("%d", &index); Q)

addRight(ch, index, tree); :


-

} else { aai C

printf("Enter datakey and parent index: "); 0


(.9
scanf(" %c", &ch); ro
scanf("%d", &index); E::::,
::.::
addleft(ch, index, tree); ·ro
} 0::

}
}
printf("Binary tree is:\n");
displayTree(tree, arraySize);
printf("\nEnter the data element you want to update: ");
chars, t;
scanf(" %c", &s); C")

scanf(" %c", &t);


0
updateElement(tree, arraySize, s, t); z
Q)
printf("Binary tree is:\n"); O')
C1l
0...
displayTree(tree, arraySize);

printf("\nEnter DeleteNode: ");


scanf(" %c", &delNode);
deleteNode(tree, delNode, arraySize);
printf("Tree after deletion\n");
displayTree(tree, arraySize);
return 0;
}

Execution Results - All test cases have succeeded!

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

Enter datakey and parent index: f 2


Binary tree is: C
a C b f C
Enter the data element you want to update: C
C
Binary tree is: a
a c b f a
Enter DeleteNode: a
Tree after deletion
f C b

>.
O')
0
0
C
..c
(.)

0
Q)

:a
-
C

ai
0
(.9
ro
E::::,
::.::
·ro
0::

You might also like