使用嵌套的“TreeNode”创建二叉树类时遇到问题C++ 中的类

发布于 2025-01-12 11:56:52 字数 2139 浏览 2 评论 0原文

我试图创建一个简单的二叉树来存储整数。我不想只在 class BinaryTree 之外创建一个 struct node,然后在 内部实例化 node* 的对象BinaryTree 类。我想将 node嵌入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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文