I'm having trouble with this program, it outputs me a garbage number when I set another value in a node. I was told to fix my conditional statements. Anyone have an idea to fix it? I would appreciate it.
/*
Build a general tree which will store only a numeric value. Allow the user to enter two pieces of information - the parent key and the key for the node to be added. Add each node in arrival sequence. In order to do this, you must define a node, a position within the tree, and a list. Let the list be an array (multiply occurring). Provide the ability to retrieve all of the nodes in post-order sequence. Also provide the ability to search for a particular value and make sure you address the scenario where I enter a value which does not exist.
*/
#include <iostream>
using namespace std;
void addNode(Tree &myTree, int pKey, int nKey)
{
//add node as root
if (myTree.root == NULL)
{
//Create a node
Node newNode;
//Initialize the node
newNode.key = nKey;
newNode.parent = NULL;
newNode.children = NULL;
newNode.next = NULL;
//Add node to tree
myTree.root = &newNode;
cout<<"test node as root "<<endl;
}
else
{
//New Node is a child of root
do
{
//If root does not have any children
cout<<"test in do "<<endl;
if (myTree.root->children == NULL)
{
cout<<"test in root does not have any children "<<endl;
//Create a list of children and add a new node to that list
//Create a new list
List myList;
//Initialize the list
myList.head = NULL;
myList.count = 0;
//create a new node
Node newNode;
//initialize the node
newNode.key = nKey;
newNode.parent = myTree.root; //add root as parent
newNode.children = NULL;
newNode.next = NULL;
//add new node to list
myList.head = &newNode;
//increase the number of items in the list
// myList.count++;
}
//If root has children
else
{
//add a new node to the list of children
//create a new node
Node newNode;
//initialize the node
newNode.key = nKey;
newNode.parent = myTree.root; //add root as parent
newNode.children = NULL;
newNode.next = NULL;
//add new node to end of list
//create a temporary pointer to a node
Node* temp;
//initialize the pointer to the head of the list
temp = myTree.root->children->head;
//move the pointer to the almost end of the list
for (int i=0; i < myTree.root->children->count-1; i++)
{
temp=temp->next;
}
//add the node as the next node
temp->next = &newNode;
//increase the number of items in the list
// myTree.root->children->count++;
}
} while (myTree.root->key != pKey);
}
}
void searchNode(Tree &myTree, int pKey, int nKey)
{
int flag=0;
//create a temporary pointer to a node
Node* temp;
//initialize the pointer to the head of the list
temp = myTree.root->children->head;
if(myTree.root->key == nKey)
{
cout << "Found at root node!!\n"; flag = 1;
}
//move the pointer through the list
else
{
for(int i = 0; i < myTree.root->children->count; i++)
{
if(temp->key == nKey && temp->parent->key == pKey)
{
cout << "Found!!!\n"; flag = 1;
//move the pointer
temp = temp->next;
}
if(flag == 0)
{
cout << "Not Found!!\n";
}
}
}
}
void printTree(Tree myTree)
{
//print tree root
cout << "n: " << myTree.root->key << endl;
//if root has a left child, print it
if (myTree.root->children != NULL)
{
cout << "c: ";
//create a temporary pointer to a node
Node* temp;
//initialize the pointer to the head of the list
temp = myTree.root->children->head;
//move the pointer through the list
for (int i=0; i < myTree.root->children->count; i++)
{
//display the contents of the node
cout << temp->key <<"\t";
//move the pointer
temp=temp->next;
}
}
cout << endl;
}
int main ()
{
cout << "This Program will build and manipulate a tree" << endl;
int pKey = 0;
int nKey = 0;
//Create the tree
Tree myTree;
//Initialize the tree
myTree.root = NULL;//Set root to NULL
do
{
//Allow the user to enter two pieces of information - the parent key and the key for the node to be added.
cout << "Please type in the parent key: ";
cin >> pKey;
cout << "Please type in the node key: ";
cin >> nKey;