本文整理汇总了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();
}
示例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();
}
示例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;
}
示例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();
}
示例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();
}
示例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);
}
示例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;
}
示例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;
}
示例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;
//.........这里部分代码省略.........
示例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);
//.........这里部分代码省略.........
示例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)");
//.........这里部分代码省略.........