【问题标题】:Binary Search Tree unchangeable header file二叉搜索树不可更改的头文件
【发布时间】:2018-04-05 10:40:47
【问题描述】:

我正在做一个关于二叉搜索树的作业。我们得到了一个头文件,我们不能修改它。

我有一个用于创建 BST 并存储初始值的函数、一个通过复制现有树创建 BST 的函数和一个插入 BST 的函数。

我正在努力找出问题所在,我在网上寻求帮助的任何地方总是在头文件中存在我无法做到的差异。任何帮助将不胜感激。

#include "stdafx.h"
#include "bst.h"

// Creating nodes  
Node* tree;
Node* treeNew;


// Creates an empty binary tree - 1
BinarySearchTree::BinarySearchTree()
{
    treeNew = nullptr;
}

// Creates a binary tree with an initial value to store - 2
BinarySearchTree::BinarySearchTree(std::string word)
{
    Node *tree = new Node;
    tree->left = nullptr;
    tree->right = nullptr;
    tree->data = word;
}

// Creates a binary tree by copying an existing tree - 3
BinarySearchTree::BinarySearchTree(const BinarySearchTree &rhs)
{
    if (tree != NULL) {

        Node *treeNew = new Node;
        treeNew->data = tree->data;

        Node *leftTreeNew = new Node;
        leftTreeNew = tree->left;

        Node *rightTreeNew = new Node;
        rightTreeNew = tree->right;
    }
    else {
        treeNew = NULL;
    }
}

// Adds a word to the tree
void BinarySearchTree::insert(std::string word)
{
    Node *nextNode = tree;
    Node *currentNode;


    while (nextNode != NULL) {
        currentNode = nextNode;

        if (word < currentNode->data) {
            nextNode = currentNode->left;
        } else {
            nextNode = currentNode->right;
        }
    } 
}

这是我的头文件

#pragma once

#include <iostream>
#include <string>

// Node of the tree
struct Node
{
    // Data stored in this node of the tree
    std::string data;
    // The left branch of the tree
    Node *left = nullptr;
    // The right branch of the tree
    Node *right = nullptr;
};

class BinarySearchTree
{
private:
    // Pointer to root of the tree
    Node *root = nullptr;
public:
    // Creates an empty binary tree
    BinarySearchTree();
    // Creates a binary tree with an inital word to store
    BinarySearchTree(std::string word);
    // Creates a binary tree by copying an existing tree
    BinarySearchTree(const BinarySearchTree &rhs);
    // Destroys (cleans up) the tree
    ~BinarySearchTree();
    // Adds a word to the tree
    void insert(std::string word);
    // Removes a word from the tree
    void remove(std::string word);
    // Checks if a word is in the tree
    bool exists(std::string word) const;
    // Creates a string representing the tree in alphabetical order
    std::string inorder() const;
    // Creates a string representing the tree in pre-order
    std::string preorder() const;
    // Creates a string representing the tree in post-order
    std::string postorder() const;
    // Checks if two trees are equal
    bool operator==(const BinarySearchTree &other) const;
    // Checks if two trees are not equal
    bool operator!=(const BinarySearchTree &other) const;
    // Reads in words from an input stream into the tree
    friend std::istream& operator>>(std::istream &in, BinarySearchTree &tree);
    // Writes the words, in-order, to an output stream
    friend std::ostream& operator<<(std::ostream &out, const BinarySearchTree &tree);
};

【问题讨论】:

  • 问题是什么?
  • @Jeffrey 我有一个 test.cpp,我可以运行它来测试我的功能是否正常工作,但每次都失败,它们只是不工作,我不知道为什么
  • 您的代码没有使用标题中定义的类中的内容。例如,您在代码中的何处设置成员变量 root?
  • “他们只是不工作,我不知道为什么”与正确的问题描述相去甚远。 如何他们不工作?你期待什么?你观察到了什么?您在使用调试器单步执行代码时学到了什么?
  • Node* tree; Node* treeNew; -- 你为什么要在这里声明全局变量?您通过“在侧面”创建自己的一组全局变量、构建自己的树版本而不使用标题中声明的类成员变量来作弊。通过使用全局变量,您在类之外创建了一些东西,从而违反了封装的基本规则。我建议您删除这两个变量,然后重新开始,因为您唯一需要处理的是类本身,以及您可能希望在类函数中使用的任何局部变量。

标签: c++ binary-search-tree


【解决方案1】:

你的代码有很多错误,你需要纠正它们。

例如,

  1. 在函数insert(std::string word)中,如果树为null,则没有什么可做的,但实际上你应该创建一个节点(值为“word”)并插入它。同样,根有时会发生变化,您必须考虑它。

  2. 在函数 BinarySearchTree(const BinarySearchTree &rhs) 中,您从不使用 rhs。此外,我猜你的意思是一个深度的dopy,所以你的代码根本无法工作。

我的建议是你应该重写你的函数(在完全理解 BST 之后)。同时,您可以尽快将您的问题描述清楚。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-14
    • 1970-01-01
    • 1970-01-01
    • 2010-10-26
    相关资源
    最近更新 更多