简体   繁体   English

为二叉搜索树创建一个新节点

[英]Creating a new Node for a binary search tree

For a school project I am trying to make a binary search tree at the same time we are supposed to learn how to use 'friendship' in classes. 对于一个学校项目,我试图同时制作一个二叉搜索树,我们应该学习如何在课堂上使用“友谊”。 The errors I get while compiling are: [I put comments in code where the errors originate from for clarity] (Keep in mind I am not allowed to nest Node in the BST class they are both supposed to be in separate files and classes for the sake of this programming assignment) 我在编译时遇到的错误是:[为了清楚起见,我在代码中添加了注释,错误的来源是(请记住,我不允许将Node嵌套在BST类中),它们都应该放在单独的文件和类中为了这个编程任务)

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

I tried using the 'new' operator in BST.cpp and Node.cpp but I still can't get rid of those error messages. 我尝试在BST.cpp和Node.cpp中使用'new'运算符,但仍然无法摆脱这些错误消息。 I believe that I might be missing a few syntax that is making the compiler not like it. 我相信我可能会缺少一些使编译器不喜欢的语法。 Here are the files used in this assignment: (Note some functions arent used yet since I have not gotten that far in the project.) Node.h 这是此作业中使用的文件:(请注意,由于我在项目中还没有到此为止,所以尚未使用某些功能。)Node.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 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;
}

My intent so far is to have Node::create_Node to make a new node, Nullify all the pointers, and lastly pass the pointer of the node back to BST.cpp so the pointers can be modified and inserted into the tree. 到目前为止,我的意图是让Node :: create_Node新建一个节点,使所有指针无效,最后将节点的指针传递回BST.cpp,以便可以修改指针并将其插入树中。 Below are BST.cpp and BST.h (I put comments where the errors occur for your clarity) BST.h: 下面是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

Finally, BST.cpp (where the errors occur) The errors happen when I attempt to modify the pointers of z (z is a pointer the brand new node that was just created) including its m_left, m_right and m_parent. 最后,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
    }
}

If you want to use the return of get_left() etc as a target for an assignment then you must return a reference. 如果要将get_left()等的返回值用作分配的目标,则必须返回引用。

The bigger error however is that you've made all those methods static for some reason. 但是,更大的错误是由于某种原因使所有这些方法静态化。 That's not going to work either. 那也不行。

Node*& Node::get_left()
{
    return m_left;
}
Node*& Node::get_right()
{
    return m_right;
}
Node*& Node::get_parent()
{
    return m_parent;
}

However since the point is to learn how to use friendship, you should probably just delete these methods and declare BST as a friend of Node and have BST access these fields directly. 但是,由于要点是学习如何使用友谊,因此您可能应该删除这些方法并将BST声明为Node的朋友,并让BST直接访问这些字段。 That seems to be the point of the exercise. 这似乎是练习的重点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM