繁体   English   中英

C中的二进制搜索树实现-错误插入

[英]Binary search tree implementation in C - Inserting incorrectly

我正在尝试在C中实现二进制搜索树。我的插入方法无法正常工作。 其顺序打印为:

1 2 4 3 5 7 6 10

预订打印为:

5 3 2 1 4 7 10 6

订单打印为:

1 4 2 3 6 10 7 5

我试过摆弄小号和大号,并使用“左”和“右”字符数组来标记下一步的每个输入,但到目前为止我什么都没得到。 我将不胜感激。 非常感谢你!

    int main(int argc, const char * argv[]) {

        struct node* root1 = (struct node*)malloc(sizeof(struct node));
        root1->value = 5;
        root1->left = NULL;
        root1->right = NULL;
        root1->parent = NULL;
        insert(3, root1);
        insert(2, root1);
        insert(1, root1);
        insert(7, root1);
        insert(10, root1);
        insert(6, root1);
        insert(4, root1);
    }

    //Does an initial comparison to set up the helper function 'H' to actually insert the value.
    void insert(int val, struct node* rootNode) {
        if (val < (rootNode)->value) {
            insertH(val, rootNode, "left");
        } else if (val > (rootNode)->value) {
            insertH(val, rootNode, "right");
        }
    }

    //Inserts val into the BST in its proper location
    void insertH(int val, struct node* rootNode, char* helper) {
    if (!strcmp(helper, "left")) {
        if (rootNode->left == NULL) {
            rootNode->left = (struct node*)malloc(sizeof(struct node));
            rootNode->left->value = val;
            (rootNode->left)->parent = rootNode;
            rootNode->left->left = NULL;
            rootNode->left->right = NULL;
        } else {
            if (val < (rootNode)->value) {
                insertH(val, rootNode->left, "left");
            } else if (val > (rootNode)->value) {
                insertH(val, rootNode->left, "right");
            }
        }
    } else if (!strcmp(helper, "right")) {
        if (rootNode->right == NULL) {
            rootNode->right = (struct node*)malloc(sizeof(struct node));
            rootNode->right->value = val;
            (rootNode->right)->parent = rootNode;
            rootNode->right->left = NULL;
            rootNode->right->right = NULL;

        } else {
            if (val < (rootNode)->value) {
                insertH(val, rootNode->right, "left");
            } else if (val > (rootNode)->value) {
                insertH(val, rootNode->right, "right");
            }
        }
    }
}

在BST中,新密钥始终插入在叶子上。 我们从根开始搜索密钥,直到遇到叶节点。 找到叶节点后,将新节点添加为叶节点的子节点。
这是带有注释的代码,可帮助您了解其工作原理,希望对您有所帮助。

/* A utility function to insert a new node with given key in BST */
struct node* insert(struct node* node, int key)
{
    /* If the tree is empty, return a new node */
    if (node == NULL) return newNode(key);

    /* Otherwise, recur down the tree */
    if (key < node->key)
        node->left  = insert(node->left, key);
    else if (key > node->key)
        node->right = insert(node->right, key);   

    /* return the (unchanged) node pointer */
    return node;
}

暂无
暂无

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

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