int main() { BST<int> t; int a[8] = {30,10,16,35,40,5,33,11}; for(int i=0; i < 8; i++) { t.Insert(a[i]); } t.DisplayInorder(); cout << endl; BST<string> q; string days[7] = {"Mon", "Tue", "Wed", "Thr", "Fri", "Sat", "Sun"}; for(int i=0; i < 7; i++) { q.Insert(days[i]); } q.DisplayInorder(); cout << endl; BST<char> r; for(char i='A'; i <='K'; i++) { r.Insert(i); } r.DisplayInorder(); cout << endl; system("pause"); return 0; }
int main() { BST bst; if(bst.Empty()) cout << "BST::Empty() works" << endl; else cout << "BST::Empty() doesn't work" << endl; bst.Insert(0); if(!bst.Empty()) cout << "BST::Empty() works" << endl; else cout << "BST::Empty() doesn't work" << endl; if(bst.Delete(0)) cout << "BST::Delete(const int) works" << endl; else cout << "BST::Delete(const int) doesn't work" << endl; if(!bst.Delete(-1)) cout << "BST::Delete(const int) works" << endl; else cout << "BST::Delete(const int) doesn't work" << endl; for(int i = 0; i < 10; i++) bst.Insert(i); cout << bst.Min() << endl; cout << bst.Max() << endl; if(bst.SearchIter(0) && !bst.SearchIter(10)) cout << "BST::SearchIter(const int) works" << endl; else cout << "BST::SearchIter(const int) doesn't work" << endl; if(bst.SearchRec(0) && !bst.SearchRec(10)) cout << "BST::SearchRec(const int) works" << endl; else cout << "BST::SearchRec(const int) doesn't work" << endl; bst.Clear(); if(bst.Empty()) cout << "BST::Empty() works" << endl; else cout << "BST::Empty() doesn't work" << endl; return 0; } // end main()
//Fills a tree with criminals from a text file void populateTree(BST& tree, string file) { string currentLine = ""; ifstream criminals(file); getline(criminals, currentLine); //Gets first SUBJECT line while (!criminals.eof()) { string data = ""; getline(criminals, currentLine); //Gets name of criminal if (currentLine != "FINISHED") { while (currentLine != "SUSPECT") { data += currentLine + " "; getline(criminals, currentLine); //Gets attributes and subsequent SUBJECTs } Criminal insertion = Criminal(data); tree.Insert(insertion); } else { criminals.close(); return; } } }
int main() { BST *btree = new BST; btree->Insert("kiwi"); btree->Insert("mango"); btree->Insert("apple"); // display in preorder form btree->preOrder(); cout << endl; btree->inOrder(); cout << endl; btree->postOrder(); cout << endl; return 0; }
void openAccount(BST<int> B) { int id, balance; string name; cout << "\tEnter the name: "; cin >> name; cout << "\tEnter the balance: "; cin >> balance; cout << "\tEnter the new ID: "; cin >> id; B.Insert(id, name, balance); }
int main(int argc,char **argv) { // Create an empty Binary Search Tree BST Tree; string input = ""; while (input !="ENDINSERT") { cin >> input; if (input != "ENDINSERT") Tree.Insert(input); } Tree.Print("POST"); }
int main(int argc,char **argv) { // Create an empty Binary Search Tree BST Tree; string input; cin >> input; while(input != "ENDINSERT") { //cout << "got here" << endl; Tree.Insert(input); cin >> input; } //cout << "done inserting" << endl; Tree.Print("POST"); }
void main() // key(pair사용시 data.first) = 노드의 이름 같은 개념, element(pair 사용시 data.second) = 노드의 고유 data { //declare the type of K, E typedef int K; typedef char E; BST<K, E> HWbst; // Making BST class HWbst link every nodes int menu = 0; while (1) { // Menu // cout << endl << " #### Choose Menu ####" << endl; cout << "1. Insert 2. Delete 3. Print 4. Exit" << endl; cin >> menu; switch (menu) { case 1: // Insert Menu { pair<K, E> A; // new node's pair type data cout << " *** 새로운 TreeNode의 Key : "; cin >> A.first; cout << " *** 새로운 TreeNode의 Element : "; cin >> A.second; HWbst.Insert(A); // use insert function break; } case 2: // Delete Menu { int a = 0; cout << " *** 삭제할 노드의 Key 입력 : "; cin >> a; HWbst.Delete(a); break; } case 3: // Print all nodes by level order { HWbst.Print(); break; } case 4: // close this program { return; } } } }
int main() { char restart; char selection; BST<string> p; string sentence = "George Samsa awoke one morning only to discover " "he had been transformed into a giant Cockroach"; istringstream iss(sentence); while(iss) { string word; iss >> word; p.Insert(strtolower(word)); } do{ cout << "a.\tDisplay the words in alphabetical order\n" << "b.\tdisplay the tree sideways\n" << "c.\tHow many leaves does the tree have\n" << "d.\tWhat is the height of the tree\n" << "e.\tSelect a random letter from a-z and delete all words that begin with that letter\n\n"; cout << "Make a selection: "; cin >> selection; switch(tolower(selection)) { case 'a': cout << "\nWords in alphabetical order: "; p.DispTree(); cout << endl; break; case 'b': p.DispSideway(); break; case 'c': cout << "\nNumber of leaves in tree: "; p.countNodes(); break; case 'd': cout << "\nHeight of tree: "; p.treeHeight(); break; } cout << "\nAgain(Y/N)?: "; cin >> restart; cout << endl; }while(toupper(restart) == 'Y'); return 0; }
int main(int argc,char **argv) { // Create an empty Binary Search Tree BST Tree; //Tree=new BST(); string input; cin>>input; while(input!="ENDINSERT"){ Tree.Insert(input); cin>>input; } Tree.Print("POST"); }
int main(int argc, char** argv) { atexit(ExitHook); BST<int> bst; Dumper<int> dumper; std::vector<int> vec; vec.reserve(NT); for (int i = 0; i < N; ++i) vec.push_back(i); std::random_shuffle(vec.begin(), vec.end()); #if DUMP std::copy(vec.begin(), vec.end(), std::ostream_iterator<int>(std::cout, " ")); std::endl(std::cout); #endif for (int i = 0; i < (int)vec.size(); ++i) bst.Insert(vec[i]); #if DUMP bst.Accept(dumper); std::endl(std::cout); #endif for (int i = 0; i < NT; ++i) { bool ret = bst.Remove(i); #if DUMP std::cout << "Remove " << i << " " << ret << std::endl; #endif } #if DUMP bst.Accept(dumper); std::endl(std::cout); #endif return 0; }
int main(int argc , char* argv[]) { BST<TermRec> T; ifstream F("flist.txt") , file ; ofstream fp(argv[1]); if (!F.is_open()) { cout << "\nError opening file flist.txt , Aborting!!"; return -1;} string file_to_be_indexed , line , word , file_name; int line_no , i , start; TermRec *Node , *word_obj; while (getline( F , file_to_be_indexed )) { file.open(file_to_be_indexed.c_str()); if (file.is_open()) { start = 0; while ( (i = file_to_be_indexed.find("/",start)) != std::string::npos ) //finding file name start = i+1; file_name = file_to_be_indexed.substr(start); line_no = 1; while (getline(file , line)) { start = 0; line.append(" "); while ( (i = line.find(" ",start)) != std::string::npos) { word = line.substr(start , i-start); for ( int j=0 ; j < word.length() ; j++) word[j] = tolower(word[j]); word_obj = new TermRec( word ); Node = T.Insert(word_obj); Node->Insert(file_name , line_no); delete word_obj; start = i+1; while (line[start] == ' ') //to accomodate more than one space between words start++;} line_no++;} file.close();} else cout << "\nError opening file " << file_to_be_indexed; } write_to_file(T.get_root() , fp); return 0; }
int main() { BST<int> tree; int a[6] = {25, 9, 36, 2, 17, 40}; for (int i = 0; i < 6; i++) { tree.Insert(a[i]); } cout << "\nInorder Traversal Display: \n"; tree.DisplayInorder(); cout << "\n\nPostorder Traversal Display: \n"; tree.DisplayPostorder(); cout << "\n\nPreorder Traversal Display: \n"; tree.DisplayPreorder(); cout << "\n\nDisplay Leaves only: \n"; tree.DisplayLeaves(); cout << "\n\nDisplay Parents only: \n"; tree.DisplayParent(); cout << endl << endl; return 0; }
int main(int argc,char **argv) { // Create an empty Binary Search Tree BST Tree; string input; cin>>input; while(input!="ENDINSERT"){ Tree.Insert(input); cin>>input; } //Tree.Print("POST"); //cout<<"transition"<<endl; cin>>input; while(input!="ENDDELETE"){ Tree.Delete(input); cin>>input; } Tree.Print("POST"); }
int main(){ LinkedList tList = LinkedList(); char* c = new char[3]; c[1] = '\n'; c[2] = '\0'; for(int i = 0; i < 200000; i++){ c[0] = (char)i; tList.Insert(c, NULL); } tList = tList; cout << "LinkedList contents:" << tList.GetSize() << endl; //tList->Output(cout); //tList->Clear(); LinkedList tNew = tList; //tList->Clear(); cout << endl << endl << "List Copy contents:" << tNew.GetSize() << endl; //tNew.Output(cout); BST* tBST = new BST(); BSTNode* tBSTNode; tBSTNode = tBST->Insert("Cool\n"); tBSTNode = tBST->Insert("Awesome\n"); tBSTNode = tBST->Insert("Test\n"); tBSTNode = tBST->Insert("1\n"); //tBST->Remove(tBSTNode); tBSTNode = tBST->Insert("4\n"); tBSTNode = tBST->Insert("2\n"); tBSTNode = tBST->Insert("3\n"); cout << endl << endl << "BST contents:" << tBST->GetSize() << endl; tBST->Output(cout, tBST->GetRoot()); delete tBST; }
int main() { BST<int> B; int r; fstream f; int bal, id; string name; f.open("data.txt", ios::in); f >> id >> name >> bal; while (!f.eof()) { B.Insert(id, name, bal); f >> id >> name >> bal; } f.close(); bool again; string leftover; char selection, c; cout << "--------- Bank of California ---------" << endl; cout << "a.\tDisplay all accounts" << endl; cout << "b\tShow my balance" << endl; cout << "c.\tDeposit" << endl; cout << "d.\tWithdraw" << endl; cout << "e.\tClose account" << endl; cout << "f.\tOpen a new account" << endl; cout << endl; do { do { cout << "Please enter your choice(a-f):"; cin >> selection; getline(cin, leftover, '\n'); //gets rid of the rest of the input } while (selection != 'a' && selection != 'b' && selection != 'c' && selection != 'd' && selection != 'e' && selection != 'f'); cout << endl; switch (selection) { case 'a': displayAll(B); break; case 'b': displayBalance(B); break; case 'c': depositFunds(B); break; case 'd': withdrawFunds(B); break; case 'e': closeAccount(B); break; case 'f': openAccount(B); break; } cout << endl; do { cout << "CONTINUE(y/n)?"; cin >> c; getline(cin, leftover, '\n'); //gets rid of the rest of the input } while (c != 'y' && c != 'n'); if (c == 'y'){ again = true; cout << endl; } else { again = false; } } while (again == true); system("pause"); return 0; }
int main(){ BST testBST; BST testAssign; BNode* temp; int data; PrintMenu(); cout << "-->"; char choice; cin >> choice; while(choice != 'q' && choice != 'Q'){ if (choice == '!'){ BST testCopy(testBST); cout << "Result:" << "Print New Copy" << endl; testCopy.PrintIn(cout); cout << endl; testCopy.Insert(-10000); cout << "Result: " << "Print Modified Copy" << endl; testCopy.PrintIn(cout); cout << endl; cout << "Result: " << "Print Original Test List" << endl; testBST.PrintIn(cout); cout << endl; } else{ switch (choice){ case 'h': case 'H': PrintMenu(); break; case '+': cin >> data; testBST.Insert(data); break; case '-': cin >> data; testBST.Remove(data); break; case '@': cout <<"Result:"; temp = testBST.AtCursor(); if (temp) cout << temp->GetData() << endl; else cout << "NULL POINTER" << endl; break; case '<': testBST.GoToBeginning(); break; case '>': testBST.GoToEnd(); break; case 'n': case 'N': testBST.GoToNext(); break; case 'p': case 'P': testBST.GoToPrev(); break; case 'c': case 'C': testBST.ClearList(); break; case 'e': case 'E': if (testBST.Empty()) cout <<"Result:" << "List Is Empty" << endl; else cout <<"Result:" << "List is Not Empty" << endl; break; case '#': //assign list testAssign = testBST; testAssign.Insert(-100000); cout << "Modify New List" << endl; testAssign.PrintIn(cout); cout << endl; cout << "Old List should not be affected" << endl; testBST.PrintIn(cout); cout << endl; testAssign.~BST(); cout << "Destroy New List" << endl; cout << "Old List should not be affected" << endl; testBST.PrintIn(cout); cout << endl; break; case '?': cin >> data; if (testBST.Find(data) != NULL) cout << "Result:" << data << "\tfound" << endl; else cout << "Result:" << data << "\tnot found" << endl; break; case 'i': case 'I': cout << "Print INORDER" << endl; testBST.PrintIn(cout); cout << endl; break; case 'r': case 'R': cout << "Print PREORDER" << endl; testBST.PrintPre(cout); cout << endl; break; case 'o': case 'O': cout << "Print POSTORDER" << endl; testBST.PrintPost(cout); cout << endl; break; default: cout << "INVALID CHOICE, Choose Again" << endl; } } testBST.PrintIn(cout); cout << endl; cout << "-->"; cin >> choice; } return 0; }
int main() { BST testBST; cout << "inserting 20\n"; testBST.Insert(20); cout << "inserting 15\n"; testBST.Insert(15); cout << "inserting 30\n"; testBST.Insert(30); cout << "inserting 5\n"; testBST.Insert(5); cout << "inserting 18\n"; testBST.Insert(18); cout << "inserting 40\n"; testBST.Insert(40); cout << "inserting 25\n"; testBST.Insert(25); cout << "calling PrintIn\n"; testBST.PrintIn(cout); cout << "calling PrintPost\n"; testBST.PrintPost(cout); cout << "calling PrintPre\n"; testBST.PrintPre(cout); cout << "calling Find(4)\n"; BNode * found = testBST.Find(4); cout << "found " << (*found).GetData() << endl; testBST.ClearList(); cout << "Cleared list, is it empty? " << testBST.Empty() << endl; testBST.Insert(5); testBST.Insert(6); cout << "Should return 5? " << testBST.getParent(testBST.Find(6))->GetData() << endl; testBST.PrintIn(cout); // test Remove with zero children testBST.Remove(6); cout << "Removed 6\n"; testBST.PrintIn(cout); testBST.Remove(5); cout << "Removed 5\n"; testBST.PrintIn(cout); cout << "clearing List for further tests\n"; testBST.ClearList(); testBST.Insert(5); testBST.Insert(6); testBST.Insert(7); testBST.PrintIn(cout); // test Remove with one child testBST.Remove(6); cout << "Removed 6\n"; testBST.PrintIn(cout); cout << "5 should now link to 7\n"; // test remove with two children cout << "Clearing list\n"; testBST.ClearList(); testBST.Insert(20); testBST.Insert(15); testBST.Insert(30); testBST.Insert(5); testBST.Insert(18); testBST.Insert(40); testBST.Insert(25); cout << "Tree is now:\n"; testBST.PrintIn(cout); cout << "Removing 30\n"; testBST.Remove(30); }
int _tmain(int argc, _TCHAR* argv[]) { //Binary Search Tree int bstArraySize = 11; int bstArray[] = { 15, 6, 18, 3, 7, 17, 20, 2, 13, 4, 9 }; cout << "BST Input Array: " ; PrintArray(bstArray, bstArraySize); BST myBST; for (int i = 0; i < bstArraySize; i++) myBST.Insert(myBST.root, bstArray[i]); cout << "InOrderTreeWalk: "; myBST.InOrderTreeWalk(myBST.root); cout << endl; cout << "PreOrderTreeWalk: "; myBST.PreOrderTreeWalk(myBST.root); cout << endl; cout << "PostOrderTreeWalk: "; myBST.PostOrderTreeWalk(myBST.root); cout << endl; myBST.Search(myBST.root, 100); myBST.Search(myBST.root, 20); myBST.Maximum(myBST.root); myBST.Minimum(myBST.root); myBST.SearchParent(myBST.root, 9); cout << "Delete Node 15: "; myBST.Delete(myBST.root, 15); myBST.InOrderTreeWalk(myBST.root); cout << endl; cout << endl; //Max Heap int table[] = {16, 4, 10, 14, 7, 9, 3, 2, 8, 1, 0, 0, 0, 0}; int heapSize = 10; int* maxHeapArray = new int[15]; for (int i = 0; i < heapSize; i++) *(maxHeapArray + i) = table[i]; cout << "Max-Heap: "; PrintArray(maxHeapArray, heapSize); MaxHeap myMaxHeap; myMaxHeap.MaxHeapify(maxHeapArray, heapSize, 1); cout << "Max-Heapified 1: "; PrintArray(maxHeapArray, heapSize); for (int i = 0; i < heapSize; i++) *(maxHeapArray + i) = table[i]; cout << "Max-Heap: "; PrintArray(maxHeapArray, heapSize); myMaxHeap.BuildHeap(maxHeapArray, heapSize); cout << "BuildHeap: "; PrintArray(maxHeapArray, heapSize); cout << "HeapExtractMax: " << myMaxHeap.HeapExtractMax(maxHeapArray, heapSize) << endl; cout << "Remaining: "; PrintArray(maxHeapArray, heapSize - 1); cout << "HeapInsert 32: \n"; myMaxHeap.HeapInsert(maxHeapArray, heapSize - 1, 32); cout << "New Heap: "; PrintArray(maxHeapArray, heapSize); for (int i = 0; i < heapSize; i++) *(maxHeapArray + i) = table[i]; cout << "Max-Heap: "; PrintArray(maxHeapArray, heapSize); myMaxHeap.HeapSort(maxHeapArray, heapSize); cout << "HeapSort: "; PrintArray(maxHeapArray, heapSize); cout << endl; //AVL tree int avlArraySize =11; //int avlArray[] = { 15, 6, 18, 3, 7, 17, 20, 2, 13, 4, 9 }; int avlArray[] = { 30, 50, 40, 60, 70, 17, 20, 2, 13, 4, 9 }; cout << "AVL Input Array: "; PrintArray(avlArray, avlArraySize); AVL myAVL; for (int i = 0; i < avlArraySize; i++) { myAVL.Insert(myAVL.root, avlArray[i]); myAVL.InOrderTreeWalk(myAVL.root); cout << endl; } cout << "Delete 30: \n"; myAVL.Delete(myAVL.root, 30); myAVL.InOrderTreeWalk(myAVL.root); cout << endl; cout << "Delete 13: \n"; myAVL.Delete(myAVL.root, 13); myAVL.InOrderTreeWalk(myAVL.root); cout << endl; cout << "Delete 20: \n"; myAVL.Delete(myAVL.root, 20); myAVL.InOrderTreeWalk(myAVL.root); cout << endl; //RB tree int rbArraySize = 11; //int rbArray[] = { 1, 3, 2, 4, 5, 6, 8, 7, 9, 10, 11 }; int rbArray[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; cout << "RB Input Array: "; PrintArray(rbArray, rbArraySize); RB myRB; for (int i = 0; i < rbArraySize; i++) { myRB.Insert(myRB.root, myRB.root, rbArray[i]); myRB.InOrderTreeWalk(myRB.root); cout << endl; } RB myRB2; myRB2.Insert(myRB2.root, myRB2.root, 50); myRB2.Insert(myRB2.root, myRB2.root, 20); myRB2.Insert(myRB2.root, myRB2.root, 30); myRB2.Insert(myRB2.root, myRB2.root, 40); myRB2.Insert(myRB2.root, myRB2.root, 45); myRB2.Insert(myRB2.root, myRB2.root, 43); myRB2.Insert(myRB2.root, myRB2.root, 44); myRB2.Insert(myRB2.root, myRB2.root, 42); myRB2.InOrderTreeWalk(myRB2.root); cout << endl; myRB2.Delete(myRB2.root, 50); myRB2.InOrderTreeWalk(myRB2.root); cout << endl; //4.2 Minimal Tree : Given a sorted(increasing order) array with unique integer elements, write an // algorithm to create a binary search tree with minimal height. int minTree[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; TreeNode* n = MinTreeInsert(minTree, 0, 9); //4.5 Validate BST: Implement a function to check if a binary tree is a binary search tree. int* valArray = new int[10]; CopyBST(n, valArray, 10, 0); for (int i = 0; i < 10; i++) cout << i << " "; cout << endl; cout << ValidateBST(n, -1000000)<<endl; //4.8 First Common Ancestor: Design an algorithm and write code to find the first common ancestor // of two nodes in a binary tree.Avoid storing additional nodes in a data structure.NOTE: This is not //necessarily a binary search tree. int binaryTree[] = { 3, 5, 2, 6, 9, 7, 1, 4, 8, 10}; TreeNode* bt = MinTreeInsert(binaryTree, 0, 9); //4.9 BST Sequences : A binary search tree was created by traversing through an array from left to right // and inserting each element.Given a binary search tree with distinct elements, print all possible // arrays that could have led to this tree. //4.1O Check Subtree : Tl and T2 are two very large binary trees, with Tl much bigger than T2.Create an // algorithm to determine ifT2 is a subtree of Tl. // A tree T2 is a subtree of Tl if there exists a node n in Tl such that the subtree of n is identical to T2. // That is, if you cut off the tree at node n, the two trees would be identical. //4.11 Random Node : You are implementing a binary search tree class from scratch, which, in addition // to insert, find, and delete, has a method getRandomNode() which returns a random node // from the tree.All nodes should be equally likely to be chosen.Design and implement an algorithm // for getRandomNode, and explain how you would implement the rest of the methods. getchar(); return 0; }