使用List Children在C ++中实现树

时间:2018-03-22 01:41:46

标签: c++ list tree nodes

我试图在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;
}

编辑:我想通了,我需要使用new而不是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;
}

1 个答案:

答案 0 :(得分:0)

问题是在您尝试使用它的函数中没有定义子项,您还必须传递 node 类型变量的引用/指针。