TreeNode * Btree::contruct_tree() { TreeNode *root=NULL; TreeNode *leaf=NULL; Btree btree; /* //Test case1: root=btree.insert(1); //insert root leaf=btree.insert(2,root); */ //Test case2: root=btree.insert(5); //insert root //left tree leaf=btree.insert(4,root); leaf=btree.insert_nocompare(11,leaf); btree.insert_nocompare(7,leaf); btree.insert_nocompare(2,leaf); //right tree leaf=btree.insert_nocompare(8,root); btree.insert_nocompare(13,leaf); leaf=btree.insert_nocompare(4,leaf); btree.insert_nocompare(5,leaf); leaf=btree.insert_nocompare(1,leaf); return root; }
int main() { Solution solution; Btree btree; vector<vector<int> > result; TreeNode *root; root=btree.contruct_tree(); result=solution.levelOrderBottom(root); for(vector<vector<int> >::iterator it=result.begin();it!=result.end();it++) { cout<<"["; for(vector<int>::iterator it2=it->begin();it2!=it->end();it2++) { cout<<*it2<<","; } cout<<"]"<<endl; } return 0; }
Btree<T>& Btree<T>::GiveNullObject() { // The NULLOBJECT method must be overwritten in the derived classes to // actually return an instance of the respective class with the null byte // set to null otherwise mismatched class objects will be returned. static Btree<T> _NULLOBJECT("temp.btr"); _NULLOBJECT.SetAsNullObject(); return _NULLOBJECT; }
void main() { Btree A; int array1[] = { 7, 4, 2, 3, 15, 35, 6, 45, 55, 20, 1, 14, 56, 57, 58 }; int array[] = { 1, 2 }; int k; k = sizeof(array) / sizeof(array[0]); cout << "建立排序二叉树顺序: " << endl; for (int i = 0; i<k; i++) { cout << array[i] << " "; A.create_Btree(array[i]); } cout << endl; cout << "二叉树节点个数: " << A.count(A.root) << endl; cout << "二叉树叶子个数:" << A.findleaf(A.root) << endl; cout << "二叉树中度数为1的结点的数量为:" << A.findnode(A.root) << endl; cout << endl << "先序遍历序列: " << endl; A.display1(); cout << endl << "中序遍历序列: " << endl; A.display2(); cout << endl << "后序遍历序列: " << endl; A.display3(); cout << endl << "平衡树判断测试:" << endl; A.display4(); system("PAUSE"); }
int main() { ifstream file; ofstream fileout; int a; Btree tree; file.open("input.txt"); while (!file.eof()) { file >> a; tree.insert(a, tree.root); } file.close(); fileout.open("out.txt"); tree.printout(tree.root, fileout); fileout.close(); }
int main() { Solution solution; Btree btree; vector<int> result; TreeNode *root; root=btree.contruct_tree(); result=solution.postorderTraversal(root); for(vector<int>::iterator it2=result.begin();it2!=result.end();it2++) { cout<<*it2<<","; } return 0; }
int main(){ Btree<Key,Value> index; index.recover("test.index"); Key k1("12134"); Key k2("11111"); Value v1(1,1); Value v2(3,2); Value v3(4,5); index.insert_kv(k1,v1); index.insert_kv(k1,v2); index.insert_kv(k2,v3); list<Value> vs; list<Value>::iterator it; index.find_values(k1,vs); for(it=vs.begin();it!=vs.end();it++){ cout<<(*it).s_id<<" "<<(*it).d_id<<endl; } }
int main() { Btree tree; // = new Btree(); int i; for(i=0; i<=100; i++) { tree.insert(i); } tree.destroy_tree(); if ( tree.search(15) != NULL ) cout << "found\n" << endl; else cout << "not found\n" << endl; return 0; }
void main() { Btree A; int array1[] = { 7, 4, 2, 3, 15, 35, 6, 45, 55, 20, 1, 14, 56, 57, 58 }; int array2[] = { 2, 1 }; int array[] = { 4, 2, 6, 1, 3, 5, 7 }; int k; k = sizeof(array1) / sizeof(array1[0]); cout << "建立排序二叉树顺序: " << endl; for (int i = 0; i<k; i++) { cout << array1[i] << " "; A.create_Btree(array1[i]); } cout << endl; cout << endl << "先序遍历序列: " << endl; A.display1(); cout << endl << "中序遍历序列: " << endl; A.display2(); cout << endl << "后序遍历序列: " << endl; A.display3(); cout << endl << "分层遍历序列: " << endl; A.display4(); cout << endl << "inorder is answer." << endl; system("PAUSE"); }
main() { int i,j,k; BtreeNode* myBtreeRoot=new BtreeNode; Btree myBtree; int key,value; int list_of_keys[MAX_INPUT_SIZE],list_of_values[MAX_INPUT_SIZE]; int no_of_keys; char input_filename[100]; //clock_t Start, Time; FILE *fp_sorted=fopen("BTree_sorted.out","w"); FILE *fp_level=fopen("BTree_level.out","w"); intitialize_node(myBtreeRoot); printf("\nPlease enter the input-filename:"); scanf("%s",input_filename); //insert all the keys from the random file FILE *fp_in=fopen(input_filename,"r"); fscanf(fp_in,"%d",&no_of_keys); for(i=0;i<no_of_keys;i++) fscanf(fp_in,"\n%d %d",&list_of_keys[i],&list_of_values[i]); fclose(fp_in); //Start = clock(); for(i=0;i<no_of_keys;i++) { myBtreeRoot=myBtree.insert(myBtreeRoot,list_of_keys[i],list_of_values[i]); myBtree.create_root(myBtreeRoot); //printf(" [root[0]=%d] [root[1]=%d] [root[2]=%d] children:[",myBtreeRoot->keys[0],myBtreeRoot->keys[1],myBtreeRoot->keys[2]); //for(j=0;myBtreeRoot->child_ptrs[j]!=NULL;j++) // printf("%d,%d,%d ",myBtreeRoot->child_ptrs[j]->keys[0],myBtreeRoot->child_ptrs[j]->keys[1],myBtreeRoot->child_ptrs[j]->keys[2]); //printf("]\n\n"); } //Time = clock() - Start; // time in micro seconds //printf("\nTime for Insertion in BTREE=%lf\n\n",Time/(double)CLOCKS_PER_SEC); //print the tree- levels //printf("Btree_level.out\n"); myBtree.display_level(myBtreeRoot,fp_level); //print the tree- levels //printf("Btree_sorted.out\n"); myBtree.display_sorted(myBtreeRoot,fp_sorted); /* Start = clock(); //search the keys for(i=0;i<no_of_keys;i++) { value=myBtree.search(myBtreeRoot,list_of_keys[i]); if(value==-1) printf("\n Key %d not found.",list_of_keys[i]); else printf("\n Key %d found with value %d",list_of_keys[i],value); } Time = clock() - Start; // time in micro seconds printf("\nTime for Searching in BTREE=%lf\n",Time/(double)CLOCKS_PER_SEC); */ }
TreeNode * Btree::contruct_tree() { TreeNode *root=NULL; TreeNode *leaf=NULL; Btree btree; /* //Test case1: root=btree.insert(1); //insert root leaf=btree.insert(2,root); */ //Test case2: /*root=btree.insert(5); //insert root //left tree leaf=btree.insert(4,root); leaf=btree.insert_nocompare(11,leaf); btree.insert_nocompare(7,leaf); btree.insert_nocompare(2,leaf); //right tree leaf=btree.insert_nocompare(8,root); btree.insert_nocompare(13,leaf); leaf=btree.insert_nocompare(4,leaf); leaf=btree.insert_nocompare(1,leaf);*/ //Test case3: root=btree.insert(3); //insert root //left tree leaf=btree.insert_nocompare(9,root); //btree.insert_nocompare('#'-'0',leaf); //btree.insert_nocompare('#'-'0',leaf); //right tree leaf=btree.insert_nocompare(20,root); btree.insert_nocompare(15,leaf); btree.insert_nocompare(7,leaf); /*//Test case3:Only left tree root=btree.insert(1); //insert root //left tree btree.insert_nocompare('#'-'0',root); leaf=btree.insert_nocompare(2,root); btree.insert_nocompare('#'-'0',leaf); leaf=btree.insert_nocompare(3,leaf); btree.insert_nocompare('#'-'0',leaf); leaf=btree.insert_nocompare(4,leaf); btree.insert_nocompare(5,leaf);*/ /*//Test case4: root=btree.insert(-8); //insert root //left tree btree.insert_nocompare(0,root); leaf=btree.insert_nocompare(3,root); leaf=btree.insert_nocompare(-8,leaf); leaf=btree.insert_nocompare(-1,leaf); btree.insert_nocompare(8,leaf);*/ return root; }
void menu() { Btree <int> test; char choice; do { cout << endl; cout << "(C)ount" << endl; cout << "(I)nsert" << endl; cout << "(R)emove" << endl; cout << "(P)rint " << endl; cout << "(Q)uit" << endl; cout << "(S)earch" << endl; cin >> choice; switch (choice) { case 'C': case 'c': { test.count(); } case 'I': case 'i': { int value; cout << "Give me a value and I will insert it " << endl; cin >> value; test.Insert(value); break; } case 'P': case 'p': { test.preprint(); break; } case 'R': case 'r': { int value; cout << "Give me a value and I will remove it " << endl; cin >> value; test.Remove(value); break; } case 'q': case 'Q': { cout << "Bye Bye" << endl; return; } case 's': case 'S': { int value; cout << "Give me a value and I will search for it " << endl; cin >> value; if (test.Search(value)) cout << "It was found in the binary tree!" << endl; else cout << "It was not found in the binary tree." << endl; break; } default: cout << "Bad user, enter a proper choice." << endl; } } while (1); }