Hello friends!!!!!
Implementing Binary tree using array
// Implementation of Binary Tree Using an array
#include <stdio.h>
#define MAX 100
void initialize(int A[]){
for(int i = 0 ; i < MAX ; i++){
A[i] = -1;
}
}
void Create_root(int A[],int value){
if(A[0] != -1){
printf("Root value already exists\n");
}
else{
A[0] = value;
printf("%d is added as root\n", value);
}
}
void Insert_Right(int A[],int value , int Parent_Index){
int index = Parent_Index * 2 + 2;
if(A[Parent_Index] == -1){
printf("The Parent value doesn't exist.\n");
}
else if(index >= MAX){
printf("Sorry guys, the right index if out of bonds.\n");
}
else{
A[index] = value;
printf("Insertion successful\n");
}
}
void Insert_Left(int A[],int value , int Parent_Index){
int index = Parent_Index * 2 + 1;
if(A[Parent_Index] == -1){
printf("The Parent value doesn't exist.\n");
}
else if(index >= MAX){
printf("Sorry guys, the left index if out of bonds.\n");
}
else{
A[index] = value;
printf("Insertion is successful.\n");
}
}
void display_Tree(int A[]){
for(int i = 0 ; i < MAX ; i++){
if(A[i] != -1){
printf("%d " , A[i]);
}
}
}
int main() {
int Tree[100];
initialize(Tree);
Create_root(Tree,40);
Insert_Left(Tree,50,0);
Insert_Right(Tree,49,0);
Insert_Right(Tree,35,1);
Insert_Left(Tree,23,1);
display_Tree(Tree);
return 0;
}