在C++中实现具有列表子项的树

Implementing a Tree in C++ with List Children

本文关键字:列表 C++ 实现      更新时间:2023-10-16

我正在尝试在具有一串数据和表示其子级的节点列表的节点中创建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 类型变量的引用/指针。