简体   繁体   English

如何从右到左在C中的树中插入节点?

[英]How to insert nodes in tree in C from right to left?

Now, I understand that code below works only for root and its children, but I don't know how to expand it. 现在,我知道下面的代码仅适用于root及其子级,但是我不知道如何扩展它。 Every node must have children before passing on "grandchildren". 在传递“孙代”之前,每个节点都必须有子代。 Thank you. 谢谢。

void insert_node(IndexTree **root, Node *node) {
    IndexTree *temp = (IndexTree*)malloc(sizeof(IndexTree));
    memcpy(&temp->value.cs, node, sizeof(Node));
    temp->left = NULL;
    temp->right = NULL;
    temp->tip=1;

if ((*root) == NULL) {
    *root = temp;
    (*root)->left = NULL;
    (*root)->right = NULL;
}
else {
    while (1) {
        if ((*root)->right == NULL) {
            (*root)->right = temp;
            break;
        }
        else if ((*root)->left == NULL) {
            (*root)->left = temp;
            break;
        }
    }
}

Use recursive functions. 使用递归函数。

Trees are recursive data types ( https://en.wikipedia.org/wiki/Recursive_data_type ). 树是递归数据类型( https://en.wikipedia.org/wiki/Recursive_data_type )。 In them, every node is the root of its own tree. 在它们中,每个节点都是其自己树的根。 Trying to work with them using nested if s and while s is simply going to limit you on the depth of the tree. 试图与他们使用嵌套的工作if S和while s的只是要限制你的树的深度。

Consider the following function: void print_tree(IndexTree* root) . 考虑以下函数: void print_tree(IndexTree* root) An implementation that goes over all values of the trees does the following: 遍历树的所有值的实现将执行以下操作:

void print_tree(IndexTree* root)
{
      if (root == NULL) return; // do NOT try to display a non-existent tree

      print_tree(root->right);
      printf("%d\n", root->tip);
      print_tree(root->left);
}

The function calls itself, which is a perfectly legal move, in order to ensure that you can parse an (almost) arbitrarily deep tree. 该函数调用自身,这是完全合法的举动,以确保您可以解析(几乎)任意深的树。 Beware, however, of infinite recursion! 但是要当心无限递归! If your tree has cycles (and is therefore not a tree), or if you forget to include an exit condition, you will get an error called... a Stack Overflow! 如果您的树有循环(因此不是树),或者如果您忘记包含退出条件,则会出现一个错误,即...堆栈溢出! Your program will effectively try to add infinite function calls on the stack, which your OS will almost certainly dislike. 您的程序将有效地尝试在堆栈上添加无限函数调用,您的OS几乎肯定会不喜欢它们。

As for inserting, the solution itself is similar to that of printing the tree: 至于插入,解决方案本身类似于打印树的解决方案:

void insert_value(IndexTree* root, int v)
{
    if (v > root->tip) {
        if (root->right != NULL) {
            insert_value(root->right, v);
        } else {
            // create node at root->right
        }
    } else {
        // same as above except with root->left
    }
}

It may be an interesting programming question to create a Complete Binary Tree using linked representation. 使用链接表示创建完整的二叉树可能是一个有趣的编程问题。 Here Linked mean a non-array representation where left and right pointers(or references) are used to refer left and right children respectively. 此处的链接表示一种非数组表示形式,其中左右指针(或引用)分别用于引用左右子元素。 How to write an insert function that always adds a new node in the last level and at the leftmost available position? 如何编写一个总是在最后一级和最左边可用位置添加新节点的插入函数? To create a linked complete binary tree, we need to keep track of the nodes in a level order fashion such that the next node to be inserted lies in the leftmost position. 要创建链接的完整二叉树,我们需要以级别顺序的方式跟踪节点,以使要插入的下一个节点位于最左侧。 A queue data structure can be used to keep track of the inserted nodes. 队列数据结构可用于跟踪插入的节点。
Following are steps to insert a new node in Complete Binary Tree. 以下是在“完整二进制树”中插入新节点的步骤。 (Right sckewed) (右偏斜)
1. If the tree is empty, initialize the root with new node.

2. Else, get the front node of the queue.

……. if the right child of this front node doesn't exist, set the right child as the new node. //as per your case
…….else If the left child of this front node doesn't exist, set the left child as the new node.

3. If the front node has both the left child and right child, Dequeue() it.

4. Enqueue() the new node . 4. Enqueue() the new node

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM