根节点应该有一个分配的节点,但仍然是 NULL。为什么我不能为我的根分配一个节点?
Root node should have an assigned node, but remains NULL. Why can't I assign a node to my root?
我正在编写面向对象格式的二叉树。我以前有过二叉树的经验,但是我已经有一段时间没有接触过这个了。我的问题是我无法为我的根分配一个节点。每次我在调试模式下检查时,root 仍然是 NULL。发生这种情况时,cur 节点包含分配给它的所有信息。
我试过将我的 root 私有化并将 this->root = NULL;
更改为 root-> = NULL;
。我也试过制作我的所有功能 public,但没有什么不同。我尝试将 root 的子级声明为 NULL 值并将名称也声明为空字符串。
main.cpp
#include <iostream>
#include <string>
#include <fstream>
#include "Friends.h"
using namespace std;
int main() {
string line;
ifstream file;
file.open("friends.txt");
Friends f;
while (getline(file, line)) {
f.insert(f.root, line);
}
f.print(f.root);
system("pause");
return 0;
}
Friends.cpp
#include "Friends.h"
#include <iostream>
#include <string>
using namespace std;
Friends::Friends() {
this->root = NULL;
}
Friends::node* Friends::createNode(string& val) {
node* newNode = new node();
newNode->left = NULL;
newNode->right = NULL;
newNode->name = val;
return newNode;
}
Friends::node* Friends::insert(node* cur, string& val) {
if (!cur) {
cur = createNode(val);
}
else if (val < cur->name) {
insert(cur->left, val);
return cur;
}
else if (val > cur->name) {
insert(cur->right, val);
return cur;
}
return NULL;
}
void Friends::print(node* cur) {
if (!cur) {
return;
}
print(cur->left);
cout << cur->name << endl;
print(cur->right);
}
Friends.h
#ifndef FRIENDS_H
#define FRIENDS_H
#include <string>
using namespace std;
class Friends {
private:
struct node {
string name;
node* left;
node* right;
};
public:
node* root;
node* insert(node* cur, string&);
void print(node* cur);
Friends();
node* createNode(string&);
};
#endif
根节点应该有一个节点,但一直显示为 NULL
值。它也没有 运行 任何错误。它只是保持 NULL
.
更改自:
node* insert(node* cur, string&);
至:
node* insert(node* &cur, string&);
应该修复
当然实现头也应该改变
我正在编写面向对象格式的二叉树。我以前有过二叉树的经验,但是我已经有一段时间没有接触过这个了。我的问题是我无法为我的根分配一个节点。每次我在调试模式下检查时,root 仍然是 NULL。发生这种情况时,cur 节点包含分配给它的所有信息。
我试过将我的 root 私有化并将 this->root = NULL;
更改为 root-> = NULL;
。我也试过制作我的所有功能 public,但没有什么不同。我尝试将 root 的子级声明为 NULL 值并将名称也声明为空字符串。
main.cpp
#include <iostream>
#include <string>
#include <fstream>
#include "Friends.h"
using namespace std;
int main() {
string line;
ifstream file;
file.open("friends.txt");
Friends f;
while (getline(file, line)) {
f.insert(f.root, line);
}
f.print(f.root);
system("pause");
return 0;
}
Friends.cpp
#include "Friends.h"
#include <iostream>
#include <string>
using namespace std;
Friends::Friends() {
this->root = NULL;
}
Friends::node* Friends::createNode(string& val) {
node* newNode = new node();
newNode->left = NULL;
newNode->right = NULL;
newNode->name = val;
return newNode;
}
Friends::node* Friends::insert(node* cur, string& val) {
if (!cur) {
cur = createNode(val);
}
else if (val < cur->name) {
insert(cur->left, val);
return cur;
}
else if (val > cur->name) {
insert(cur->right, val);
return cur;
}
return NULL;
}
void Friends::print(node* cur) {
if (!cur) {
return;
}
print(cur->left);
cout << cur->name << endl;
print(cur->right);
}
Friends.h
#ifndef FRIENDS_H
#define FRIENDS_H
#include <string>
using namespace std;
class Friends {
private:
struct node {
string name;
node* left;
node* right;
};
public:
node* root;
node* insert(node* cur, string&);
void print(node* cur);
Friends();
node* createNode(string&);
};
#endif
根节点应该有一个节点,但一直显示为 NULL
值。它也没有 运行 任何错误。它只是保持 NULL
.
更改自:
node* insert(node* cur, string&);
至:
node* insert(node* &cur, string&);
应该修复
当然实现头也应该改变