使用嵌套的“TreeNode”创建二叉树类时遇到问题C++ 中的类
我试图创建一个简单的二叉树来存储整数。我不想只在 class BinaryTree
之外创建一个 struct node
,然后在 内部实例化
类。我想将 node*
的对象BinaryTreenode
类嵌入到 BinaryTree
类中。
这是我到目前为止为该项目编写的头文件:
#ifndef HEADER_H
#define HEADER_H
#include <iostream>
using namespace std;
class BinaryTree {
private:
class TreeNode {
public:
int Data;
TreeNode* Right;
TreeNode* Left;
TreeNode() : Right(nullptr), Left(nullptr) {}
TreeNode(int data, TreeNode* lnode = nullptr, TreeNode* rnode=nullptr) :
Data(data), Right(rnode), Left(lnode) {}
};
TreeNode* root;
public:
BinaryTree();
bool Empty() const;
bool NewNode(TreeNode* node, const int& new_element);
bool Insert(TreeNode* node, const int& new_element);
};
#endif
这是这些声明的 .cpp 实现:
#include <iostream>
using namespace std;
BinaryTree::BinaryTree() : root(nullptr) {
}
bool BinaryTree::Empty() const
{
return (root == nullptr) ? true : false;
}
bool BinaryTree::NewNode(TreeNode* node, const int& new_element)
{
TreeNode* new_node;
new_node = new TreeNode;
new_node->Data = new_element;
node = new_node;
return true;
}
bool BinaryTree::Insert(TreeNode* node, const int& new_element)
{
if (Empty())
{
return NewNode(node, new_element);
}
else if (new_element < node->Data)
{
return node->Left = Insert(node->Left, new_element);
}
else
{
return node->Right = Insert(node->Right, new_element);
}
}
我正在查看的具体错误与 bool BinaryTree::Insert 相关功能。我在这两个地方收到关于赋值运算符 =
的错误通知:
(1): return node->Left = Insert(node->Left, new_element);
(2): return node->Right = Insert(node->Right, new_element);
当我调查时,出现以下错误消息:
E0513 “bool”类型的值无法分配给“BinaryTree::TreeNode *”类型的实体
我无法弄清楚如何修复此错误或它具体解决的问题。二叉搜索树的其他示例嵌入式类利用这些技术,但我无法使这些赋值运算符起作用,我也尝试更改函数的返回类型,但没有成功。
I'm trying to just create a simple binary tree to store integers. I don't want to just create a struct node
outside of my class BinaryTree
and then instantiate an object of node*
inside the BinaryTree
class. I want to embed the node
class in the BinaryTree
class.
Here is the header file I've written so far for the project:
#ifndef HEADER_H
#define HEADER_H
#include <iostream>
using namespace std;
class BinaryTree {
private:
class TreeNode {
public:
int Data;
TreeNode* Right;
TreeNode* Left;
TreeNode() : Right(nullptr), Left(nullptr) {}
TreeNode(int data, TreeNode* lnode = nullptr, TreeNode* rnode=nullptr) :
Data(data), Right(rnode), Left(lnode) {}
};
TreeNode* root;
public:
BinaryTree();
bool Empty() const;
bool NewNode(TreeNode* node, const int& new_element);
bool Insert(TreeNode* node, const int& new_element);
};
#endif
Here is the .cpp realization of these declarations:
#include <iostream>
using namespace std;
BinaryTree::BinaryTree() : root(nullptr) {
}
bool BinaryTree::Empty() const
{
return (root == nullptr) ? true : false;
}
bool BinaryTree::NewNode(TreeNode* node, const int& new_element)
{
TreeNode* new_node;
new_node = new TreeNode;
new_node->Data = new_element;
node = new_node;
return true;
}
bool BinaryTree::Insert(TreeNode* node, const int& new_element)
{
if (Empty())
{
return NewNode(node, new_element);
}
else if (new_element < node->Data)
{
return node->Left = Insert(node->Left, new_element);
}
else
{
return node->Right = Insert(node->Right, new_element);
}
}
The specific errors I'm looking at have to do with the bool BinaryTree::Insert
function. I get error notifs on the assignment operator =
at these two places:
(1): return node->Left = Insert(node->Left, new_element);
(2): return node->Right = Insert(node->Right, new_element);
The following error message shows up when I investigate:
E0513 a value of type "bool" cannot be assigned to an entity of type "BinaryTree::TreeNode *
I cannot for the life of me figure out how to fix this error or what it's specifically addressing. Other examples of a binary search tree with an embedded class utilize these techniques, and yet I cannot make these assignment operators work. I have also tried to change the return type of the functions, without any success.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论