为二进制搜索树创建一个新节点
Creating a new Node for a binary search tree
对于一个学校项目,我正在尝试制作一个二进制搜索树,同时我们应该学习如何在课堂上使用"友谊"。我在编译时遇到的错误是:[为了清楚起见,我在错误来源的代码中添加了注释](请记住,我不允许在BST类中嵌套Node,为了这个编程任务,它们都应该在单独的文件和类中)
BST.cpp: In member function `void BST::insert(std::string, std::string)':
BST.cpp:51: error: non-lvalue in assignment
BST.cpp:58: error: non-lvalue in assignment
BST.cpp:62: error: non-lvalue in assignment
makefile.txt:9: recipe for target `BST.o' failed
make: *** [BST.o] Error 1
我尝试在BST.cpp和Node.cpp中使用"new"运算符,但仍然无法消除这些错误消息。我相信我可能缺少了一些语法,这让编译器不喜欢它。以下是本次作业中使用的文件:(注意,有些函数还没有使用,因为我在这个项目中还没有走那么远。)节点.h
#ifndef NODE_H_INCLUDED
#define NODE_H_INCLUDED
#include <iostream>
#include <string>
using namespace std;
class BST;
class Node
{
public:
Node(string key, string data)
{m_key = key; m_data = data;}
~Node();
static string get_key(); //takes in ptr to node and returns its key
static string get_data(); //takes in ptr to node and returns its data
static Node* get_left(); //takes in ptr to node and returns its left child pointer
static Node* get_right(); //takes in ptr to node and returns its right child pointer
static Node* get_parent(); //takjes in ptr to node and returns its parent pointer
static Node* create_node(string key, string data);
static void destroy_node();
private:
string m_key;
string m_data;
Node *m_left;
Node *m_right;
Node *m_parent;
};
#endif // NODE_H_INCLUDED
Node.cpp
#include "Node.h"
static string Node::get_key()
{
return m_key;
}
static string Node::get_data()
{
return m_data;
}
static Node* Node::get_left()
{
return m_left;
}
static Node* Node::get_right()
{
return m_right;
}
static Node* Node::get_parent()
{
return m_parent;
}
static Node* Node::create_node(string key, string data)
{
Node* ptr = new Node(key, data);
ptr->m_left = NULL;
ptr->m_right = NULL;
ptr->m_parent = NULL;
return ptr;
}
到目前为止,我的意图是让Node::create_Node创建一个新节点,将所有指针置空,最后将节点的指针传递回BST.cpp,以便可以修改指针并将其插入到树中。以下是BST.cpp和BST.h(为了清楚起见,我在出现错误的地方添加了注释)BST.h:
#ifndef BST_H_INCLUDED
#define BST_H_INCLUDED
#include <iostream>
#include <string>
using namespace std;
class BST
{
public:
BST()
{m_root = NULL;}
~BST();
void insert(string key, string data);
void find(string key);
void remove(string key, string data);
void print();
friend class Node;
private:
Node* m_root;
};
#endif // BST_H_INCLUDED
最后,BST.cpp(错误发生的地方)当我试图修改z的指针(z是刚刚创建的全新节点的指针),包括它的m_left、m_right和m_parent时,就会发生错误。
#include "BST.h"
#include "Node.h"
void BST::insert(string key, string data)
{
Node* x = m_root;
Node* y = NULL;
Node* z = Node::create_node(key, data);
while(x != NULL)
{
y = x;
if(key < x->get_key())
{
x = x->get_left();
}
else
{
x = x->get_right();
}
}
z->get_parent() = y; //error: non-lvalue in assignment
if(y == NULL)
{
m_root = z;
}
else if(z->get_key() < y->get_key())
{
y->get_left() = z; //error: non-lvalue in assignment
}
else
{
y->get_right() = z; //error: non-lvalue in assignment
}
}
如果要使用get_left()
等的返回作为赋值的目标,则必须返回引用。
然而,更大的错误是,由于某种原因,您使所有这些方法都是静态的。这也行不通。
Node*& Node::get_left()
{
return m_left;
}
Node*& Node::get_right()
{
return m_right;
}
Node*& Node::get_parent()
{
return m_parent;
}
然而,由于重点是学习如何使用友谊,您可能只需要删除这些方法,并将BST声明为Node的朋友,然后让BST直接访问这些字段。这似乎就是练习的重点。
相关文章:
- 为什么C++对链表中的下一个节点使用指针,而像 C# 或 Java 这样的语言只使用类 Node 的名称?
- 在C++的链表末尾插入一个节点
- 在单链表前面添加一个节点后,我无法遍历每个节点
- 删除链接列表中剩余的最后一个节点
- 避免在使用链接列表从 deque 中删除最后一个节点时出现内存泄漏
- C++循环链表删除,计数从下一个节点开始
- 如何删除上一个节点?
- 添加一个节点,并在通用树中的两个给定节点之间找到路径成本,其中c 中的儿童列表
- 我试图键入一个函数来在单链表的末尾添加一个节点,但不起作用
- 尝试指向链表中的上一个节点
- 取消下一个节点的空链接列表的头
- 删除最后一个节点,并将尾部更新到最后一个节点
- 计算列表中与线性链接列表中最后一个节点具有相同数据的节点的数量
- 从C++二进制搜索树中删除一个节点(类而非结构)
- 为什么我的原始二叉搜索树被修改,即使我在另一个节点中复制了根节点
- 如何在QT中获取XML文件的最后一个节点
- 交换C 中的第一个和最后一个节点
- 删除链表的每隔一个节点
- 尾部指向最后一个节点
- 使用循环在链接列表的前面插入一个节点