【问题标题】:C Binary search tree not assigning values to left and right nodes?C二叉搜索树没有为左右节点分配值?
【发布时间】:2015-02-24 08:24:56
【问题描述】:

我似乎有一个问题,当我将值分配到我的二叉搜索树中时,根将接受输入的第一个值,但之后没有输入任何其他值。如果我直接查看 root -> left 或 root -> right,它只会返回 null。我已经盯着这个看了这么久,我已经不知所措了。我确定我的递归确实存在一些基本错误,但我只是看不到它。我真的很感激任何帮助,看看我在哪里出错了。

#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;
}

【问题讨论】:

  • 您能否至少提供示例输入、您从代码中获得的输出以及预期输出?只说“这是我的代码,帮我调试一下”有点粗鲁。
  • 话虽如此,您可能想检查您的temp 变量包含什么以及递归调用insert 时数据的去向。
  • 查看您的 TreeNode 定义也可能会有所帮助。这是在您的Bst.h 中吗?

标签: c binary-search-tree


【解决方案1】:

变化:

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

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

也以相同的方式更改左侧版本。现在您正在分配子节点,但从不将它们分配给右指针或左指针。它们基本上被扔掉了。

【讨论】:

    【解决方案2】:

    很确定错误是您将新插入的左右节点分配给temp,而不是分配给root-&gt;rightroot-&gt;left,这会使它们悬空在树外。

    更改这些行:

    //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));
    }
    

    到这里:

    //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));
        }
    

    此更改使您的代码在我测试时按预期工作; inOrder 和 preOrder 都给出了正确的结果。

    See this Ideone example

    【讨论】:

    • 非常感谢!看不到这样的东西真是太傻了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多