Ejemplo n.º 1
0
int main(int argc,char **argv)
{
  // Create an empty Binary Search Tree
  BST Tree;
  string input = "";
  while (input !="ENDINSERT")
  {
     cin >> input;
     if (input != "ENDINSERT")
     	Tree.Insert(input);     
  }
  Tree.Print("POST");
}
Ejemplo n.º 2
0
int main(int argc,char **argv)
{
    // Create an empty Binary Search Tree
    BST Tree;
    string input;
    cin >> input;
    while(input != "ENDINSERT") {
        //cout << "got here" << endl;
        Tree.Insert(input);
        cin >> input;
    }
    //cout << "done inserting" << endl;
    Tree.Print("POST");
}
Ejemplo n.º 3
0
void main() // key(pair사용시 data.first) = 노드의 이름 같은 개념,  element(pair 사용시 data.second)  = 노드의 고유 data
{
	//declare the type of K, E
	typedef  int K;
	typedef char E;
	BST<K, E> HWbst;	// Making BST class HWbst  link every nodes

	int menu = 0;
	while (1)
	{
		//   Menu   //
		cout << endl << " #### Choose Menu ####" << endl;
		cout << "1. Insert      2. Delete      3. Print      4. Exit" << endl;
		cin >> menu;
		switch (menu)
		{
			case 1: // Insert Menu
			{
					  
					  pair<K, E> A;  // new node's pair type data
					  cout << " *** 새로운 TreeNode의 Key : ";
					  cin >> A.first;
					  cout << " *** 새로운 TreeNode의 Element : ";
					  cin >> A.second;

					  HWbst.Insert(A); // use insert function
					  break;
					  
			}
			case 2: // Delete Menu
			{	
					  int a = 0;
					  cout << " *** 삭제할 노드의 Key 입력  :  ";
					  cin >> a;
					  HWbst.Delete(a);
					  break;
			}
			case 3: // Print all nodes by level order
			{
					  HWbst.Print();
					  break;
			}
			case 4:  // close this program
			{
					  return;
			}
		}
	}
	
}
Ejemplo n.º 4
0
int main(int argc,char **argv)
{
  // Create an empty Binary Search Tree
  BST Tree;
  //Tree=new BST();
  string input;
  cin>>input;
  while(input!="ENDINSERT"){
	Tree.Insert(input);
	
	cin>>input;
  }
  Tree.Print("POST");
}
Ejemplo n.º 5
0
int main(int argc,char **argv)
{
  // Create an empty Binary Search Tree
  BST Tree;
  string input;
  cin>>input;
  while(input!="ENDINSERT"){
	Tree.Insert(input);
	
	cin>>input;
  }
  //Tree.Print("POST");
  //cout<<"transition"<<endl;
  cin>>input;
  while(input!="ENDDELETE"){
		Tree.Delete(input);
	  cin>>input;
  }
  Tree.Print("POST");
}
Ejemplo n.º 6
0
int main()
{
	char command;
	my_variant_t input;
	char choice;
	string temp;
	
	QueueType<my_variant_t> queueType;
	BST<my_variant_t> bst = BST<my_variant_t>();

	do
	{
		cout << "What type of tree would you like to build, (S)trings, (N)umbers, or (Q)uit?" << endl;
		cin  >> choice;
		cout << endl;

		if (choice == 'Q' || choice == 'q')
			return 1;

		command = 'M';

		while (command != 'Q' && command != 'q')
		{
			if (command == 'M')
			{
				cout << "What would you like to do:" <<endl;
				cout << "(I)sert node" << endl;
				cout << "(R)emove node" << endl;
				cout << "(S)earch for node" << endl;
				cout << "(G)et root" << endl;
				cout << "(D)isplay tree" << endl;
				cout << "(F)ull tree test" << endl;
				cout << "(C)hop tree down" << endl;
				cout << "(N)ew tree" << endl;
				cout << "(Q)uit program" << endl;
				cout << "? ";
				cin  >> command;
				cout << endl;
			}
			else if (command == 'I' || command == 'i')
			{
				if (choice == 'N' || choice == 'n')
				{
					cin.ignore();
					bool isNumber;
					do
					{
						cout << "What number would you like to input?" << endl;
						getline(cin, temp);

						isNumber = true;
						if (temp.length() == 0)
							isNumber = false;

						for (unsigned int i = 0; i < temp.length(); i++)
						{
							if(!isdigit(temp[i]))
								isNumber = false;
						}

						if (isNumber)
						{
							input = atoi(temp.c_str());
							cout << endl;
							try
							{
								bst.PutItem(input);
								cout << input << " is inputed!" << endl << endl;
							}
							catch(DuplicateItem)
							{
								cout << input << " already exists!" << endl << endl;
							}
						}
						else
							cout << temp << " is not a valid number!" << endl << endl;

					}while(!isNumber);
				}
				else
				{
					cin.ignore();
					bool isEmptyString;
					do
					{
						isEmptyString = false;
						cout << "What is the string you would like to input?" << endl;
						getline(cin, temp);

						if (temp == "")
						{
							isEmptyString = true;
							cout << temp << " is not a valid string!" << endl << endl;
						}

					}while(isEmptyString);

					input = (char*)temp.c_str();
					cout << endl;
					try
					{
						bst.PutItem(input);
						cout << input << " is inputed!" << endl << endl;
					}
					catch(DuplicateItem)
					{
						cout << input << " already exists!" << endl << endl;
					}
				}

				command = 'M';
			}
			else if (command == 'R' || command == 'r')
			{
				if (choice == 'N' || choice == 'n')
				{
					cout << "What number would you like to remove?" << endl;
					cin.ignore();
					getline(cin, temp);
					input = atoi(temp.c_str());
					try
					{
						bst.DeleteItem(input);
						cout << endl << input << " is removed!" << endl << endl;
					}
					catch(NotFound)
					{
						cout << endl << "Number was not found!" << endl << endl;
					}
				}
				else
				{
					cout << "What is the string you would like to remove?" << endl;
					cin.ignore();
					getline(cin, temp);
					input = (char*)temp.c_str();
					try
					{
						bst.DeleteItem(input);
						cout << endl << input << " is removed!" << endl << endl;
					}
					catch(NotFound)
					{
						cout << endl << "String was not found!" << endl << endl;
					}
				}

				command = 'M';
			}
			else if (command == 'D' || command == 'd')
			{
				if (!bst.IsEmpty())
				{
					cout << endl << "Displaying..." << endl;
					bst.Print(cout);
					cout << endl << endl;
				
					bool finished = false;
					bst.ResetTree(IN_ORDER);
					cout << "InOrder: ";
					while(!finished)
						cout << bst.GetNextItem(IN_ORDER, finished) << " ";
				
					finished = false;
					bst.ResetTree(PRE_ORDER);
					cout << endl << "PreOrder: ";
					while(!finished)
						cout << bst.GetNextItem(PRE_ORDER, finished) << " ";
				
					finished = false;
					bst.ResetTree(POST_ORDER);
					cout << endl << "PostOrder: ";
					while(!finished)
						cout << bst.GetNextItem(POST_ORDER, finished) << " ";

					cout << endl << endl;
				}
				else
					cout << "The tree is empty!" << endl << endl;

				command = 'M';
			}
			else if (command == 'C' || command == 'c')
			{
				cout << endl << "Chopping...Timber!" << endl;
				bst.MakeEmpty();
				cout << "Tree is empty!" << endl << endl;

				command = 'M';
			}
			else if (command == 'Q' || command == 'q')
			{
				continue;
			}
			else if (command == 'N' || command == 'n')
			{
				bst.MakeEmpty();
				break;
			}
			else if (command == 'S' || command == 's')
			{
				if (choice == 'N' || choice == 'n')
				{
					cin.ignore();
					bool isNumber;
					do
					{
						cout << "What number would you like to find?" << endl;
						getline(cin, temp);

						isNumber = true;
						if (temp.length() == 0)
							isNumber = false;

						for (unsigned int i = 0; i < temp.length(); i ++)
						{
							if(!isdigit(temp[i]))
								isNumber = false;
						}

						if (isNumber)
						{
							input = atoi(temp.c_str());
							cout << endl;
							
							bool found;
							bst.GetItem(input, found);

							if (found)
								cout << input << " was found!" << endl << endl;
							else
								cout << input << " was not found!" << endl << endl;
						}
						else
							cout << temp << "is not a valid number!" << endl << endl;
					}while(!isNumber);
				}
				else
				{
					cin.ignore();
					bool isEmptyString;
					do
					{
						isEmptyString = false;
						cout << "What is the string you would like to find?" << endl;
						getline(cin, temp);

						if (temp == "")
						{
							isEmptyString = true;
							cout << temp << " is not a valid string!" << endl << endl;
						}
					}while(isEmptyString);

					input = (char*)temp.c_str();
					cout << endl;
					bool found;
					bst.GetItem(input, found);
					
					if (found)
						cout << input << " was found!" << endl << endl;
					else
						cout << input << " was not found!" << endl << endl;
				}

				command = 'M';
			}
			else if (command == 'G' || command == 'g')
			{
				try
				{
					cout << "The root item is : " << bst.RetrieveRoot() << endl << endl;
				}
				catch(EmptyTree)
				{
					cout << "The root does not exist!" << endl << endl;
				}

				command = 'M';
			}
			else if (command == 'F' || command == 'f')
			{
				cout << "Checking..." << endl;
	
				if (bst.IsFullTree())
					cout << "The tree is full!" << endl << endl;
				else
					cout << "The tree is not full!" << endl << endl;

				command = 'M';
			}
			else 
			{
				cout << "Sorry the option you entered is invalid! Please try again." << endl << endl;
				command = 'M';
				cout << endl;
			}
		}