使用C将数组转换为二叉树?

时间:2015-03-06 05:44:57

标签: c arrays binary-tree

我正在尝试使用C语言将整数数组转换为二进制树。

例如,对于数组a[10]={5,2,1,6,7,3,4},二叉树应该看起来像

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

我尝试使用以下代码进行转换

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;    
}  

有人可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

这里有几个问题:

  • 您的节点分配应该位于insert中的条件块内,否则您将为空节点分配内存。
  • 如果节点是叶节点且没有子节点,则应将leftright指针初始化为NULL
  • 最重要的。您对树的更改将丢失。 poimter变量是insert的当前实例的本地变量(您可以递归调用)并且不会超越早期实例或main。将指针传递给节点指针。
  • 考虑始终使用零基指数;它们是C的标准。

所以,这是它应该如何运作的:

#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;
}

(我用硬编码的数组替换了scanf内容。代码不会清理分配的内存,它确实应该这样做。)

答案 1 :(得分:0)

可能你只需要输出数组以匹配树之类的视图。在这种情况下,您不需要使用节点创建二叉树,而只需使用具有适当索引的数组。

如果您当前的索引为X,则孩子会成为2X+12X+2。可能这就是你真正想要的。

参见示例:

#include <stdio.h>

int main()
{

    int a[7]={5,2,1,6,7,3,4}; // <= A hard coded array

    int n=0;

    // Getting the unsorted Tree output. 
    //  sizeof(a)/sizeof(int) - used to get the array length

    while(n < (sizeof(a)/sizeof(int))/2){
        printf("Parent: %d\n",a[n]);  // <= parent node
        printf("Left Child: %d\n",a[2*n +1]); // <= left Child
        printf("Right Child: %d\n",a[2*n +2]); // <= right Child

        printf("\n");
        n++;
    }

    return 0;
}

输出:

Parent: 5                                                                                                                                                            
Left Child: 2                                                                                                                                                        
Right Child: 1                                                                                                                                                       

Parent: 2                                                                                                                                                            
Left Child: 6                                                                                                                                                        
Right Child: 7                                                                                                                                                       

Parent: 1                                                                                                                                                            
Left Child: 3                                                                                                                                                        
Right Child: 4  
相关问题