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() { 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() { 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; }
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; }