// Binary tree can be represented as an array using the method
#include <stdio.h>
#include <malloc.h>
struct node
{
struct node *left;
char data;
struct node *right;
};
struct node* buildtree(int);
void inorder(struct node*);
char a[] = {'A','B','C','D','E','F','G','\0','H','\0'
,'\0','\0','\0','\0','\0','\0','\0','\0','\0','\0'};
int main()
{
struct node *root;
root = buildtree(0);
printf("output:\n");
inorder(root);
return 0;
}
struct node* buildtree(int index)
{
struct node *temp;
if (a[index] != '\0')
{
temp = (struct node*) malloc(sizeof(struct node*));
temp->left = buildtree(2 * index + 1);
temp->data = a[index];
temp->right = buildtree(2 * index + 2);
}
return temp;
}
void inorder(struct node *rNode)
{
if (rNode != NULL)
{
inorder(rNode->left);
printf("%c ",rNode->data);
inorder(rNode->right);
}
}
// output:
// D B H E A F C G