当前位置: 首页>>代码示例>>C++>>正文


C++ BSTree::Insert方法代码示例

本文整理汇总了C++中BSTree::Insert方法的典型用法代码示例。如果您正苦于以下问题:C++ BSTree::Insert方法的具体用法?C++ BSTree::Insert怎么用?C++ BSTree::Insert使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在BSTree的用法示例。


在下文中一共展示了BSTree::Insert方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: main

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();
}
开发者ID:PeterWang9,项目名称:code,代码行数:35,代码来源:bst.cpp

示例2: TestBSTree

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();
}
开发者ID:changfeng777,项目名称:DataStruct,代码行数:28,代码来源:BinarySearchTree.hpp

示例3: main

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;
}
开发者ID:Fireplusplus,项目名称:Data-Structure,代码行数:48,代码来源:BinarySearchTree.cpp

示例4: main

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();
}
开发者ID:pennymaxlw,项目名称:Algorithms_and_Data_Structures,代码行数:16,代码来源:ms100_015_016.cpp

示例5: Test1

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();
}
开发者ID:JulyShuofeng,项目名称:gitcode,代码行数:14,代码来源:test.cpp

示例6: main

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);
}
开发者ID:eandbsoftware,项目名称:AlgorithmStudy,代码行数:15,代码来源:4.9.new.cpp

示例7: main

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;
}
开发者ID:eandbsoftware,项目名称:AlgorithmStudy,代码行数:21,代码来源:5_CommonAncestor.cpp

示例8: main

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;
}
开发者ID:ManosAgelidis,项目名称:BinarySearchTree,代码行数:24,代码来源:main.cpp

示例9: main

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;
//.........这里部分代码省略.........
开发者ID:nacitar,项目名称:old-projects,代码行数:101,代码来源:driver.cpp

示例10: main

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);
//.........这里部分代码省略.........
开发者ID:dzackgarza,项目名称:Trees,代码行数:101,代码来源:BSTmain.cpp

示例11: UnitTest

// 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)");
//.........这里部分代码省略.........
开发者ID:burkell530,项目名称:Repository321,代码行数:101,代码来源:exercise_25.cpp


注:本文中的BSTree::Insert方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。