二进制搜索树不变的标头文件
Binary Search Tree unchangeable header file
我在二进制搜索树上进行分配。给我们一个标题文件,不允许我们更改它。
我具有创建具有初始值的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);
};
您的代码有很多错误,您需要纠正它们。
例如,
-
在函数插入(std :: string Word(中,如果树是无效的,则无需完成,但实际上您应该创建一个节点(带有" word"的值(,然后插入它。同样,根可能有时会改变,您必须考虑。
-
在功能binarysearchtree(const binarysearchtree&amp; rhs(中,您永远不会使用RHS。此外,我想您的意思是有深度的态度,因此您的代码根本无法工作。
我的建议是,您应该重写您的功能(在对BST有充分的了解之后(。同时,您可以清楚地描述您的问题。
相关文章:
- 有根的二进制搜索树.保留与其父级的链接
- 正在尝试重载二进制搜索树分配运算符
- 使用C++创建特殊的二叉搜索树
- 在递归二叉搜索树中搜索
- 在二叉搜索树中插入时出现分段错误
- C++二叉搜索树模板从函数返回节点
- 我可以在没有堆栈的情况下在二叉搜索树中实现迭代器吗?
- 在二叉搜索树C++中计算平均值
- 在字符串的二叉搜索树中搜索子字符串 - C++
- 在二叉搜索树中插入新元素
- 如何打印在二叉搜索树中找到的数据?
- 查找存储在二叉搜索树的所有非叶子中的数据总和?(返回整数的独立递归函数
- 这是一个二叉搜索树吗?黑客排名问题
- 二叉搜索树 - 实现"search"函数
- 二进制搜索树不变的标头文件
- 将二进制搜索树导出到.csv文件
- 读取每个单词的文件,并使用二叉搜索树(词典)对这些单词进行排序
- 将二叉搜索树写入 C++ 中的文件
- 通过头文件函数将遍历的数据从二进制搜索树加载到Vector中
- c++在二叉搜索树中从文本文件中输入单词