二进制搜索树不变的标头文件

Binary Search Tree unchangeable header file

本文关键字:文件 搜索树 二进制      更新时间:2023-10-16

我在二进制搜索树上进行分配。给我们一个标题文件,不允许我们更改它。

我具有创建具有初始值的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);
};

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

例如,

  1. 在函数插入(std :: string Word(中,如果树是无效的,则无需完成,但实际上您应该创建一个节点(带有" word"的值(,然后插入它。同样,根可能有时会改变,您必须考虑。

  2. 在功能binarysearchtree(const binarysearchtree&amp; rhs(中,您永远不会使用RHS。此外,我想您的意思是有深度的态度,因此您的代码根本无法工作。

我的建议是,您应该重写您的功能(在对BST有充分的了解之后(。同时,您可以清楚地描述您的问题。