Exemple #1
0
void main(void)
{
   DataType q;
   q.key = 1; q.ID = 'a';
   y.Insert(q);
   q.key = 6; q.ID = 'b';
   y.Insert(q);
   q.key = 4; q.ID = 'c';
   y.Insert(q);
   q.key = 8; q.ID = 'd';
   y.Insert(q);
   cout << "Elements in ascending order are" << endl;
   y.Ascend();
   DataType s;
   y.Delete(4,s);
   cout << "Delete of 4 succeeds " << endl;
   cout << s.key << ' ' << s.ID << endl;
   cout << "Elements in ascending order are" << endl;
   y.Ascend();
   y.Delete(8,s);
   cout << "Delete of 8 succeeds " << endl;
   cout << s.key << ' ' << s.ID << endl;
   cout << "Elements in ascending order are" << endl;
   y.Ascend();
   y.Delete(6,s);
   cout << "Delete of 6 succeeds " << endl;
   cout << s.key << ' ' << s.ID << endl;
   cout << "Elements in ascending order are" << endl;
   y.Ascend();
   try {y.Delete(6,s);}
   catch (BadInput)
      {cout << "Delete of 6 fails " << endl;}
   cout << "Elements in ascending order are" << endl;
   y.Ascend();
}
void TestBSTree()
{	
	// 5 3 4 1 7 8 2 6 0 9
	BSTree<int, double> t;
	t.Insert(5,5);
	t.Insert(3,3);
	t.Insert(4,4);
	t.Insert(1,1);
	t.Insert(7,7);
	t.Insert(8,8);
	t.Insert(2,2);
	t.Insert(6,6);
	t.Insert(0,0);
	t.Insert(9,9);

	t.PrevOrder();

	BSTNode<int,double>* ret = t.Find(9);
	cout<<"Find 9 ?: "<<ret->_key<<endl;
	ret = t.Find(10);
	cout<<"Find 10 ?: "<<ret<<endl;

	t.Remove(8);
	t.Remove(1);
	t.Remove(5);

	t.PrevOrder();
}
int main()
{
	BSTree<int> t;
	
	t.Insert(5);
	t.Insert(3);
	t.Insert(7);
	t.Insert(1);
	t.Insert(4);
	t.Insert(6);
	t.Insert(8);
	t.Insert(0);
	t.Insert(2);
	t.Insert(9);
	
//	t.InOrder();
//
//	t.Remove(0);
//	t.Remove(1);
//	t.Remove(9);
//	t.Remove(7);
//
//	t.InOrder();
//
//	for (int i = 0; i < 10; i++)
//		t.Remove(i);
//	t.InOrder();

//	t.Find(9);
//	for (int i = 0; i <= 10; i++)
//	{
//		BSTNode<int> *ret = t.Find(i);
//		if (NULL != ret)
//			std::cout << ret->_key << std::endl;
//		else
//			std::cout << i << " not found !" << std::endl;
//	}

//	std::cout << t.Height() << std::endl;
//	std::cout << t.LeafNums() << std::endl;

//	t.LevelOrder();
//	t.PrevOrderNonR();
//	t.InOrderNonR();
	t.PostOrderNonR();

	return 0;
}
int main() {
  BSTree tree;
  tree.Insert(8);
  tree.Insert(6);
  tree.Insert(10);
  tree.Insert(5);
  tree.Insert(7);
  tree.Insert(9);
  tree.Insert(11);

  tree.BreadthFirstTraversal();
  //tree.Rotate();
  //tree.BreadthFirstTraversal();
  tree.RotateIterative2();
  tree.BreadthFirstTraversal();
}
Exemple #5
0
void Test1()
{
	int a[] = { 5, 3, 4, 1, 7, 8, 2, 6, 0, 9 };
	BSTree<int, int> bst;
	for (int i = 0; i < (sizeof(a) / sizeof(a[0])); ++i)
	{
		bst.Insert(a[i], i);
	}
	bst.Inorder();
	//cout << bst.Find(10)->_key << endl;
	cout << "------------------------------------------" << endl;
	cout<<bst.Remove(8)<<endl;
	bst.Inorder();
}
void main()
{
	BSTree oBSTree;

	oBSTree.Insert(100);
	oBSTree.Insert(50);
	oBSTree.Insert(150);
	oBSTree.Insert(25);
	oBSTree.Insert(75);
	oBSTree.Insert(125);
	oBSTree.Insert(175);
	oBSTree.Insert(110);

	FindSum(oBSTree.pRoot, 75);
}
void main()
{
	BSTree oBSTree;

	oBSTree.Insert(100);
	oBSTree.Insert(50);
	oBSTree.Insert(150);
	oBSTree.Insert(25);
	oBSTree.Insert(75);
	oBSTree.Insert(125);
	oBSTree.Insert(175);
	oBSTree.Insert(110);

	Node* pTmp = CommonAncestor(oBSTree.pRoot, 110, 175);

	cout << pTmp->data << endl;

	pTmp = CommonAncestor_Book(oBSTree.pRoot, 110, 175);

	cout << pTmp->data << endl;
}
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;
}
Exemple #9
0
int main(int argc,char*argv[])
{
	AVLTree<string> avlTree;
	BSTree<string> bsTree;
	string str, last;
	char c;

	if (argc != 2)
	{
		cerr << "Parameters: <filename>.  use quotations around long path names." << endl;
		return 1;
	}

	ifstream ifsInput(argv[1],ios::in | ios::binary);

	if (!ifsInput)
	{
		cerr << "Couldn't open input file." << endl;
		return 2;
	}

	
	while (ifsInput.get(c) && !ifsInput.eof())
	{
		switch (c)
		{
			case '.':
			case ',':
			case '\'':
			case '\"':
			case ':':
			case ';':
			case '<':
			case '>':
			case '/':
			case '?':
			case '[':
			case ']':
			case '{':
			case '}':
			case '\\':
			case '|':
			case '!':
			case '#':
			case '$':
			case '%':
			case '^':
			case '&':
			case '*':
			case '(':
			case ')':
			case '_':
			case '=':
			case '`':
			case ' ':
			case '\r':
			case '\n':
			case '\t':
				if (str.size())
				{
					avlTree.Insert(str);
					bsTree.Insert(str);
					last = str;
				}
				
				str = "";
				continue;
			default:
				str += c;
				continue;
		}

	}
	ifsInput.close();
	//x.Output(Print);
	cout << "AVL Height: " << avlTree.Height() << endl;
	cout << "BST Height: " << bsTree.Height() << endl;
	cout << "AVL Size:   " << avlTree.Size() << endl;
	cout << "BST Size:   " << bsTree.Size() << endl;


	clock_t stime, etime;
	double elapse; 
	int i;
	
	cout << "Searching for \"" << last << "\"..." << endl;
	stime = clock();
	for (i = 0; i < 100000; i++)
		avlTree.Find(last);
	etime = clock();
	elapse =(double)(etime-stime)/CLOCKS_PER_SEC;
	cout << "Search in AVL tree took " << elapse << " seconds " << endl;


	stime = clock();
	for (i = 0; i < 100000; i++)
		bsTree.Find(last);
	etime = clock();
	elapse =(double)(etime-stime)/CLOCKS_PER_SEC;
	cout << "Search in BST tree took " << elapse << " seconds " << endl;

	avlTree.Output(Print);

	return 0;
}
Exemple #10
0
int main()
{

  {
    cout << "Open files"<<endl;
    ifstream inS("SR.dat", ios::in|ios::binary);
    if ( !inS )
    {
      cout << "file open error: SR.dat"<<endl;
      cout << "Make sure that this file is in the working directory"<<endl;
      cin.get();  exit(1);
    }
    ifstream inI("ind.dat", ios::in|ios::binary);
    if ( !inI )
    {
      cout << "file open error: ind.dat"<<endl;
      cout << "Make sure that this file is in the working directory"<<endl;
      cin.get();  exit(1);
    }
    ifstream in ("CISP430records.dat", ios::in|ios::binary );
    if ( !in )
    {
      cout << "file open error: CISP430records.dat"<<endl;
      cout << "Make sure that this file is in the working directory"<<endl;
      cin.get();  exit(1);
    }
    ifstream Prein ("pre.dat", ios::in|ios::binary );
    if ( !Prein )
    {
      cout << "file open error: pre.dat"<<endl;
      cout << "Make sure that this file is in the working directory"<<endl;
      cin.get();
      exit(1);
    }
    ifstream Postin ("post.dat", ios::in|ios::binary );
    if ( !Postin )
    {
      cout << "file open error: post.dat"<<endl;
      cout << "Make sure that this file is in the working directory"<<endl;
      cin.get();  exit(1);
    }
    cout << "Done: Open Files"<<endl
         <<"*************************"<<endl;

    cout << "Read Files"<<endl;


    inI.read(reinterpret_cast<char*>(ind), IND_CNT*sizeof(unsigned));
    inI.close();
    BSTree  bst;
    unsigned least_rec = 0;
    unsigned recs_read = 0;


    unsigned MAX_READ = MAX_RECS;

    MAX_READ = (MAX_READ<MAX_RECS)? MAX_READ:MAX_RECS;

    bool Final_Test = MAX_READ == MAX_RECS;
    if ( !Final_Test )
    {
      cout << "For the final test MAX_READ must be "<<MAX_RECS
           <<endl<<"*************************"<<endl;
    }

    recs[recs_read].read(in);
    keys[recs_read] = recs[recs_read].Key();
    recs_read++;
    while ( recs_read < MAX_READ)
    {
      if ( !(recs_read%((MAX_READ/10)?MAX_READ/10:1)) )
        cout << recs_read<<endl;
      Record rec;
      rec.read(in);
      if ( !in.eof() )
      {
        recs[recs_read] = rec;
        keys[recs_read] = rec.Key();
        recs_read++;
      }
    }
    in.close();


    for ( unsigned i=0; i<recs_read; i++ )
    {
      if ( !((i+recs_read)%((MAX_READ/10)?MAX_READ/10:1)) )
        cout << i+recs_read <<endl;
      Preorder[i].read(Prein);
      Postorder[i].read(Postin);
      recs_inorder[i].read(inS);
    }
    Prein.close();
    Postin.close();
    inS.close();
    cout << "Done: Read files" << endl<<"*************************"<<endl;
    cout  <<"Insert data into BSTree"<<endl;


    mix(recs,recs_read);
    mix(keys,recs_read);

    for ( unsigned i=0; i<recs_read; i++ )
    {
      bst.Insert(recs[i]);
    }
     cout << "Done: Insert" << endl
     <<"*************************"<<endl
     <<"Test: Depth"<<endl;

    unsigned bd = bst.Depth();
    const unsigned CD = 46;
    if ( Final_Test && bd != CD )
    {
      cout << "bst depth: " << bst.Depth() << endl;
      cout << "Correct Depth: " << CD<<endl
           <<"<enter> to continue"<<endl;
      cin.get();

    }
    cout << "Done: Depth"<<endl
    <<"*************************"<<endl
    <<"Test: Traversals"<<endl;
    least = recs_inorder[least_rec];
    pcnt = 0;
    bst.InOrderTraversal(Inord);
    cout << "   Done: In order traversal #1"<<endl;

    ord_cnt = 0;
    bst.PreOrderTraversal(Preord);
    cout << "   Done: Pre order traversal #1"<<endl;

    ord_cnt = 0;
    bst.PostOrderTraversal(Postord);

    cout << "   Done: Post order traversal #1"<<endl
    <<"Done: Traversals"<<endl
    <<"*************************"<<endl
    <<"Test: Find"<<endl;


    for ( unsigned i=0; i<recs_read; i++ )
    {
       Record ret = bst.Find(keys[i]);
       if ( ret.Key() != keys[i] )
       {
         cout <<i<< "  Find Error. You found the Record with key: "
         << ret.Key() <<endl<< " when searching for key: "
         << keys[i] << endl
         <<"<enter> to continue"<<endl;
         cin.get();
       }
    }
    cout << "Done: Find"<<endl
    <<"*************************"<<endl
    <<"Test: Find bad key"<<endl;


    KeyType bad_key("~badkey"); //to the right
    Record BK = bst.Find(bad_key);
    KeyType KNF("KEY NOT FOUND");
    if ( BK.Key() != KNF  )
    {
      cout << "Looking for bad Key: " << bad_key<<endl;
      cout << "  you found: " << BK << endl
      <<"<enter> to continue"<<endl;
      cin.get();
    }
    bad_key[0] = 0; //to the left
    BK = bst.Find(bad_key);
    if ( BK.Key() != KNF  )
    {
      cout << "Looking for bad Key: " << bad_key<<endl;
      cout << "  you found: " << BK << endl
      <<"<enter> to continue"<<endl;
      cin.get();
    }

    bad_key[0] = 'i'; //first letter of the first LC key
    bad_key[1] = '~'; //to the right of the LC
    BK = bst.Find(bad_key);
    if ( BK.Key() != KNF  )
    {
      cout << "Looking for bad Key: " << bad_key<<endl;
      cout << "  you found: " << BK << endl
      <<"<enter> to continue"<<endl;
      cin.get();
    }

    bad_key[0] = 'Z'; //first letter of the first RC key
    bad_key[1] = 0; //to the left of the RC
    BK = bst.Find(bad_key);
    if ( BK.Key() != KNF  )
    {
      cout << "Looking for bad Key: " << bad_key<<endl;
      cout << "  you found: " << BK << endl
      <<"<enter> to continue"<<endl;
      cin.get();
    }

    cout << "Done: Find bad key"<<endl
    <<"*************************"<<endl;

    cout<<endl<<"Call Destructor"<<endl;
  }

   cout<<endl <<"*************************"
       <<endl <<"**********done***********"
       <<endl <<"*************************"<<endl;
    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;
}