2 Replies - 762 Views - Last Post: 20 October 2009 - 04:00 PM Rate Topic: -----

#1 Anum22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 16-September 09

Need help with pointer based trees

Post icon  Posted 19 October 2009 - 03:38 PM

My professor handed me back my project telling me there was something wrong with it but refused to tell me how to fix it or what was wrong. He doesn't seem to know how to read code and expects us to figure everything out ourselves when all the material is new to us. Here is my code can some one help me to figure out where the problem is? It is suppose to be a pointer based three tree traversal

#include <iostream>

using namespace std;


struct treenode									  // Declare a structure treenode
{
	treenode *left;
	char information;
	treenode *right;
};

struct stack										// Declare a stack
{
	treenode *link;
	bool working;
};

stack tree[63];									// stack size
treenode *tree_data = new treenode;
treenode *bottom;

treenode PreOrder_Store()						 // Declare the preorder store
{
	tree_data = NULL;
	int top = 0;
	char answer;
	char answer1;
	bool store;
	store = false;
	bottom = new treenode;

	while(!store)
	{
		bottom -> left = NULL;
		bottom -> right = NULL;

		if (tree_data == NULL)
		{
			tree_data = bottom;
		}
		else 
		{
			treenode *leftnode, *rightnode;
			leftnode = new treenode;
			rightnode = new treenode;

			cout << "Is this sub(tree) non-empty (Y or N)? " << endl;	 // ask the user
			cin >> answer;

			if (answer == 'Y')
			{
				cout << "Enter its root" << endl;
				cin >> bottom -> information;
				cout << "Is there a right subtree for this root (Y or N)? " << endl;  // ask the user
				cin >> answer1;

				if (answer1 == 'Y')
				{
					bottom -> right = rightnode;
					rightnode -> right = NULL;
					rightnode -> left = NULL;
					leftnode -> right = NULL;
					leftnode -> left = NULL;

					tree[top].link = rightnode;
					top++;

					cout << "You are at the " << bottom -> information << " looking at its left subtree." << endl;

					bottom -> left = leftnode;
					tree[top].link = bottom;
					top++;
					bottom = leftnode;
				}
			}
			else if ((top > 0) && (answer == 'Y') && (bottom == NULL) || ((top > 0) && (answer == 'N')))
			{
				top--;
				bottom = tree[top].link;
				cout << "You are at the " << bottom -> information << " looking at its right subtree." << endl;
				top--;
				bottom = tree[top].link;
			}
			else if ((answer == 'Y') && (bottom == NULL) || (answer == 'N')) 
			{
				store = true;
			}
		}
	}
	return *tree_data;
}
void PreOrder()											 // Declare post order
{
	int top = 0;
	bottom = tree_data;

	while((!bottom == NULL) || (top > 0))
	{
		if (bottom -> left == NULL)
		{
			cout << bottom -> information << endl;
			top --;
			bottom = tree[top].link;
		}
		else
		{
			cout << bottom -> information << endl;
			tree[top].link = bottom -> right;
			top++;
			bottom = bottom -> left;
		}
	}
}
void Inorder()														   // declare inorder
{
	int top = 0;
	bottom = tree_data;

	while((!bottom == NULL) || (top > 0))
	{
		if (bottom -> left == NULL)
		{
			cout << bottom -> information << endl;
			top --;
			bottom = tree[top].link;

			if (bottom -> left == NULL)
			{
				break;
			}
			else 
			{
				cout << bottom -> information << endl;
				bottom = bottom -> right;
			}
		}
		else
		{
			tree[top].link = bottom;
		}
	}
}
void PostOrder()												 // declare postorder
{
	int top = 0;
	bottom = tree_data;
	bool finish = false;

	while (!finish)
	{
		if ((bottom -> right == NULL) && (finish = false))
		{
			bottom = bottom -> right;
			cout << bottom -> information << endl;
			top ++;
			bottom = tree[top].link;
		}
		else if ((bottom -> right == NULL) && (finish = false))
		{
			cout << bottom -> information << endl;
			bottom = bottom -> left;
			tree[top].link = bottom;
			top++;
			bottom = tree[top].link;
		}
		else 
		{
			cout << bottom -> information << endl;
			finish = true;
			top--;
			bottom = tree[top].link;
		}
	}
}
int main(stack,treenode)									   // the main function
{
	


			PreOrder_Store(); 
			cout << "PreOrder Traversal: " << endl;
			PreOrder(); 
			cout << "InOrder Traversal: " << endl;
			Inorder(); 
			cout << "PostOrder Traversal: " << endl;
			PostOrder();
			system("pause");
			return 0;
}




Is This A Good Question/Topic? 0
  • +

Replies To: Need help with pointer based trees

#2 KYA  Icon User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3196
  • View blog
  • Posts: 19,226
  • Joined: 14-September 07

Re: Need help with pointer based trees

Posted 19 October 2009 - 03:47 PM

No one is going to dig through all that without an inkling of what to look for.

Is the output not correct? Does it seg fault? so on and so forth....
Was This Post Helpful? 0
  • +
  • -

#3 Anum22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 16-September 09

Re: Need help with pointer based trees

Posted 20 October 2009 - 04:00 PM

View PostKYA, on 19 Oct, 2009 - 02:47 PM, said:

No one is going to dig through all that without an inkling of what to look for.

Is the output not correct? Does it seg fault? so on and so forth....

The output is correct to an extent but then the program breaks and there is an error
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1