3 Replies - 163 Views - Last Post: 13 October 2019 - 10:58 AM Rate Topic: -----

#1 Comedian   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 21-September 19

How do I get this program to stop looping when a user selects a choice

Posted 13 October 2019 - 10:29 AM

#include <iostream>
#include <string>
using namespace std;

class Node
{
public:
	string data;
	Node * left;
	Node * right;
	Node(string newData)
	{
		data = newData;
		left = NULL;
		right = NULL;
	}
};

class StringTree 
{
private:
	Node * root;

	void destroyTree(Node * &node)
	{
		if (node != NULL)
		{
			destroyTree(node->left);
			destroyTree(node->right);
			delete  node;
		}
	}
	
	void insert(string newData, Node * &node)
	{
		if (node == NULL)
		{			
			node = new Node(newData);			
		} else if (newData < node->data)
		{
			if (node->left != NULL)	insert(newData, node->left);
			else
			{
				node->left = new Node(newData);				
			}
		} else if (newData > node->data)
		{
			if (node->right != NULL) insert(newData, node->right);
			else
			{
				node->right = new Node(newData);				
			}
		}
		else 
		{
			cout << newData << " already exists in the tree !" << endl;
		}
	}
	
	void inOrder(Node * node)
	{
		if (node != NULL)
		{
			inOrder(node->left);
			cout << node->data<<endl;
			inOrder(node->right);
		}		
	}
	
	void preOrder(Node * node)
	{
		if (node != NULL)
		{
			cout << node->data << endl;
			inOrder(node->left);			
			inOrder(node->right);
		}
	}
	
	void postOrder(Node * node)
	{
		if (node != NULL)
		{			
			inOrder(node->left);
			inOrder(node->right);
			cout << node->data << endl;
		}
	}
	
	Node * search(string searchData, Node * node)
	{		
		if (node == NULL || node->data == searchData) return node;
		if (searchData < node->data) return search(searchData, node->left);
		return search(searchData, node->right);
	}
	
	Node * remove(string searchData, Node * node)
	{
		if (node == NULL) return NULL;

		if (searchData < node->data) 
		{			
			node->left = remove(searchData, node->left);
		}
		else if (searchData > node->data) 
		{			
			node->right = remove(searchData, node->right);
		}
		else 
		{
			// node with no children
			if (node->left == NULL && node->right == NULL) 
			{				
				delete node;
				return NULL;
			}
			else if (node->left == NULL) {
				// node with one left child	
				Node * tmp = node->right;
				delete node;
				return tmp;
			}
			else if (node->right == NULL) {
				// node with one right child						
				Node * tmp = node->left;
				delete node;
				return tmp;
			}
			else {
				// nodes with two 2 ore more children
				// search for min data in right sub tree
				Node * tmp = minValue(node->right);
				node->data = tmp->data;				
				node->right = remove(tmp->data, node->right);
			}
		}

		return node;
	}
	
	Node * minValue(Node * node) 
	{
		if (node->left != NULL) 
		{
			return minValue(node->left);
		}
		return node;
	}
public:
	StringTree() 
	{
		root = NULL;
	}
	
	~StringTree()
	{
		destroyTree(root);
	}
	
	void insert(string newData)
	{		
		insert(newData, root);
	}
	
	void inOrder()
	{		
		cout << "InOrder Traversal" << endl;
		inOrder(root);
		cout << endl;
	}
	
	void preOrder()
	{
		cout << "PreOrder Traversal" << endl;
		preOrder(root); 
		cout << endl;
	}
	
	void postOrder()
	{
		cout << "PostOrder Traversal" << endl;
		postOrder(root);
		cout << endl;
	}
	
	bool search(string searchData)
	{
		return search(searchData, root) != NULL;
	}
	
	void remove(string searchData)
	{
		root = remove(searchData, root);
	}
/*	
	void write(Node* n)
	{
   		fstream New("BST tree");
   		write_node(n, New);
   		New.close();
	}

	void write_node(Node* n, fstream& file)
	{
   		if(!n) return;
   		write_node(n->left, file);
   		file << n*>data;
   		write_node(n->right, file);
	}
*/
};

int main() 
{	
	StringTree s;
	const int n = 9;
	int userChoice;
	
	string names[n] = { "Jim","John","Jane","Lane","Bob","Tim","Sarah", 
		"Celena", "Bill"};
	
	cout << "Welcome to my BST program, your options are as follows: " << endl;
	cout << "Show premade tree: 1" << endl;
	cout << "Show premade tree in inorder: 2" << endl;
	cout << "Show premade tree in preorder: 3" << endl;
	cout << "Show premade tree in postorder: 4" << endl;
	cout << "Search string in tree: 5" << endl;
	cout << "Remove string in premade tree: 6" << endl;
	cout << "Save premade string in file: 7" << endl;
	cin >> userChoice;
	
	do
	{
		for (int i = 0; i < n; i++)
		{
			cout << "Inserting " << names[i] << endl;
			s.insert(names[i]);
		}
	}while(userChoice = 1);
	
	do
	{
		s.inOrder();
	}while(userChoice = 2);
	
	do
	{
		s.preOrder();
	}while(userChoice = 3);
	
	do
	{
		s.postOrder();
	}while(userChoice = 4);

	do
	{
		cout << "Is Bob in the tree: " << boolalpha << s.search("Bob") << endl;
		cout << "Is Will in the tree: " << boolalpha << s.search("Will") << endl;
	}while(userChoice = 5);
	
	do
	{
		cout << endl << "Removing Bob from tree" << endl << endl;
		s.remove("Bob");
		s.inOrder();
	}while(userChoice = 6);
	
	/*
	do{
		cout <<  << endl;
	}while(userChoice = 7)
	*/
	return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: How do I get this program to stop looping when a user selects a choice

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15359
  • View blog
  • Posts: 61,599
  • Joined: 12-June 08

Re: How do I get this program to stop looping when a user selects a choice

Posted 13 October 2019 - 10:37 AM

What part is looping?
What is your input?
Was This Post Helpful? 0
  • +
  • -

#3 Comedian   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 21-September 19

Re: How do I get this program to stop looping when a user selects a choice

Posted 13 October 2019 - 10:53 AM

Actually i just ran the program again and noticed its not even spitting out the right output. So the user is supposed to input 1-7 and the program is supposed to call the function that is in the do while loop
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15359
  • View blog
  • Posts: 61,599
  • Joined: 12-June 08

Re: How do I get this program to stop looping when a user selects a choice

Posted 13 October 2019 - 10:58 AM

I would highly suggest _NOT_ using DOWHILE loops as if they were IF statements. Use IF statements.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1