Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Code Help: Problem: Write a program that can input and display a persons family tree. My code is throwing up so many errors... help!

C++ Code Help:

Problem: Write a program that can input and display a persons family tree. My code is throwing up so many errors... help!

It allows up to three generations.

Main.cpp

#include

#include "stdafx.h"

#include

#include

#include

#include

#include

#include

using namespace std;

//Following program accepts family upto three levels and

//displays the family tree

typedef string Elem;

//Define a structure node which contains list

//of its children nodes.

struct Node

{

Elem elt;

list children;

//Constructor

Node() :elt(), children() {}

};

//Define class Position, its data member consists of a pointer v to a node of the tree.Define

//method children to return list of positions of nodes children.

class Position

{

public:

Node* v;

public:

Position(Node* _v = NULL) :v(_v) {}

//Overload dereferencing operator

Elem& operator*()

{

return v->elt;

}

//Function to return list of positions of

// the current nodes children.

list children()

{

list::iterator i;

list pl;

for (i = v->children.begin();

i != v->children.end(); i++)

//Push position of node onto position list pl

{

pl.push_back(Position(*i));

}

return pl;

}

};

//Create class Tree with constructor to initialize empty tree and members to add new children to

//external nodes.

class Tree

{

protected:

Node* node;

public:

Position pos;

public:

//Constructor

Tree()

{

_root = NULL;

}

//Function to add root to binary tree

void addRoot(Elem elt1)

{

_root = new Node;

_root->elt = elt1;

}

//Function to add nodes to external nodes

void expandExternal(const Position& P,

list nl)

{

Node *v = P.v;

//Assign node list that is passed as argument to

// nodes children list.

v->children = nl;

}

//Function to return root node position

Position root()

{

return Position(_root);

}

private:

Node* _root;

};

//In main function create an object for class Tree.Using expandExternal function add children to

//the tree.The tree is constructed up to three levels of family.

int main()

{

Tree t;

list nl;

list pl, cl;

Node* temp;

Elem rootname;

Elem tempname;

cout << "Enter the name of the starting"

<< " family member: ";

cin >> rootname;

//Add first family member as root node to tree

t.addRoot(rootname);

Position rootpos;

rootpos = t.root();

char choice;

cout << "Does "<

<< " have children? y/n ";

cin >> choice;

while (choice == 'y')

{

cout << "Enter the name of the child: ";

cin >> tempname;

temp = new Node;

temp->elt = tempname;

nl.push_back(temp);

cout << "Does "<

<< " have another child? y/n ";

cin >> choice;

}

//Using the position of root node, add children to

//root node using expandExternal function.

t.expandExternal(rootpos, nl);

nl.clear();

//List pl list position of nodes that children to

//root node

pl = rootpos.children();

list::iterator i;

pl = rootpos.children();

list::iterator i;

//Add nodes to the tree until there are no nodes to

// add.

while (pl.size() != 0)

{

for (i = pl.begin(); i != pl.end(); i++)

{

cout << "Does " << *(*i) << " have child?y/n ";

cin >> choice;

while (choice == 'y')

{

cout << "Enter the name of the child: ";

cin >> tempname;

temp = new Node;

temp->elt = tempname;

//Push the new node onto the list nl.

nl.push_back(temp);

cout << "Does " << *(*i) << " have another child?y/n ";

cin >> choice;

}

if (nl.size() != 0)

{

t.expandExternal((*i), nl);

nl.clear();

cl = (*i).children();

ps.insert(ps.end(), cl.begin()

, cl.end());

}

}

pl = ps;

ps.clear();

}

//Print the family tree

cout << " The family tree is: ";

cout << "Father \t children ";

cout << *rootpos << " :\t";

pl = rootpos.children();

for (i = pl.begin(); i != pl.end(); i++)

cout << *(*i) << " ";

cout << " ";

//The size of list pl is zero when external

//nodes are processed.

while (pl.size() != 0)

{

for (i = pl.begin(); i != pl.end(); i++)

{

cout << *(*i) << " :\t";

cl = (*i).children();

for (list::iterator j = cl.begin();

j != cl.end(); j++)

{

cout << *(*j) << " ";

ps.push_back(*j);

}

cout << " ";

}

pl = ps;

ps.clear();

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro Oracle Fusion Applications Installation And Administration

Authors: Tushar Thakker

1st Edition

1484209834, 9781484209837

More Books

Students also viewed these Databases questions

Question

Do you have any favorite stories from your work life? Religion

Answered: 1 week ago