【问题标题】:Converting an array into binary tree using C?使用C将数组转换为二叉树?
【发布时间】:2015-05-07 17:13:15
【问题描述】:

我正在尝试使用 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;    
}  

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

【问题讨论】:

    标签: c arrays binary-tree


    【解决方案1】:

    这里有几个问题:

    • 您的节点分配应该放在insert 的条件块内,否则您会为空节点分配内存。
    • 您应该将leftright 指针初始化为NULL,以防节点是叶节点并且没有子节点。
    • 最重要的。您对树所做的更改将丢失。指针变量对于insert(递归调用)的当前实例是本地的,不会转移到更早的实例或main。传递一个指向节点指针的指针。
    • 考虑始终使用零基索引;它们在 C 中是标准的。

    所以,它应该是这样工作的:

    #include <stdio.h>
    #include <stdlib.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 的东西。代码不会清理分配的内存,它确实应该这样做。)

    【讨论】:

      【解决方案2】:

      您可能只需要输出数组以匹配树状视图。在这种情况下,您不需要制作带有节点的二叉树,只需使用具有适当索引的数组即可。

      如果您当前的索引是 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  
      

      【讨论】:

      • 如果我想打印数组形成的树的中序遍历怎么办。不使用二叉树是否可以打印?
      • 是的,不需要做任何事情,如果对树进行排序,只需打印数组 :) 就这么简单。但我提供的解决方案只是说可以用数组,但是用节点学习对知识有好处:)(见这里,存储二叉树部分的方法:en.wikipedia.org/wiki/Binary_tree
      • 实际上树没有排序,树是由问题中解释的数组形成的。我需要以无序方式打印数组,即给定数组的顺序是 {6 ,2,7,5,3,1,4}。我试过在不形成树的情况下打印它。但我不知道该怎么做。你能给我它的代码吗?
      猜你喜欢
      • 2015-06-17
      • 2022-01-12
      • 2011-07-24
      • 1970-01-01
      • 2013-04-17
      • 2020-08-24
      • 1970-01-01
      • 2012-02-28
      • 2021-01-13
      相关资源
      最近更新 更多