Beispiel #1
0
int main()
{
	BSTree<int,int> bst;
	bst.Insert_NR(1, 1);
	bst.Insert_NR(2, 2);
	bst.Insert_NR(3, 3);
	bst.Insert_NR(4, 4);
	bst.Insert_NR(6, 6);
	bst.InOrder();
	
	bst.Delete_NR(6);
	bst.Delete_NR(2);
	bst.Delete_NR(3);
	bst.Delete_NR(4);
	bst.InOrder();
	

	bst.Insert_R(1, 1);
	bst.Insert_R(2, 2);
	bst.Insert_R(3, 3);
	bst.Insert_R(4, 4);
	bst.Insert_R(6, 6);
	bst.InOrder();
	
	bst.Delete_R(6);
	bst.Delete_R(2);
	bst.Delete_R(3);
	bst.Delete_R(4);
	bst.InOrder();
	
	return 0;
}
int main(int args,char* argv[]){


	BSTree * myTree = new BSTree;
	myTree->Insert(80);
	myTree->Insert(43);
	myTree->Insert(21);
	myTree->Insert(42);
	myTree->Insert(20);
	myTree->Insert(50);
	myTree->Insert(6);
	myTree->Insert(74);
	myTree->Insert(12);
	myTree->Insert(78);
	myTree->Insert(52);

	myTree->InOrder();
	cout << endl;
	myTree->PostOrder();
	cout << endl;
	myTree->PreOrder();
	cin.get();
	return 0;
}
// For testing (DO NOT ALTER)
void UnitTest() {
  string temp = "This unit test will test some of your code:\n";
  cout << temp << string(temp.length() - 1, '-') << endl;
  cout << "Total Number of Tests: 67" << endl;
  // Tests
  BSTree tree;
  string actual = "";

  Test(tree.Remove(1) == false, "Default Constructor / Remove(1)");
  Test(
      tree.Insert(50) == true && tree.Insert(50) == false
          && tree.Insert(25) == true && tree.Insert(25) == false
          && tree.Insert(75) == true && tree.Insert(75) == false
          && tree.Insert(30) == true && tree.Insert(30) == false
          && tree.Insert(29) == true && tree.Insert(29) == false
          && tree.Insert(31) == true && tree.Insert(31) == false
          && tree.Insert(32) == true && tree.Insert(32) == false
          && tree.Insert(33) == true && tree.Insert(33) == false
          && tree.Insert(34) == true && tree.Insert(34) == false,
      "Insert Stress Test");
  Test(tree.GetSize() == 9, "GetSize()");
  actual = "25 29 30 31 32 33 34 50 75 ";
  Test(tree.FindMin() == 25, "FindMin()");
  Test(tree.InOrder() == actual, "InOrder()", tree.InOrder(), actual);

  tree.Clear();
  Test(tree.GetSize() == 0, "Clear() / GetSize()");
  Test(tree.FindMin() == 0, "FindMin()");

  Test(tree.Insert(10) == true, "Insert(10)");
  Test(tree.GetSize() == 1, "GetSize()");
  actual = "10 ";
  Test(tree.InOrder() == actual, "InOrder()", tree.InOrder(), actual);
  Test(tree.Remove(10) == true, "Remove(10)");
  Test(tree.GetSize() == 0, "GetSize()");
  actual = "";
  Test(tree.InOrder() == actual, "InOrder()", tree.InOrder(), actual);

  Test(
      tree.Insert(10) == true && tree.Insert(5) == true
          && tree.Insert(15) == true,
      "Insert(10), Insert(5), Insert(15)");
  Test(tree.GetSize() == 3, "GetSize()");
  Test(tree.FindMin() == 5, "FindMin()");
  actual = "5 10 15 ";
  Test(tree.InOrder() == actual, "InOrder()", tree.InOrder(), actual);

  Test(tree.Remove(1) == false, "Remove(1)");
  Test(tree.Remove(5) == true, "Remove(5)");
  Test(tree.GetSize() == 2, "GetSize()");
  Test(tree.FindMin() == 10, "FindMin()");
  actual = "10 15 ";
  Test(tree.InOrder() == actual, "InOrder()", tree.InOrder(), actual);

  Test(tree.Insert(5) == true, "Insert(5)");
  Test(tree.GetSize() == 3, "GetSize()");
  Test(tree.FindMin() == 5, "FindMin()");
  actual = "5 10 15 ";
  Test(tree.InOrder() == actual, "InOrder()", tree.InOrder(), actual);

  Test(tree.Remove(15) == true, "Remove(15)");
  Test(tree.GetSize() == 2, "GetSize()");
  Test(tree.FindMin() == 5, "FindMin()");
  actual = "5 10 ";
  Test(tree.InOrder() == actual, "InOrder()", tree.InOrder(), actual);

  Test(tree.Insert(15) == true, "Insert(15)");
  Test(tree.GetSize() == 3, "GetSize()");
  Test(tree.FindMin() == 5, "FindMin()");
  actual = "5 10 15 ";
  Test(tree.InOrder() == actual, "InOrder()", tree.InOrder(), actual);

  Test(tree.Remove(10) == true, "Remove(10)");
  Test(tree.GetSize() == 2, "GetSize()");
  Test(tree.FindMin() == 5, "FindMin()");
  actual = "5 15 ";
  Test(tree.InOrder() == actual, "InOrder()", tree.InOrder(), actual);

  Test(tree.Insert(3) == true, "Insert(3)");
  Test(tree.GetSize() == 3, "GetSize()");
  Test(tree.FindMin() == 3, "FindMin()");
  actual = "3 5 15 ";
  Test(tree.InOrder() == actual, "InOrder()", tree.InOrder(), actual);


  Test(tree.Remove(15) == true, "Remove(15)");
  Test(tree.GetSize() == 2, "GetSize()");
  Test(tree.FindMin() == 3, "FindMin()");
  actual = "3 5 ";
  Test(tree.InOrder() == actual, "InOrder()", tree.InOrder(), actual);

  Test(tree.Insert(10) == true, "Insert(10)");
  Test(tree.GetSize() == 3, "GetSize()");
  Test(tree.FindMin() == 3, "FindMin()");
  actual = "3 5 10 ";
  Test(tree.InOrder() == actual, "InOrder()", tree.InOrder(), actual);

  Test(tree.Remove(1) == false, "Remove(1)");
  Test(tree.Remove(100) == false, "Remove(100)");
  Test(tree.Remove(4) == false, "Remove(4)");
  Test(tree.Remove(7) == false, "Remove(7)");

  Test(tree.Remove(5) == true, "Remove(5)");
  Test(tree.GetSize() == 2, "GetSize()");
  Test(tree.FindMin() == 3, "FindMin()");
  actual = "3 10 ";
  Test(tree.InOrder() == actual, "InOrder()", tree.InOrder(), actual);

  Test(tree.Remove(10) == true, "Remove(10)");
  Test(tree.GetSize() == 1, "GetSize()");
  Test(tree.FindMin() == 3, "FindMin()");
  actual = "3 ";
  Test(tree.InOrder() == actual, "InOrder()", tree.InOrder(), actual);

  Test(tree.Remove(3) == true, "Remove(10)");
  Test(tree.GetSize() == 0, "GetSize()");
  Test(tree.FindMin() == 0, "FindMin()");
  actual = "";
  Test(tree.InOrder() == actual, "InOrder()", tree.InOrder(), actual);

  Test(tree.Remove(1) == false, "Remove(1)");

  cout << string(temp.length() - 1, '-') << endl;
  cout << "Unit Test Complete!\n" << "Passed: " << ut_passed << " / "
       << ut_total << endl << "Failed: " << ut_failed << " / " << ut_total
       << endl << endl;
}