Converting an array into binary tree using C?

vineeth suhas

I am trying to convert an integer array into a binary tree using C.

For example for an array a[10]={5,2,1,6,7,3,4} , the Binary tree should look like

    5    
   / \  
  2   1    
 /\   /\  
6  7  3  4    

I tried to convert using the following code

typedef struct btree    
{        
    int value;    
    struct btree *left;    
    struct btree *right;    
}Btree;    
void insert(Btree *t,int *a,int index,int n)    
{    
    t=(Btree *)malloc(sizeof(Btree));    
    if(index<n)    
    {    
        t->value=a[index];    
        insert(t->left,a,2*index,n);    
        insert(t->right,a,2*index+1,n);    
    }    
}    
int main(void) {    
    int a[100],i;    
    Btree *t;    
    for(i=0;i<10;i++)    
        scanf("%d",&a[i]);    
    insert(t,a,0,i+1);    
    return 0;    
}  

Can someone help me out with this problem?

M Oehm

There are several issues here:

  • Your node allocation should go inside the conditional block in insert, otherwise you allocate memory for null nodes.
  • You should initialise the left and right pointers to NULL in case the node is a leaf node and has no children.
  • Most important. You changes to the tree are lost. The poimter variables are local to the current instance of insert (which you call recursively) and don't transcend to earlier instances or to main. Pass a pointer to the node pointer.
  • Consider using zero-base indices throughout; they are standard in C.

So, here's how it should work:

#include <stdlib.h>
#include <stdio.h>

typedef struct btree {
    int value;
    struct btree *left;
    struct btree *right;
} Btree;

void insert(Btree **t, int *a, int index, int n)
{

    if (index < n) {
        *t = malloc(sizeof(**t));

        (*t)->value = a[index];
        (*t)->left = NULL;
        (*t)->right = NULL;

        insert(&(*t)->left, a, 2 * index + 1, n);
        insert(&(*t)->right, a, 2 * index + 2, n);
    }
}

void print(Btree *t, int level)
{
    if (t) {
        print(t->left, level + 1);
        printf("%*s->%d\n", 4*level, "", t->value);
        print(t->right, level + 1);
    }
}

int main(void)
{
    int a[] = {5, 2, 1, 6, 7, 3, 4};
    Btree *t;

    insert(&t, a, 0, 7);
    print(t, 0);

    // TODO: Clean up memory used by nodes

    return 0;
}

(I've replaced the scanf stuff with a hard-coded array. The code does not clean up the allocated memory, which it really should.)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Convert binary tree to array in c

From Dev

Convert binary tree to array in c

From Dev

Converting an expression to binary tree

From Dev

Converting ordered array into Binary Tree : Runtime Error in the code

From Dev

C++ Binary Search Tree using Recursion

From Dev

C++ Binary Tree Using Vector

From Dev

Binary to char converting in c

From Dev

Storing a binary tree in an array

From Dev

Array placement of Binary Tree

From Dev

Creating an Array out of a string representing a binary tree using brackets in python

From Dev

store a binary search tree graph into an array using recursion and print it out

From Dev

Converting Binary tree to Doubly linked list

From Dev

Converting a list of nested lists in binary tree

From Dev

Using a Binary Tree

From Dev

Converting a Django binary field to an array

From Dev

Converting a hexadecimal array into Binary and then into Decimal

From Dev

Converting array of binary numbers to decimal

From Dev

Convert (un)sorted array to binary search tree in c

From Dev

How can build a Binary Search Tree from an array of integers in C?

From Dev

Initialize a Balanced Binary Search Tree from Array C++

From Dev

Binary Search Tree C

From Dev

Binary tree for strings c

From Dev

Binary tree for strings c

From Dev

C++ Binary Tree

From Dev

Binary tree traversal in c

From Dev

Converting Binary to text using JavaScript

From Dev

Converting Binary to text using JavaScript

From Dev

Converting binary to octal using strings

From Dev

Print binary tree in a pretty way using c++