使用数组实现二叉树

时间:2019-04-22 11:56:16

标签: c arrays tree binary-tree abstract-data-type

在硬件中,我被要求使用指针来实现二叉树,然后使用bt的数组实现。问题是,尽管我知道怎么做,但它们必须共享相同的主文件。我的意思是,数组实现将使用与指针实现完全相同的代码。这意味着当我引用insertTree(tree,tree-> left)必须也适用于该数组时,.i完全丢失了。 我的节点是:

    Typedef  struct BTNode{
     itemtype data;
      Struct BTNode * left;
     Struct BTNode * left;
    }BTNode;

1 个答案:

答案 0 :(得分:1)

在“标准”情况下, malloc 的结果支持一个新单元格,当它变得无用时,您 free

使用数组可以是具有 BTNode 的数组,而不是 malloc 一个新的单元格,您可以在该数组中获得一个免费条目。因为可以先后顺序获取/释放单元格,所以您也可以链接空闲单元格,因此,当释放单元格时,会将其重新引入与数组关联的空闲列表中

因此只有 malloc / free 调用必须修改才能使用或不使用数组


注意:

Typedef  struct BTNode{
     itemtype data;
      Struct BTNode * left;
     Struct BTNode * left;
    }BTNode;

你的意思

typedef  struct BTNode{
   itemtype data;
   struct BTNode * left;
   struct BTNode * right;
} BTNode;