Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用C将数组转换为二叉树?_C_Arrays_Binary Tree - Fatal编程技术网

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

使用C将数组转换为二叉树?,c,arrays,binary-tree,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;

我正在尝试使用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;    
}  
typedef结构btree
{        
int值;
结构树*左;
结构树*右;
}b树;
无效插入(b树*t,整数*a,整数索引,整数n)
{    
t=(Btree*)malloc(sizeof(Btree));
如果(indexvalue=a[index];
插入(t->left,a,2*索引,n);
插入(t->右,a,2*索引+1,n);
}    
}    
int main(void){
int a[100],i;
b树*t;

对于(i=0;i,这里有几个问题:

  • 您的节点分配应该在
    insert
    中的条件块内,否则您将为空节点分配内存
  • 如果节点是叶节点且没有子节点,则应初始化指向
    NULL
    left
    right
    指针
  • 最重要的是,对树所做的更改将丢失。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;
}
#包括
#包括
类型定义结构树{
int值;
结构树*左;
结构树*右;
}b树;
无效插入(b树**t,int*a,int索引,int n)
{
如果(索引值=a[指数];
(*t)->左=空;
(*t)->right=NULL;
插入(&(*t)->左,a,2*索引+1,n);
插入(&(*t)->右,a,2*索引+2,n);
}
}
无效打印(Btree*t,整数级)
{
if(t){
打印(t->左,级别+1);
printf(“%*s->%d\n”,4*level,”,t->value);
打印(t->右侧,级别+1);
}
}
内部主(空)
{
inta[]={5,2,1,6,7,3,4};
b树*t;
插入(&t,a,0,7);
打印(t,0);
//TODO:清理节点使用的内存
返回0;
}

(我已经用硬编码数组替换了
scanf
内容。代码不会清理分配的内存,这是它真正应该做的。)

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

如果您当前的索引是
X
childs成为
2X+1
2X+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;
}

如果我想打印数组形成的树的按序遍历,该怎么办?是否可以不使用二叉树打印它?是的,无需执行任何操作,如果树已排序,只需打印数组:)这么简单..但我提供的解决方案只是想说可以用数组表示二叉树,,但使用节点学习对知识有好处:)(请参阅此处的存储二叉树的方法部分:)实际上树没有排序,树是由问题中所解释的数组组成的。我需要按顺序打印数组,即给定数组的顺序是{6,2,7,5,3,1,4}。我试过打印它,但没有形成树。但是我不知道怎么做。你能给我提供它的代码吗?
Parent: 5                                                                                                                                                            
Left Child: 2                                                                                                                                                        
Right Child: 1                                                                                                                                                       

Parent: 2                                                                                                                                                            
Left Child: 6                                                                                                                                                        
Right Child: 7                                                                                                                                                       

Parent: 1                                                                                                                                                            
Left Child: 3                                                                                                                                                        
Right Child: 4