【问题标题】:How to get entire contents of binary tree in array form?如何以数组形式获取二叉树的全部内容?
【发布时间】:2013-10-25 11:22:56
【问题描述】:

我有一个表示二叉树的 C 结构:

struct btree {
    char *word; 
    int frequency; 
    struct btree *left; 
    struct btree *right; 
}; 

我想创建一个函数btree_list(struct btree*),它返回传递给它的二叉树中所有btree 对象的数组。顺序无所谓。

以下是此功能如何工作的示例:

struct btree *T = populate_with_random_values(); 
struct btree *entries = (struct btree*) malloc(sizeof(struct btree) * btree_size(T));

entries = btree_list(T);

while (*entries != NULL) {
    printf("There are %d occurences of the word %s", entries->frequency, entries->word); 
    entries++; 
}

对于entriesE->leftE->right 中的每个元素E,也应设置为NULL,因为它们在技术上并未被使用。我将如何实施?

【问题讨论】:

  • 所以你知道,两行malloc entries 数组,并立即在下一行泄漏所有内存,这将覆盖刚刚收到的地址btree_list 的返回值.所以我希望它不会那样那样工作。

标签: c algorithm binary-tree


【解决方案1】:

这可能是数组:

typedef struct {
    struct btree **data;
    size_t count;
} t_tbl;

t_tbl *tbl_create(size_t count)
{
    t_tbl *new = NULL;

    if (count > 0) {
        new = malloc(sizeof(t_tbl));
        new->data = malloc(count * sizeof(struct btree *));
        new->count = 0;
    }
    return new;
}

void tbl_destroy(t_tbl *table)
{
    if (table) {
        free(table->data);
        free(table);
    }
}

这可能是过程:

void btree_populate_array(const t_node *root, t_tbl *table)
{
    if (root->left) btree_populate_array(root->left, table);
    table->data[table->count++] = root;
    if (root->right) btree_populate_array(root->right, table);
}

if (root) {
    t_tbl *table = tbl_create(btree_size);
    btree_populate_array(root, table);
    /* Do stuff with array */
    tbl_destroy(table);
}

如果你不知道btree的大小,你必须检查malloc

void btree_count(const t_node *root, size_t *count)
{
    if (root->left) btree_count(root->left, count);
    (*count)++;
    if (root->right) btree_count(root->right, count);
}

size_t btree_size = 0;

if (root) {
    btree_count(root, &btree_size);
}

【讨论】:

    【解决方案2】:

    与其返回数组,不如只传递它的基地址以使您的生活更轻松并返回数组的计数:

    int TraverseTree(int arr[],btreenode *root, int depth)
    {
        static int count = 0;
        if (root)
        {
            count++;
            TraverseTree(arr,root->right,depth+1);
            arr[depth]=root->data;
            TraverseTree(arr,root->left, depth+1);
        }
        return count;
    }
    

    【讨论】:

      【解决方案3】:

      嗯,你想要一个前序、中序还是后序遍历?

      这是一个伪代码的预购示例:(感谢Wikipedia

      iterativePreorder(node)
        parentStack = empty stack
        while not parentStack.isEmpty() or node ≠ null
          if node ≠ null then
            visit(node)
            if node.right ≠ null then
              parentStack.push(node.right)
            node = node.left
          else
            node = parentStack.pop()
      

      您必须稍微调整一下才能让它返回节点列表,但是遍历树的想法就在那里。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-10-08
        • 1970-01-01
        • 2022-07-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-28
        相关资源
        最近更新 更多