Its my 1st time posting here... Do tell me if I am doing anything wrong.

cout << "Please specify a question that has a yes answer for your object and a no answer for my guess:" << endl;
	getline(cin, qtn);
	foundyq = qtn.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ  ");
	while(int(foundyq) != -1)
	{
		if(noTry != 0 && noTry != 1)
		{
			cout << "This is not a question. Re-type the question and add a question mark at the end." << endl;
			cout << "Please specify a question that has a yes answer for your object and a no answer for my guess:" << endl;
			cin >> qtn;
			foundyq = qtn.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ");
		}
		else if(noTry == 1)
		{
			cin >> qtn;
			foundyq = qtn.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ");
		}
		else
		{
			cout << "This is not a question. Re-type the question and add a question mark at the end." << endl;
			cout << "Please specify a question that has a yes answer for your object and a no answer for my guess:" << endl;
		}
		noTry++;
	}
	noTry = 0;

	gbst.replace(nInfo, qtn);
	gbst.inorderTraversal();
	system ("PAUSE");

Basically, the codes I show above is to ask user to input a question which will replace a leave in my binary search tree then display in orderly.

My problem here is that after displaying "Please specify a question that has a yes answer for your object and a no answer for my guess:" , the program does not allow the user to input a question.

Results of the codes above:

Please specify a question that has a yes answer for your object and a no answer for my guess:
Book
Is it a living thing?
Human

I want the results to be like this instead:

Please specify a question that has a yes answer for your object and a no answer for my guess:
Is it an animal?
Book
Is it a living thing?
Is it an animal?
VernonDozier commented: Used code tags on first post! +22

Recommended Answers

All 10 Replies

Added a while loop and it works but now but because of the while loop, "Please specify a question that has a yes answer for your object and a no answer for my guess:" output is repeated. Ignoring that, I continued coding and encountered a runtime error that I have no idea how to solve. Desperately need to get it done soon so any help is greatly appreciated.

Error:
'BinaryTreeGame.exe': Loaded 'C:\Documents and Settings\Charissa\My Documents\Visual Studio 2008\Projects\BinaryTreeGame\Debug\BinaryTreeGame.exe', Symbols loaded.
'BinaryTreeGame.exe': Loaded 'C:\WINDOWS\system32\ntdll.dll'
'BinaryTreeGame.exe': Loaded 'C:\WINDOWS\system32\kernel32.dll'
'BinaryTreeGame.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.DebugCRT_1fc8b3b9a1e18e3b_9.0.30729.1_x-ww_f863c71f\msvcp90d.dll'
'BinaryTreeGame.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.DebugCRT_1fc8b3b9a1e18e3b_9.0.30729.1_x-ww_f863c71f\msvcr90d.dll'
First-chance exception at 0x104981ee in BinaryTreeGame.exe: 0xC0000005: Access violation reading location 0xcdcdcde5.
Unhandled exception at 0x104981ee in BinaryTreeGame.exe: 0xC0000005: Access violation reading location 0xcdcdcde5.
The program '[5816] BinaryTreeGame.exe: Native' has exited with code 0 (0x0).

I'm not sure where exactly the problem is so here's all my codes except for BinaryTree.h

BinarySearchTree.h

//Header File Binary Search Tree

#ifndef H_binarySearchTree
#define H_binarySearchTree
#include <iostream>
#include <cassert>
#include "BinaryTree.h"

using namespace std;

template<class elemType>
class BinarySearchTree: public BinaryTree<elemType>
{
public:
    bool search(const elemType& searchItem);

	bool searchByIndex(const elemType& searchItem, vector<string> &data);

	void replace(const elemType& searchItem, string qtn, vector<string> &data);

	string getLNode(const elemType& searchItem);

	string getRNode(const elemType& searchItem);

    void insert(const elemType& insertItem);

	void insert(const elemType& insertItem, string direct);

	void insert(const elemType& insertItem1,const elemType& insertItem2 , vector<string> data);

    void deleteNode(const elemType& deleteItem);

private:
    void deleteFromTree(nodeType<elemType>* &p);
};

template<class elemType>
bool BinarySearchTree<elemType>::searchByIndex(const elemType& searchItem, vector<string> &data)
{
    nodeType<elemType> *current;
    bool found = false;
	int searchItemIndex, currentItemIndex;

	for (size_t i = 0; i < data.size(); ++i)
	{
		if(searchItem == data[i])
			searchItemIndex = i;
	}

    if(root == NULL)
       cerr<<"Cannot search the empty tree."<<endl;
    else
    { 
       current = root;

       while(current != NULL && !found)
       {
             if(current->info == searchItem)
				 found = true;
              else
				  for (size_t i = 0; i < data.size(); ++i)
				  {
					  if(current->info == data[i])
						  currentItemIndex = i;
				  }
                  if(currentItemIndex > searchItemIndex)
                     current = current->llink;
                  else
                     current = current->rlink;
       }//end while
    }//end else

    return found;
}//end search


template<class elemType>
void BinarySearchTree<elemType>::replace(const elemType& searchItem, string qtn, vector<string> &data)
{
    nodeType<elemType> *current;
    bool found = false;
	int searchItemIndex, currentItemIndex;

	for (size_t i = 0; i < data.size(); ++i)
	{
		if(searchItem == data[i])
		{
			searchItemIndex = i;
		}
	}

    if(root == NULL)
       cerr<<"Tree is empty, nothing to replace."<<endl;
    else
    { 
       current = root;

       while(current != NULL && !found)
       {
             if(current->info == searchItem)
			 {
				 current->info = qtn;
				 data[searchItemIndex] = qtn;
			 }
              else
				  for (size_t i = 0; i < data.size(); i++)
				  {
					  if(current->info == data[i])
					  {
						  currentItemIndex = i;
					  }
				  }
                  if(currentItemIndex > searchItemIndex)
				  {
                     current = current->llink;
				  }
                  else
                     current = current->rlink;
       }//end while
    }//end else
}//end replaace

template<class elemType>
string BinarySearchTree<elemType>::getLNode(const elemType& searchItem)
{
    nodeType<elemType> *current;
    bool found = false;
	string nodeInfo = "";

    if(root == NULL)
       cerr<<"Cannot search the empty tree."<<endl;
    else
    { 
       current = root;

       while(current != NULL && !found)
       {
             if(current->info == searchItem)
			 {
				 current = current->llink;
				 nodeInfo = current->info;
				 found = true;
			 }
              else
                  if(current->info > searchItem)
                     current = current->llink;
                  else
                     current = current->rlink;
       }//end while
    }//end else

    return nodeInfo;
}//end getLNode

template<class elemType>
string BinarySearchTree<elemType>::getRNode(const elemType& searchItem)
{
    nodeType<elemType> *current;
    bool found = false;
	string nodeInfo = "";

    if(root == NULL)
       cerr<<"Cannot search the empty tree."<<endl;
    else
    { 
       current = root;

       while(current != NULL && !found)
       {
             if(current->info == searchItem)
			 {
				 current = current->rlink;
				 nodeInfo = current->info;
				 found = true;
			 }
              else
                  if(current->info > searchItem)
                     current = current->llink;
                  else
                     current = current->rlink;
       }//end while
    }//end else

    return nodeInfo;
}//end getRNode

template<class elemType>
void BinarySearchTree<elemType>::insert(const elemType& insertItem)
{
    nodeType<elemType> *current;  //pointer to traverse the tree
    nodeType<elemType> *trailCurrent; //pointer behind current
    nodeType<elemType> *newNode;  //pointer to create the node

    newNode = new nodeType<elemType>;
    assert(newNode != NULL);
    newNode->info = insertItem;
    newNode->llink = NULL;
    newNode->rlink = NULL;

	if(root == NULL)
	{
       root = newNode;
	}
    else
    {
       current = root;
 
       while(current != NULL)
       {
           trailCurrent = current;

           if(current->info == insertItem)
           {
              cerr<<"The insert item is already in the list -- ";
              cerr<<"duplicates are not allowed."<<endl;
              return;
           }
           else
		   {
			   
              if(current->info > insertItem)
			  {
                 current = current->llink;
			  }
              else
			  {
                 current = current->rlink;
			  }
		   }

       }//end while

       if(trailCurrent->info > insertItem)
	   {
          trailCurrent->llink = newNode;
	   }
       else
	   {
          trailCurrent->rlink = newNode;
	   }
   }
}//end insert

template<class elemType>
void BinarySearchTree<elemType>::insert(const elemType& insertItem, string direct)
{
    nodeType<elemType> *current;  //pointer to traverse the tree
    nodeType<elemType> *trailCurrent; //pointer behind current
    nodeType<elemType> *newNode;  //pointer to create the node

    newNode = new nodeType<elemType>;
    assert(newNode != NULL);
    newNode->info = insertItem;
    newNode->llink = NULL;
    newNode->rlink = NULL;

	if(root == NULL)
	{
       root = newNode;
	}
    else
    {
       current = root;
 
       while(current != NULL)
       {
           trailCurrent = current;

           if(current->info == insertItem)
           {
              cerr<<"The insert item is already in the list -- ";
              cerr<<"duplicates are not allowed."<<endl;
              return;
           }
           else
		   {
              if(direct == "left")
			  {
                 current = current->llink;
			  }
              else
			  {
                 current = current->rlink;
			  }
		   }

       }//end while

       if(direct == "left")
	   {
          trailCurrent->llink = newNode;
	   }
       else
	   {
          trailCurrent->rlink = newNode;
	   }
   }
}//end insert

template<class elemType>
void BinarySearchTree<elemType>::insert(const elemType& insertItem1,const elemType& insertItem2 , vector<string> data)
{
    nodeType<elemType> *current;  //pointer to traverse the tree
    nodeType<elemType> *trailCurrent; //pointer behind current
    nodeType<elemType> *newNode1;  //pointer to create the node
	nodeType<elemType> *newNode2;

    newNode1 = new nodeType<elemType>;
	newNode2 = new nodeType<elemType>;
    assert(newNode1 != NULL);
	assert(newNode2 != NULL);
    newNode1->info = insertItem1;
	newNode2->info = insertItem2;

	string findItem = "";
	int findItemIndex, currentItemIndex;

	for (size_t i = 0; i < data.size(); ++i)
	{
		if(insertItem1 == data[i])
		{
			findItemIndex = i+1;
			findItem = data[i+1];
		}
	}
	
	current = root;
	
	while(current != NULL)
	{
		trailCurrent = current;
		
		if(current->info == findItem)
		{
			cerr<<"The insert item is already in the list -- ";
			cerr<<"duplicates are not allowed."<<endl;
			return;
		}
		else
		{
			for (size_t i = 0; i < data.size(); ++i)
			{
				if(current->info == data[i])
					currentItemIndex = i;
			}
			
			if(currentItemIndex > findItemIndex)
			{
				current = current->llink;
			}
			else
			{
				current = current->rlink;
			}
		}
	}//end while
	
		trailCurrent->llink = newNode1;
		trailCurrent->rlink = newNode2;

}//end insert
#endif
#include<iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>

using namespace std;

#include "BinaryTree.h"
#include "BinarySearchTree.h"

int main()
{
	vector<string> dataList;
	BinarySearchTree<string> gbst;
	string nInfo = "Is it a living thing?", obj = "", qtn = "";
	char userAns = 'y';
	int qtnNo = 1, noTry = 0, foundIndex;
	size_t found, foundObj, foundyq;

	//insert into vector
	dataList.push_back("book");
	dataList.push_back("Is it a living thing?");
	dataList.push_back("human");

	//insert into binary tree
	gbst.insert("Is it a living thing?");
	gbst.insert("human" , "right");
	gbst.insert("book" , "left");

	// display game 1st question
	cout << "You must think of a single object. \nPlease answer the following questions about this object." << endl;
	cout << "QUESTION: " << nInfo << " (Y or N): "; 

	found = nInfo.find_first_not_of("abcdefghijklmnopqrstuvwxyzI ");
	while(int(found) != -1)
	{
		cin >> userAns;
		switch (userAns)
		{
			cout << "QUESTION: " << nInfo << " (Y or N): " << endl;
			case 'y': case 'Y':
				if(qtnNo = 1)
				{
					nInfo = gbst.getRNode("Is it a living thing?");
					qtnNo++;
				}
				else
				{
					nInfo = gbst.getRNode(nInfo);
				}
				break;

			case 'n': case 'N':
				if(qtnNo = 1)
				{
					nInfo = gbst.getLNode("Is it a living thing?");
					qtnNo++;
				}
				else
				{
					nInfo = gbst.getLNode(nInfo);
				}
				break;

			default : 
				cout<<"\nPlease only enter 'y' or 'n'";
		}

	found = nInfo.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ");
	}
	cout << "I guess that you object is a(n) " << nInfo << " (Y or N): ";
	cin >> userAns;

	switch (userAns)
	{
		case 'y': case 'Y':
			cout << "Notice the superior intellect of the computer!" << endl;
			break;

		case 'n': case 'N':
			cout << "What object were you thinking of? ";
			cin >> obj;
			foundObj = obj.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ");

			while(int(foundObj) != -1)
			{
				if(noTry != 0 && noTry != 1)
				{
					cout << "No symbols please." << endl;
					cout << "What object were you thinking of? ";
					cin >> obj;
					foundObj = obj.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ");
				}
				else if(noTry == 1)
				{
					cin >> obj;
					foundObj = obj.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ");
				}
				else
				{
					cout << "No symbols please." << endl;
					cout << "What object were you thinking of? ";
				}
				
				noTry++;
			}
			noTry = 0;

			while (qtn == "")
			{
				cout << "Please specify a question that has a yes answer for your object and a no answer for my guess:" << endl;
				getline(cin, qtn);
			}

			foundyq = qtn.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ  ");
			while(int(foundyq) == -1)
			{
				if(noTry != 0 && noTry != 1)
				{
					cout << "Re-type the question and add a question mark at the end." << endl;
					cout << "Please specify a question that has a yes answer for your object and a no answer for my guess:" << endl;
					getline(cin, qtn);
					foundyq = qtn.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ");
				}
				else if(noTry == 1)
				{
					getline(cin, qtn);
					foundyq = qtn.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ");
				}
				else
				{
					cout << "Re-type the question and add a question mark at the end." << endl;
					cout << "Please specify a question that has a yes answer for your object and a no answer for my guess:" << endl;
				}
				noTry++;
			}
			noTry = 0;

			for(size_t i = 0; i < dataList.size(); ++i)
			{
				if(dataList[i] == nInfo)
				{
					foundIndex = i;
				}
			}
			
			dataList.insert(dataList.begin()+(foundIndex),nInfo);
			dataList.insert(dataList.begin()+(foundIndex+2),obj);
			
			gbst.insert(nInfo, obj, dataList);
			gbst.replace(nInfo, qtn, dataList);
			for(size_t i = 0; i < dataList.size(); ++i)
			{
				cout << dataList[i] << " ";
			}
			cout<< endl;
			gbst.inorderTraversal();
			//left no ans (nInfo)
			//right yes ans (obj)
			break;

		default : 
				cout<<"\nPlease only enter 'y' or 'n'";
	}
	system ("PAUSE");

	return 0;
}

Looking at your first post, it appears that you have a very common problem when mixing cin and getline. See this thread and see if that's about the behavior you are getting.

http://www.daniweb.com/forums/thread90228.html

When you have a cin statement, followed at some point by a getline statement, the cin statement will leave the '\n' in the input buffer, then the getline will take that '\n' as an answer and thus not pause. Clear the input buffer after the cin statement with this line and you should get the pause for input with getline:

cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );

I didn't really look at your second post because you said you had added something to "fix" your first problem and that caused another problem? Try removing your first attempt at fixing it and putting the above line after all of your cin statements and see if that fixes everything.

Thanks that solve my 1st problem. However, I ran it again, the second error still appear.

We can't compile and run the program unless you post BinaryTree.h. What are the conditions of the run-time error (when does it happen, what input, etc.)?

ok. I found the problem. I did not add "found=true;" in BinarySearchTree.h

template<class elemType>
void BinarySearchTree<elemType>::replace(const elemType& searchItem, string qtn, vector<string> &data)
{
	nodeType<elemType> *current;
	bool found = false;
	int searchItemIndex, currentItemIndex;

	for (size_t i = 0; i < data.size(); ++i)
	{
		if(searchItem == data[i])
		{
			searchItemIndex = i;
		}
	}

	if(root == NULL)
	   cerr<<"Tree is empty, nothing to replace."<<endl;
	else
	{ 
	   current = root;

	   while(current != NULL && !found)
	   {
			 if(current->info == searchItem)
			 {
				 current->info = qtn;
				 data[searchItemIndex] = qtn;
				 found = true; // <--- forgot to add this earlier
			 }
			  else
				  for (size_t i = 0; i < data.size(); i++)
				  {
					  if(current->info == data[i])
					  {
						  currentItemIndex = i;
					  }
				  }
				  if(currentItemIndex > searchItemIndex)
				  {
					 current = current->llink;
				  }
				  else
					 current = current->rlink;
	   }//end while
	}//end else
}//end replaace

Then i run the codes again. Now I got this error in my BinaryTree.h

'BinaryTreeGame.exe': Loaded 'C:\Documents and Settings\Charissa\My Documents\Visual Studio 2008\Projects\BinaryTreeGame\Debug\BinaryTreeGame.exe', Symbols loaded.
'BinaryTreeGame.exe': Loaded 'C:\WINDOWS\system32\ntdll.dll'
'BinaryTreeGame.exe': Loaded 'C:\WINDOWS\system32\kernel32.dll'
'BinaryTreeGame.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.DebugCRT_1fc8b3b9a1e18e3b_9.0.30729.1_x-ww_f863c71f\msvcp90d.dll'
'BinaryTreeGame.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.DebugCRT_1fc8b3b9a1e18e3b_9.0.30729.1_x-ww_f863c71f\msvcr90d.dll'
First-chance exception at 0x004185bc in BinaryTreeGame.exe: 0xC0000005: Access violation reading location 0xcdcdcded.
Unhandled exception at 0x004185bc in BinaryTreeGame.exe: 0xC0000005: Access violation reading location 0xcdcdcded.
The program '[6796] BinaryTreeGame.exe: Native' has exited with code 0 (0x0).

The code stops here in BinaryTree.h

template<class elemType>
void BinaryTree<elemType>::inorder(nodeType<elemType> *p)
{
	if(p != NULL)
	{
		inorder(p->llink);  //<-- there is a yellow arrow pointing here
		cout<<p->info<<" "<<endl;;
		inorder(p->rlink);
	}
}

here is my full BinaryTree.h

//Header File Binary Tree
#ifndef H_binaryTree
#define H_binaryTree

#include <iostream>
#include <string>

using namespace std;

     //Definition of the node
template<class elemType>
struct nodeType
{
   elemType	           info;
   nodeType<elemType>  *llink;
   nodeType<elemType>  *rlink;
};

    //Definition of the class
template <class elemType>
class BinaryTree
{
public:
    const BinaryTree<elemType>& operator=
                 (const BinaryTree<elemType>&); 
      //Overload the assignment operator.
    bool isEmpty();

    void inorderTraversal();

    void preorderTraversal();

    void postorderTraversal();

    int treeHeight();

    int treeNodeCount();

    int treeLeavesCount();

    void destroyTree();

    BinaryTree(const BinaryTree<elemType>& otherTree); 

    BinaryTree();   

    ~BinaryTree();   

protected:
    nodeType<elemType> *root;

private:
    void copyTree(nodeType<elemType>* &copiedTreeRoot,
                  nodeType<elemType>* otherTreeRoot);

    void destroy(nodeType<elemType>* &p);

    void inorder(nodeType<elemType> *p);

    void preorder(nodeType<elemType> *p);

    void postorder(nodeType<elemType> *p);

    int height(nodeType<elemType> *p);

    int max(int x, int y);

    int nodeCount(nodeType<elemType> *p);

    int leavesCount(nodeType<elemType> *p);
};

//Definition of member functions

template<class elemType>
BinaryTree<elemType>::BinaryTree()
{
	root = NULL;
}

template<class elemType>
bool BinaryTree<elemType>::isEmpty()
{
	return (root == NULL);
}

template<class elemType>
void BinaryTree<elemType>::inorderTraversal()
{
	inorder(root);
}

template<class elemType>
void BinaryTree<elemType>::preorderTraversal()
{
	preorder(root);
}

template<class elemType>
void BinaryTree<elemType>::postorderTraversal()
{
	postorder(root);
}

template<class elemType>
int BinaryTree<elemType>::treeHeight()
{
	return height(root);
}

template<class elemType>
int BinaryTree<elemType>::treeNodeCount()
{
	return nodeCount(root);
}

template<class elemType>
int BinaryTree<elemType>::treeLeavesCount()
{
	return leavesCount(root);
}

template <class elemType>
void  BinaryTree<elemType>::copyTree
                      (nodeType<elemType>* &copiedTreeRoot,
		               nodeType<elemType>* otherTreeRoot)
{
	if(otherTreeRoot == NULL)
		copiedTreeRoot = NULL;
	else
	{
		copiedTreeRoot = new nodeType<elemType>;
		copiedTreeRoot->info = otherTreeRoot->info;
		copyTree(copiedTreeRoot->llink, otherTreeRoot->llink);
		copyTree(copiedTreeRoot->rlink, otherTreeRoot->rlink);
	}
} //end copyTree

template<class elemType>
void BinaryTree<elemType>::inorder(nodeType<elemType> *p)
{
	if(p != NULL)
	{
		inorder(p->llink);
		cout<<p->info<<" "<<endl;;
		inorder(p->rlink);
	}
}

template<class elemType>
void BinaryTree<elemType>::preorder(nodeType<elemType> *p)
{
	if(p != NULL)
	{
		cout<<p->info<<" ";
		preorder(p->llink);
		preorder(p->rlink);
	}
}

template<class elemType>
void BinaryTree<elemType>::postorder(nodeType<elemType> *p)
{
	if(p != NULL)
	{
		postorder(p->llink);
		postorder(p->rlink);
		cout<<p->info<<" ";
	}		
}

   //Overload the assignment operator
template<class elemType>
const BinaryTree<elemType>& BinaryTree<elemType>::
           operator=(const BinaryTree<elemType>& otherTree)
{ 
     
	if(this != &otherTree) //avoid self-copy
	{
		if(root != NULL)  //if the binary tree is not empty, 
			              //destroy the binary tree
			destroy(root);

		if(otherTree.root == NULL) //otherTree is empty
			root = NULL;
		else
			copyTree(root, otherTree.root);
	}//end else

   return *this; 
}

template <class elemType>
void  BinaryTree<elemType>::destroy(nodeType<elemType>* &p)
{
	if(p != NULL)
	{
		destroy(p->llink);
		destroy(p->rlink);
		delete p;
		p = NULL;
	}
}

template <class elemType>
void  BinaryTree<elemType>::destroyTree()
{
	destroy(root);
}

	//copy constructor
template <class elemType>
BinaryTree<elemType>::BinaryTree
              (const BinaryTree<elemType>& otherTree)
{
	if(otherTree.root == NULL) //otherTree is empty
		root = NULL;
	else
		copyTree(root, otherTree.root);
}

template <class elemType>
BinaryTree<elemType>::~BinaryTree()
{
	destroy(root);
}

template<class elemType>
int BinaryTree<elemType>::height(nodeType<elemType> *p)
{
	if(p == NULL)
		return 0;
	else
		return 1 + max(height(p->llink), height(p->rlink));
}

template<class elemType>
int BinaryTree<elemType>::max(int x, int y)
{
	if(x >= y)
		return x;
	else
		return y;
}

#endif

input:
y
n
lion
is it an animal?

input:
y
n
lion
is it an animal?

Upload the most recent versions of the three files as a zip attachment. I copied and pasted the code, but you have made changes. It'd be easiest to just upload the most recent version of everything as a zip file so everyone has the most recent versions and there's no guesswork.

> First-chance exception at 0x004185bc in BinaryTreeGame.exe: 0xC0000005: Access violation reading location 0xcdcdcded.
Heap memory (in debug mode) gets filled with 0xcd when you free it.

> inorder(p->llink); //<-- there is a yellow arrow pointing here
p or p->llink is pointing to a node which no longer exists.

Did you miss a p = NULL when deleting a node?

I did not delete any node. Only insert and change the value in 1 node in the replace method.

> if(qtnNo = 1)
Surely == 1

Also, use the debugger.
- Put a breakpoint on the line which generates the segfault.
- Run the code, let the debugger trap the breakpoint
- You can then have a good look round all the data structures and try to figure out what went wrong.
- Put other breakpoints earlier in the code, say single step the insert into BST function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.