c - 具有多个子节点和两个指向左右的节点的二叉搜索树

标签 c data-structures binary-search-tree

我想用 C 编程语言制作一个如下图所示的二叉树。

制作具有两个节点的二叉树的结构是这样的-

struct node {
    int data;
    struct node* left;
    struct node* right;
};

但是要制作一个有多个 child 的树,结构每次都需要改变,那么有什么办法可以制作一个每次都改变的结构吗?

enter image description here

最佳答案

你可以这样定义一个节点:

struct node
{
    int data;
    node *firstChild;
    node *nextSibling;
};

然后,

  1. 第一个 child 以这种方式访问​​:myNode->firstChild

  2. 第二个 child :myNode->firstChild->nextSibling

  3. 第 N 个 child :myNode->firstChild->nextSibling->...->nextSibling

这将涉及在遍历子节点时进行空检查和迭代。

关于c - 具有多个子节点和两个指向左右的节点的二叉搜索树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46988202/

相关文章:

data-structures - 什么时候以及为什么要在 CL 中使用哈希表而不是 a-lists?

java - 将字符串添加到 BST 时出错

python - 我当前的二叉搜索树实现有什么问题?最后打印树只打印根

c - 关于使用数据类型 int64_t 在 C 中进行十六进制乘法的问题

c - 分配 - 指针到指针

c - getchar() 调用后 printf 打印额外的 "D"

python - 属性错误: 'NoneType' object has no attribute height in BST pythons height

c - 如何从递归查看的列表中使用sprintf保存字符串

data-structures - 如何在 Go 中实现队列?

java - 如何在java中的列表结构末尾追加一个元素?