Exemplo n.º 1
0
int main()
{
	BST bst;
	
	if(bst.Empty())
		cout << "BST::Empty() works" << endl;
	else
		cout << "BST::Empty() doesn't work" << endl;
		
	bst.Insert(0);
	
	if(!bst.Empty())
		cout << "BST::Empty() works" << endl;
	else
		cout << "BST::Empty() doesn't work" << endl;
		
	if(bst.Delete(0))
		cout << "BST::Delete(const int) works" << endl;
	else
		cout << "BST::Delete(const int) doesn't work" << endl;
		
	if(!bst.Delete(-1))
		cout << "BST::Delete(const int) works" << endl;
	else
		cout << "BST::Delete(const int) doesn't work" << endl;
	
	for(int i = 0; i < 10; i++)
		bst.Insert(i);
		
	cout << bst.Min() << endl;
	cout << bst.Max() << endl;
	
	if(bst.SearchIter(0) && !bst.SearchIter(10))
		cout << "BST::SearchIter(const int) works" << endl;
	else
		cout << "BST::SearchIter(const int) doesn't work" << endl;
		
	if(bst.SearchRec(0) && !bst.SearchRec(10))
		cout << "BST::SearchRec(const int) works" << endl;
	else
		cout << "BST::SearchRec(const int) doesn't work" << endl;
		
	bst.Clear();
	
	if(bst.Empty())
		cout << "BST::Empty() works" << endl;
	else
		cout << "BST::Empty() doesn't work" << endl;

	return 0;

} // end main()
Exemplo n.º 2
0
int main(){

	BST testBST;
	BST testAssign;
	BNode* temp;
	int data;

	PrintMenu();
	cout << "-->";
	char choice;
	cin >> choice;

	while(choice != 'q' && choice != 'Q'){

		if (choice == '!'){
			BST testCopy(testBST);
			cout << "Result:" << "Print New Copy" << endl;
			testCopy.PrintIn(cout); cout << endl;
			testCopy.Insert(-10000);
			cout << "Result: " << "Print Modified Copy" << endl;
			testCopy.PrintIn(cout); cout << endl;
			cout << "Result: " << "Print Original Test List" << endl;
			testBST.PrintIn(cout); cout << endl;
		}
		else{
			switch (choice){
				case 'h':
				case 'H': PrintMenu(); break;
				case '+':
					cin >> data;
					testBST.Insert(data);
					break;
				case '-':
					cin >> data;
					testBST.Remove(data);
					break;

				case '@':
					cout <<"Result:";
					temp = testBST.AtCursor();
					if (temp)
						cout << temp->GetData() << endl;
					else
						cout << "NULL POINTER" << endl;
					break;
				case '<':
					testBST.GoToBeginning();
					break;
				case '>':
					testBST.GoToEnd();
					break;
				case 'n':
				case 'N':
					testBST.GoToNext();
					break;
				case 'p':
				case 'P':
					testBST.GoToPrev();
					break;
				case 'c':
				case 'C':
					testBST.ClearList();
					break;

				case 'e':
				case 'E':
					if (testBST.Empty())
						cout <<"Result:" <<  "List Is Empty" << endl;
					else
						cout <<"Result:" <<  "List is Not Empty" << endl;
					break;
				case '#':
					//assign list
					testAssign = testBST;
					testAssign.Insert(-100000);
					cout << "Modify New List" << endl;
					testAssign.PrintIn(cout); cout << endl;
					cout << "Old List should not be affected" << endl;
					testBST.PrintIn(cout); cout << endl;
					testAssign.~BST();
					cout << "Destroy New List" << endl;
					cout << "Old List should not be affected" << endl;
					testBST.PrintIn(cout); cout << endl;
					break;
				case '?':
					cin >> data;
					if (testBST.Find(data) != NULL)
						cout << "Result:" << data << "\tfound" << endl;
					else
						cout << "Result:" << data << "\tnot found" << endl;
					break;
				case 'i':
				case 'I':
					cout << "Print INORDER" << endl;
					testBST.PrintIn(cout); cout << endl;
					break;
				case 'r':
				case 'R':
					cout << "Print PREORDER" << endl;
					testBST.PrintPre(cout); cout << endl;
					break;
				case 'o':
				case 'O':
					cout << "Print POSTORDER" << endl;
					testBST.PrintPost(cout); cout << endl;
					break;
				default:
					cout << "INVALID CHOICE, Choose Again" << endl;
			}

		}
		testBST.PrintIn(cout); cout << endl;
		cout << "-->";
		cin >> choice;
	}

	return 0;
}
Exemplo n.º 3
0
int main() {

  BST testBST;
  cout << "inserting 20\n";
  testBST.Insert(20);
  cout << "inserting 15\n";
  testBST.Insert(15);
  cout << "inserting 30\n";
  testBST.Insert(30);
  cout << "inserting 5\n";
  testBST.Insert(5);
  cout << "inserting 18\n";
  testBST.Insert(18);
  cout << "inserting 40\n";
  testBST.Insert(40);
  cout << "inserting 25\n";
  testBST.Insert(25);

  cout << "calling PrintIn\n";
  testBST.PrintIn(cout);
  cout << "calling PrintPost\n";
  testBST.PrintPost(cout);
  cout << "calling PrintPre\n";
  testBST.PrintPre(cout);

  cout << "calling Find(4)\n";
  BNode * found = testBST.Find(4);
  cout << "found " << (*found).GetData() << endl;

  testBST.ClearList();
  cout << "Cleared list, is it empty? " << testBST.Empty() << endl;
  testBST.Insert(5);
  testBST.Insert(6);
  cout << "Should return 5? " << testBST.getParent(testBST.Find(6))->GetData() << endl;

  testBST.PrintIn(cout);
  // test Remove with zero children
  testBST.Remove(6);
  cout << "Removed 6\n";
  testBST.PrintIn(cout);
  testBST.Remove(5);
  cout << "Removed 5\n";
  testBST.PrintIn(cout);

  cout << "clearing List for further tests\n";
  testBST.ClearList();
  testBST.Insert(5);
  testBST.Insert(6);
  testBST.Insert(7);
  testBST.PrintIn(cout);
  // test Remove with one child
  testBST.Remove(6);
  cout << "Removed 6\n";
  testBST.PrintIn(cout);
  cout << "5 should now link to 7\n";

  // test remove with two children
  cout << "Clearing list\n";
  testBST.ClearList();
  testBST.Insert(20);
  testBST.Insert(15);
  testBST.Insert(30);
  testBST.Insert(5);
  testBST.Insert(18);
  testBST.Insert(40);
  testBST.Insert(25);
  cout << "Tree is now:\n";
  testBST.PrintIn(cout);
  cout << "Removing 30\n";
  testBST.Remove(30);
}