在C++中实现具有列表子项的树
Implementing a Tree in C++ with List Children
我正在尝试在具有一串数据和表示其子级的节点列表的节点中创建C++非常简单的树。我已经完成了大部分工作,但在运行我的代码时遇到了错误(编辑(。当我尝试将子节点添加到节点时,一切都会崩溃,特别是在行children.push_back(n);
//It sets up the specified tree in a manner appropriate to the exercise, and then
//traverses the tree to find the node named "FindMe".
#include<stdio.h>
#include<stdlib.h>
#include <string>
#include <iostream>
#include <list>
struct node//Our implementation of a node.
{
public:
std::string Name;
std::list<struct node *> children;
void NameSet(std::string n){
Name = n;
}
std::string NameGet(){
return Name;
}
void ChildrenSetter(struct node *n){
children.push_back(n);
}
std::list<struct node *> ChildrenGetter(){
return children;
}
};
// A utility function to create a new BST node
struct node *newNode(std::string item)
{
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->NameSet(item);
return temp;
}
bool find(struct node *root, std::string f)
{
if (root != NULL)
{
std::cout << (root->NameGet()) << std::endl;
if(root->NameGet().compare(f)==0){
return true;
}
else{
std::list<struct node *> children = root->ChildrenGetter();
std::list<struct node *>::iterator outputIt;
for(outputIt = children.begin(); outputIt != children.end(); outputIt++){
if(find(*outputIt, f)){
return true;
}
}
}
}
return false;
}
/* This function creates the root */
struct node* insert(struct node* node, std::string Name)
{
if (node == NULL) return newNode(Name);
return node;
}
/* This function adds a left child to the current node. */
struct node* insertc(struct node* node, std::string Name)
{
if (node == NULL) return node;
struct node* temp = newNode(Name);
node->ChildrenSetter(newNode(Name));
return temp;
}
int main() //Here we build our tree and then find the requested node.
{
struct node *root = NULL;
root = insert(root, "Start");
struct node *A1 = insertc(root,"A1");
struct node *A2 = insertc(root, "A2");
struct node *D1 = insertc(A1,"D1");
insertc(D1,"E1");
struct node *B1 = insertc(A2,"B1");
struct node *B2 = insertc(A2, "B2");
insertc(B1,"FindMe");
insertc(B2, "C1");
if(find(root, "FindMe")){
std::cout << "Requested node found" << std::endl;
}
else{
std::cout << "Unable to find the requested node." << std::endl;
}
return 0;
}
编辑:我想通了,我需要使用新的而不是malloc。非常感谢在这里回答的人。
//This code was written by Donnelly Warren in response to a Garmin Coding exercise given
//as part of an application for a summer internship on 2/20/18
//It sets up the specified tree in a manner appropriate to the exercise, and then
//traverses the tree to find the node named "FindMe".
#include<stdio.h>
#include<stdlib.h>
#include <string>
#include <iostream>
#include <list>
struct node//Our implementation of a node.
{
public:
std::string Name;
std::list<struct node *> children;
void NameSet(std::string n){
Name = n;
}
std::string NameGet(){
return Name;
}
void ChildrenSetter(struct node *n){
children.push_back(n);
}
std::list<struct node *> ChildrenGetter(){
return children;
}
};
// A utility function to create a new BST node
struct node *newNode(std::string item)
{
struct node *temp = new struct node;
temp->NameSet(item);
return temp;
}
bool find(struct node *root, std::string f)
{
if (root != NULL)
{
std::cout << (root->NameGet()) << std::endl;
if(root->NameGet().compare(f)==0){
return true;
}
else{
std::list<struct node *> children = root->ChildrenGetter();
std::list<struct node *>::iterator outputIt;
for(outputIt = children.begin(); outputIt != children.end(); outputIt++){
if(find(*outputIt, f)){
return true;
}
}
}
}
return false;
}
/* This function creates the root */
struct node* insert(struct node* node, std::string Name)
{
if (node == NULL) return newNode(Name);
return node;
}
/* This function adds a child to the current node. */
struct node* insertc(struct node* node, std::string Name)
{
if (node == NULL) return node;
struct node* temp = newNode(Name);
node->ChildrenSetter(temp);
return temp;
}
void del(struct node* node){//This will delete our tree.
if(node != NULL){
std::list<struct node *> children = node->ChildrenGetter();
std::list<struct node *>::iterator outputIt;
for(outputIt = children.begin(); outputIt != children.end(); outputIt++){
del(*outputIt);
}
delete node;
}
}
int main() //Here we build our tree and then find the requested node.
{
struct node *root = NULL;
root = insert(root, "Start");
struct node *A1 = insertc(root,"A1");
struct node *A2 = insertc(root, "A2");
struct node *D1 = insertc(A1,"D1");
insertc(D1,"E1");
struct node *B1 = insertc(A2,"B1");
struct node *B2 = insertc(A2, "B2");
insertc(B1,"FindMe");
insertc(B2, "C1");
if(find(root, "FindMe")){
std::cout << "Requested node found" << std::endl;
}
else{
std::cout << "Unable to find the requested node." << std::endl;
}
del(root);
return 0;
}
问题是子项没有在你尝试使用它的函数中定义,你还必须传递 node 类型变量的引用/指针。
相关文章:
- Pybind11:将元组列表从Python传递到C++
- 从链接列表c++中删除一个项目
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- C++如何通过用户输入删除列表元素
- 读取文件的最后一行并输入到链接列表时出错
- 复制列表初始化的隐式转换的等级是多少
- LNK2038、MSVS2017 MAGMA的原因列表
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 没有为自己的结构调用列表推回方法
- 使用简单类型列表实现的指数编译时间.为什么
- 一对向量构造函数:初始值设定项列表与显式构造
- 标准是否使用多余的大括号(例如 T{{{10}}})定义列表初始化?
- 通过for循环使用用户输入填充列表
- C++:如何使函数只返回作为列表一部分的字符串
- 概念中的cv限定符需要表达式参数列表
- 下面是我为检测链接列表中的循环而制作的代码
- 建议在运行时将带有类实例的列表从c++导入qml
- 如何维护资源管理器项目视图中当前可见的项目列表
- 在卡萨布兰卡形成编码参数的列表
- 在没有参数列表的情况下使用模板名称"Event"无效,模板问题