简体   繁体   English

C二叉搜索树没有为左节点和右节点分配值?

[英]C Binary search tree not assigning values to left and right nodes?

I seem to be having an issue where when I am assigning values into my binary search tree, and the root will take in the first value entered, but then after that nothing else is entered. 我似乎遇到一个问题,当我向二进制搜索树中分配值时,根将接受输入的第一个值,但之后再不输入其他任何值。 If I go and directly take a look at root -> left or root -> right it just returns with null. 如果我直接查看root-> left或root-> right,它只会返回null。 I've stared at this so long I'm at my wits end. 我凝视了这么久,以至于不知所措。 I'm sure its something really basic wrong with my recursion, but I just can't see it. 我确定递归确实存在一些基本错误,但是我看不到它。 I'd really appreciate any help seeing where I went wrong here. 看到我在哪里出错了,我将不胜感激。

#include <stdio.h>
#include <stdlib.h>
#include "Bst.h"


int main(int argc, char** argv) {
    int value;
    TreeNode* root = NULL;
    printf ("Enter an integer\n");
    scanf ("%d", &value);
    while (value > 0) {
        root = insert (value, root);
        printf ("Enter an integer\n");
        scanf ("%d", &value);    
    }

    printf("The inorder traversal of the tree\n");
    inOrder(root);
    printf("\n");

    printf("The preorder traversal of the tree\n");
    preOrder(root);
    printf("\n");
    return (EXIT_SUCCESS);
}



TreeNode* insert(int newValue, TreeNode* root) {
    TreeNode* temp = NULL;

    //Sets a value to the root
    if (root == NULL) {
        temp = (TreeNode*)malloc(sizeof(TreeNode));
        temp -> value = newValue;
        temp -> left = NULL;
        temp -> right = NULL;
        return temp;
    }

    //Will put a larger value to the right within the tree
    else if (newValue > (root -> value)) {
        temp = insert(newValue, (root -> right));
    }

    //Will put a smaller value to the left
    else {
        temp = insert (newValue, (root -> left));
    }
    return root;
}

void inOrder(TreeNode* root){
    if(root == NULL){
        return;
    }
    else {
        inOrder(root -> left);
        printf("%d", root -> value);
        printf(" ");
        inOrder(root -> right);
    }
    return;
}

void preOrder(TreeNode* root){
    if(root == NULL) {
        return;
    } else {
        preOrder(root -> right);
        printf("%d", root -> value);
        printf(" ");
        preOrder(root -> left);
    }
    return;
}

Change: 更改:

temp = insert(newValue, (root -> right));

to

root->right = insert(newValue, (root -> right));

Also change the left version the same way. 同样,以相同方式更改左侧版本。 Right now you are allocating the child nodes but never assigning them to the right or left pointers. 现在,您正在分配子节点,但从未将它们分配给右或左指针。 They are essentially getting thrown away. 他们本质上被扔掉了。

Pretty sure the error is that you assign the newly inserted right and left nodes to temp and not to root->right and root->left which leaves them dangling outside the tree. 可以肯定的是,错误是您将新插入的左右节点分配给temp而不是root->rightroot->left ,这会使它们悬在树外。

Change these lines: 更改这些行:

//Will put a larger value to the right within the tree
else if (newValue > (root -> value)){
    temp = insert(newValue, (root -> right));
}

//Will put a smaller value to the left
else{
    temp = insert (newValue, (root -> left));
}

To this: 对此:

//Will put a larger value to the right within the tree
    else if (newValue > (root -> value)) {
        root->right = insert(newValue, (root -> right));
    }

//Will put a smaller value to the left
    else {
        root->left = insert (newValue, (root -> left));
    }

This change makes your code work as expected when I tested it; 此更改使您的代码在我测试时可以按预期工作; both inOrder and preOrder gave correct results. inOrder和preOrder都给出了正确的结果。

See this Ideone example 请参阅此Ideone示例

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

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