【问题标题】:Error in code when searching through the right subtree in my binary search tree在我的二叉搜索树中搜索右子树时出现代码错误
【发布时间】:2018-10-25 11:28:43
【问题描述】:

在我在 Uni 的一门课程中,我们正在创建二叉搜索树并插入数据并进行查找。我的代码在我脑海中是有意义的,因此我无法在任何地方找到错误。我花了很长时间试图找到错误,但在任何地方都找不到。唯一可能导致错误的是预编译的头文件在我开始时不起作用,所以我将它们从我的项目中删除。仅当我尝试使用 BST.Lookup 并选择右侧子树上的键时才会发生错误。 这是我的主要 cpp 文件:

// BinarySearchTrees.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include "BST.h"
#include <iostream>
#include <fstream>
#include <string>

void ReadFile(BST &Bst)
{
    int iKey;
    std::string Key;
    std::string Data;
    std::ifstream testFile("Test.txt");
    if (testFile.is_open())
    {
        while (!testFile.eof())
        {
            getline(testFile, Key, ' ');
            getline(testFile, Data);
            iKey = stoi(Key);
            Bst.Insert(iKey, Data);
        }
    }
}

int main()
{
    std::string Option;
    int Choice;
    BST BST;
    //ReadFile(BST);
    BST.Insert(6, "Oscar");
    BST.Insert(20, "Ben");
    BST.Insert(99, "James");
    BST.Insert(1, "Alex");

    while (Option != "exit")
    {
        std::cout << "If you wish to Lookup a Node, Insert value to find. Enter 'exit' to close" << std::endl;
        getline(std::cin, Option);
        if (Option == "exit")
            break;
        else
        {
            Choice = stoi(Option);
            BST.Lookup(Choice);
        }
    }
    return 0;
}

我认为 readfile 代码可能不正确,但我不确定。 我的二叉搜索树类:

#include "BST.h"

struct BST::Node {
    Key key;
    Item item;
    Node* leftChild;
    Node* rightChild;
    Node(Key, Item);
};

void BST::Insert(Key inputKey, Item inputItem)
{
    Node* previousNode = nullptr;
    if (root == nullptr)
    {
        root = new Node(inputKey, inputItem);
    }
    else
    {
        InsertRec(inputKey, inputItem, root, previousNode);
    }
}

void BST::InsertRec(Key inputKey, Item inputItem, Node* & Current, Node* & previousNode)
{
    if (Current != nullptr)
    {
        previousNode = Current;
    }

    bool isLeft = false;

    if (!isLeaf(Current))
    {
        if (inputKey > Current->key)
        {
            isLeft = false;
            InsertRec(inputKey, inputItem, Current->rightChild, previousNode);
        }
        else if (inputKey < Current->key)
        {
            isLeft = true;
            InsertRec(inputKey, inputItem, Current->leftChild, previousNode);
        }
        else
        {
            Current->item = inputItem;
        }
    }
    else
    {
        Current = new Node(inputKey, inputItem);
        if (isLeft)
        {
            previousNode->leftChild = Current;
        }
        else
        {
            previousNode->rightChild = Current;
        }

    }
}

BST::Item* BST::Lookup(Key soughtKey)
{
    Item* Item = LookupRec(soughtKey, root);
    std::string Display = /*std::to_string(soughtKey) + ": " + */ *Item;
    std::cout << Display << std::endl;
    return Item;

}

BST::Item* BST::LookupRec(Key soughtKey, Node* currentNode)
{
    if (!isLeaf(currentNode))
    {
        if ((currentNode->key > soughtKey))
        {
            LookupRec(soughtKey, currentNode->leftChild);
        }
        else if ((currentNode->key < soughtKey))
        {
            LookupRec(soughtKey, currentNode->rightChild);
        }
        else
        {
            return &currentNode->item;
        }
    }

    else
    {
        return nullptr;

    }
}

bool BST::isLeaf(Node* n)
{
    if (nullptr == n)
    {
        return true;
    }

    else
    {
        return false;
    }
}

BST::BST()
{
}

BST::Node::Node(Key K, Item I)
{
    key = K;
    item = I;
    leftChild = nullptr;
    rightChild = nullptr;
}

最后是二叉搜索树的头文件:

#pragma once
#include "iostream"
#include "string"

class BST
{
    public:
        using Key = int;
        using Item = std::string;
        void Insert(Key, Item);
        Item* Lookup(Key);
        BST();

    private:
        struct Node;
        Node* root = nullptr;
        static bool isLeaf(Node*);
        static Item* LookupRec(Key, Node*);
        static void InsertRec(Key, Item, Node* &, Node* &);


};

任何帮助将不胜感激。我在这个问题上卡了太久,如果不先解决这个问题,我就无法进步。

Test.txt 文件充满了读取和输入的键和项目,就像我在主函数开始时手动执行的操作一样,所以我认为错误不是文件数据。

提前致谢

【问题讨论】:

  • 错误是什么?你能把你看到的贴上去吗?
  • i.imgur.com/c6ZlcHD.png 这是错误的屏幕截图。它发生在我查找 99 或 20 或任何我认为落在右子树上的数字时

标签: c++11 binary-search-tree


【解决方案1】:

更新:终于找到了错误。问题出在我的 InsertRec 函数中的 bool isLeft 上。由于递归,布尔值始终为假,因此更改了代码以比较 previousNode->Key 与 Current->Key 以确定孩子是向左还是向右

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-14
    • 1970-01-01
    • 2023-03-08
    • 2019-04-15
    • 2021-08-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多