简体   繁体   English

迭代二叉搜索树插入C语言

[英]Iterative Binary Search Tree Insert in C

This seems to be a simple question, and I have looked at another thread and it seems I am doing the same thing, however without results. 这似乎是一个简单的问题,我看了另一个线程,似乎我在做同样的事情,但是没有结果。

This is my code to iteratively insert into a binary search tree, along with the structure and how to create a new node: 这是我的代码,迭代地插入二叉搜索树,以及结构和如何创建一个新节点:

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

node *create_node(int data) {
    node *n = calloc(1, sizeof(node));

    n->data = data;
    return n;
}

node *BST_insert_iterative(node *root, int data) {
    node *temp = root;

    if (root == NULL)
        return create_node(data);

    while (temp != NULL) {
        if (data > temp->data)
            temp = temp->right;
        else
            temp = temp->left;
    }

    temp = create_node(data);

    return root;
}

When I print the tree I only receive the first node: 当我打印树时,我只收到第一个节点:

Inserting 8  into BST...
Inserting 50     into BST...
Inserting 74     into BST...
Inserting 59     into BST...
Inserting 31     into BST...
Inserting 73     into BST...
Inserting 45     into BST...
Inserting 79     into BST...
Inserting 24     into BST...
Inserting 10     into BST...
In order traversal
8
Height of tree: 0

However, using the recursive insert function: 但是,使用递归插入函数:

node *BST_insert(node *root, int data) {
    if (root == NULL)
        return create_node(data);

    if (root->data >= data)
        root->left = BST_insert(root->left, data);

    else (root->data < data)
        root->right = BST_insert(root->right, data);

    return root;
}

it works just fine and I get: 它工作得很好,我得到:

Inserting 8  into BST...
Inserting 50     into BST...
Inserting 74     into BST...
Inserting 59     into BST...
Inserting 31     into BST...
Inserting 73     into BST...
Inserting 45     into BST...
Inserting 79     into BST...
Inserting 24     into BST...
Inserting 10     into BST...
In order traversal
8
10
24
31
45
50
59
73
74
79
Height of tree: 4 

In your insert function, you do not store the new node into the tree. 在插入函数中,不将新节点存储到树中。 You merely store in to a local variable temp and always return root . 您只需存储到局部变量temp并始终返回root

You must keep a pointer to the link to be updated so the new node is inserted into the tree, or at the root of the tree. 您必须保留指向要更新的链接的指针,以便将新节点插入树中或树的根目录中。 Here is a modified version: 这是一个修改版本:

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

node *create_node(int data) {
    node *n = calloc(1, sizeof(node));
    n->data = data;
    n->left = n->right = NULL;
    return n;
}

node *BST_insert_iterative(node *root, int data) {
    node **pp = &root;

    while (*pp != NULL) {
        if (data > (*pp)->data)
            pp = &(*pp)->right;
        else
            pp = &(*pp)->left;
    }
    *pp = create_node(data);
    return root;
}

Notes: 笔记:

  • There is no special case for an empty tree. 空树没有特殊情况。
  • Be aware that this approach will not be sufficient to keep the tree balanced. 请注意,这种方法不足以保持树的平衡。

Note also that your recursive function has a syntax error in what looks like a redundant test. 另请注意,递归函数在看起来像冗余测试时会出现语法错误。 You should simplify it this way: 你应该这样简化它:

node *BST_insert(node *root, int data) {
    if (root == NULL)
        return create_node(data);

    if (root->data >= data) {
        root->left = BST_insert(root->left, data);
    } else {
        root->right = BST_insert(root->right, data);
    }
    return root;
}

Chqrlie's answer worked, but I wanted to do it without using a double pointer. Chqrlie的回答有效,但我想在不使用双指针的情况下完成。

I was able to do it after realizing my root was not connected to the newly created node. 在意识到我的root没有连接到新创建的节点后,我能够做到这一点。

Here is that solution: 这是解决方案:

node *BST_insert_iterative(node *root, int data)
{
    node *temp = root;
    int condition = 1;

    if (root == NULL)
        return create_node(data);

    while (condition)
    {
        if (data > temp->data)
        {
            if (temp->right == NULL)
            {
                temp->right = create_node(data);
                condition = 0;
            }
            else
                temp = temp->right;
        }
        else
        {
            if (temp->left == NULL)
            {
                temp->left = create_node(data);
                condition = 0;
            }
            else
                temp = temp->left;
        }

    }

    return root;
}

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

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