问题在于带有两个参数的insert方法。 函数insert(char letter,string code)调用insert(TreeNode * node,char letter,char code)。但是,它不会将左或右子节点指定为节点。具有三个参数的insert方法应该创建一个新节点。然后,应将此节点指定为左或右子节点。
#include "tree.h"
#include <iostream>
#include <string>
using std::string;
using std::cout;
using std::endl;
BinaryTree::BinaryTree() {
root = nullptr;
}
BinaryTree::~BinaryTree() {
empty_tree(root);
}
void BinaryTree::empty_tree(TreeNode *node) {
if (root) {
if (node->left) {
return empty_tree(node->left);
}
if (node->right) {
return empty_tree(node->right);
}
delete node;
}
}
void BinaryTree::insert(char letter, string code) {
TreeNode *node = root;
TreeNode *temp = nullptr;
if (node) {
for (string::size_type i = 0; i < code.length(); i++) {
//temp = node;
if (code[i] == '.') {
if (node->left) {
node = node->left;
}
else {
insert(temp, letter, code[i]);
node->left = temp;
}
}
else {
if (node->right) {
node = node->right;
}
else {
return insert(temp, letter, code[i]);
node->right = temp;
}
}
}
}
/*else {
return insert();
}*/
}
void BinaryTree::insert() {
if (!root) {
root= new TreeNode();
root->letter = '*';
root->left = nullptr;
root->right = nullptr;
root->code = "*";
}
}
void BinaryTree::insert(TreeNode *node, char letter, char code) {
if (!node) {
node = new TreeNode();
node->letter = letter;
node->left = nullptr;
node->right = nullptr;
node->code += code;
}
}
void BinaryTree::print_tree() {
return print_tree(root);
}
void BinaryTree::print_tree(TreeNode *tree) {
if (tree) {
if (tree->left) {
print_tree(tree->left);
}
if (tree->right) {
print_tree(tree->right);
}
cout << "Node is " << tree->letter << " and in Morse Code is " << tree->code << endl << endl;
}
}
以下是包含main的文件。 morse-code.txt文件从a到z的顺序只有字母。每行有一个字母,后跟一个空格,后跟莫尔斯码。
#include "tree.h"
#include <iostream>
#include <fstream>
#include <string>
using std::cout;
using std::endl;
int main() {
std::string code;
std::ifstream infile;
char letter;
BinaryTree tree;;
infile.open("morse-code.txt");
if (!infile) {
std::cout << "File unable to open" << std::endl;
}
else {
cout << "morse-code.txt\n\n";
tree.insert();
while (std::getline(infile, code)) {
tree.insert(code[0], code.substr(2, code.length()-2));
cout << code << endl;
}
cout << "\n******Tree Representation******\n\n";
tree.print_tree();
}
system("pause");
}
文本文件
a .-
b -...
c -.-.
d -..
e .
f ..-.
g --.
h ....
i ..
j .---
k -.-
l -.--
m --
n -.
o ---
p .--.
q --.-
r .-.
s ...
t -
u ..-
v ...-
w .--
x -..-
y -.--
z --..
tree.h中
#ifndef _TREE_H
#define _TREE_H
//#include <iostream>
//#include <fstream>
#include <string>
using std::string;
class BinaryTree {
private:
struct TreeNode {
char letter;
string code;
TreeNode *left;
TreeNode *right;
}; TreeNode *root;
public:
BinaryTree();
~BinaryTree();
void insert();
void insert(TreeNode *new_node, char letter, char code);
void insert(char letter, string code);
void empty_tree(TreeNode *node);
void print_tree(TreeNode *node);
void print_tree();
};
#endif
答案 0 :(得分:2)
我认为插入不是您的代码唯一的问题。你的empty_tree是不正确的(不会释放所有内存,因为你过早地返回。你错误的代码的特定部分是代码的以下行:&#34; return insert(temp,letter,code [i] );&#34; 其余的代码看起来很好。
它应该看起来像这样:
void BinaryTree::empty_tree(TreeNode *node) {
if (!node) return;
delete node;
empty_tree(node->left );
empty_tree(node->right);
/* See Jerry's solution below. */
}
void BinaryTree::insert(char letter, string code) {
TreeNode *node = root;
if (!root) /* do something*/;
for (string::size_type i = 0; i < code.length(); i++) {
if (code[i] == '.') {
if (node->left)
insert(&node->left, letter, code[i]);
node = node->left;
}
else {
if (!node->right)
insert(&node->right, letter, code[i]);
node = node->right
}
}
}
void BinaryTree::insert(TreeNode **node, char letter, string code) {
*node = new TreeNode();
(*node)->letter = letter;
(*node)->left = nullptr;
(*node)->right = nullptr;
(*node)->code = code;
}
在tree.h中:
typedef struct _TreeNode {
char letter;
string code;
struct _TreeNode *left;
struct _TreeNode *right;
} TreeNode; TreeNode *root;
我*编译了以下代码:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
typedef struct _TreeNode {
char letter;
string code;
struct _TreeNode *left;
struct _TreeNode *right;
} TreeNode;
class BinaryTree {
private:
TreeNode *root;
public:
BinaryTree();
~BinaryTree();
void insert();
void insert(TreeNode **new_node, char letter, string code);
void insert(char letter, string code);
void empty_tree(TreeNode *node);
void print_tree(TreeNode *node);
void print_tree();
};
BinaryTree::BinaryTree() {
root = nullptr;
}
BinaryTree::~BinaryTree() {
empty_tree(root);
}
void BinaryTree::empty_tree(TreeNode *node) {
if (!node) return;
delete node;
empty_tree(node->left );
empty_tree(node->right);
}
void BinaryTree::insert(char letter, string code) {
TreeNode *node = root;
if (!root) insert();
for (string::size_type i = 0; i < code.length(); i++) {
if (code[i] == '.') {
if (!node->left)
insert(&node->left, letter, code.substr(0,i));
node = node->left;
} else {
if (!node->right)
insert(&node->right, letter, code.substr(0,i));
node = node->right;
}
}
}
void BinaryTree::insert() {
root = new TreeNode();
root->letter = '*';
root->left = nullptr;
root->right = nullptr;
root->code = "*";
}
void BinaryTree::insert(TreeNode **node, char letter, string code) {
*node = new TreeNode();
(*node)->letter = letter;
(*node)->left = nullptr;
(*node)->right = nullptr;
(*node)->code = code;
}
void BinaryTree::print_tree() {
return print_tree(root);
}
void BinaryTree::print_tree(TreeNode *tree) {
if (tree) {
if (tree->left ) print_tree(tree->left );
cout << "Node is " << tree->letter << " and in Morse Code is "
<< tree->code << endl << endl;
if (tree->right) print_tree(tree->right);
}
}
int main() {
string code;
ifstream infile;
char letter;
BinaryTree tree;
infile.open("morse-code.txt");
if (!infile) {
cout << "File unable to open" << endl;
}
else {
cout << "morse-code.txt\n\n";
tree.insert();
while (std::getline(infile, code)) {
tree.insert(code[0], code.substr(2, code.length()-2));
cout << code << endl;
}
cout << "\n******Tree Representation******\n\n";
tree.print_tree();
}
system("pause");
}
答案 1 :(得分:-1)
所以问题出在我的tree_driver.cpp文件中。该文件中包含的大多数问题都在insert函数中,有两个参数。以下是工作代码。
tree_driver.cpp
#include "tree.h"
#include <iostream>
#include <string>
using std::string;
using std::cout;
using std::endl;
BinaryTree::BinaryTree() {
root = nullptr;
}
BinaryTree::~BinaryTree() {
empty_tree(root);
}
void BinaryTree::empty_tree(TreeNode *node) {
if (!node) {
if (node->left) {
empty_tree(node->left);
}
if (node->right) {
empty_tree(node->right);
}
delete node;
}
}
//The following member function contains the most alterations
void BinaryTree::insert(char letter, string code) {
if (!root) {
insert(root, letter, code, true);
}
else {
TreeNode *node = root;
for (string::size_type i = 0; i < code.length(); i++) {
if (code[i] == '.') {
if (!node->left) {
if (i == code.length() - 1) {
insert(node->left, letter, code.substr(0, i + 1), true);
}
else {
insert(node->left, letter, code.substr(0, i + 1), false);
}
}
node = node->left;
if (node->left) {
if (i == code.length() - 1) {
node->letter = letter;
}
}
if (!node->left) {
if (i == code.length() - 1) {
node->letter = letter;
}
}
}
else if (code[i] == '-') {
if (!node->right) {
if (i == code.length() - 1) {
insert(node->right, letter, code.substr(0, i + 1), true);
}
else {
insert(node->right, letter, code.substr(0, i + 1), false);
}
}
node = node->right;
if (node->right) {
if (i == code.length() - 1) {
node->letter = letter;
}
}
if (!node->right) {
if (i == code.length() - 1) {
node->letter = letter;
}
}
}
}
}
}
void BinaryTree::insert(TreeNode *&node, char letter, string code, bool last) {
if (last) {
node = new TreeNode();
node->letter = letter;
node->right = nullptr;
node->left = nullptr;
node->code = code;
}
if (!last) {
node = new TreeNode();
node->letter = '\0';
node->right = nullptr;
node->left = nullptr;
node->code = code;
}
}
void BinaryTree::print_tree() {
print_tree(root);
}
void BinaryTree::print_tree(TreeNode *tree) {
if (tree) {
if (tree->left) {
print_tree(tree->left);
}
cout << "Node is " << tree->letter << " and in Morse Code is " << tree->code << endl << endl;
if (tree->right) {
print_tree(tree->right);
}
}
}
tree_tester。
#include "tree.h"
#include <iostream>
#include <fstream>
#include <string>
using std::cout;
using std::endl;
int main() {
std::string code_main;
std::ifstream infile;
BinaryTree tree;;
infile.open("morse-code.txt");
if (!infile) {
std::cout << "File unable to open" << std::endl;
}
else {
cout << "morse-code.txt\n\n";
tree.insert('0', "*");
while (std::getline(infile, code_main)) {
tree.insert(code_main[0], code_main.substr(2, code_main.length()-2));
cout << code_main << endl;
}
infile.close();
cout << "\n******Tree Representation******\n\n";
tree.print_tree();
}
system("pause");
return 0;
}
tree.h中
#ifndef _TREE_H
#define _TREE_H
#include <string>
using std::string;
class BinaryTree {
private:
struct TreeNode {
string letter;
string code;
TreeNode *left;
TreeNode *right;
}; TreeNode *root;
public:
BinaryTree();
~BinaryTree();
/*void insert();*/
void insert(TreeNode *&new_node, char letter, string code, bool last);
void insert(char letter, string code);
void empty_tree(TreeNode *node);
void print_tree();
void print_tree(TreeNode *node);
};
#endif