//Given an array that stores a complete Binary Search Tree, // write a function that efficiently prints the given array in ascending order. //array is organized lie heap so a[i] has children at a[2i+1] and a[2i+2] void printSorted(int a[], int beg, int end){ if(beg < end){ printSorted(a,2*beg+1,end); printf(" %d ",a[beg]); printSorted(a,2*beg+2,end); } }
/* * Given an array that stores a complete Binary Search Tree, write a function that efficiently prints the * given array in ascending order. * For example, given an array [4, 2, 5, 1, 3], the function should print 1, 2, 3, 4, 5 * */ void printSorted(int array[], int start, int end) { if(start > end) return; printSorted(array, start * 2 + 1, end); cout << array[start] << endl; printSorted(array, start * 2 + 2, end); }
void AddressProfiler::printStats(ostream& out) const { if (g_param_ptr->PROFILE_HOT_LINES()) { out << endl; out << heading("AddressProfiler Stats"); out << endl; out << "sharing_misses: " << m_sharing_miss_counter << endl; out << "getx_sharing_histogram: " << m_getx_sharing_histogram << endl; out << "gets_sharing_histogram: " << m_gets_sharing_histogram << endl; out << endl; out << heading("Hot Data Blocks"); out << endl; printSorted(out, m_dataAccessTrace, "block_address"); out << endl; out << heading("Hot MacroData Blocks"); out << endl; printSorted(out, m_macroBlockAccessTrace, "macroblock_address"); out << heading("Hot Instructions"); out << endl; printSorted(out, m_programCounterAccessTrace, "pc_address"); } if (g_param_ptr->PROFILE_ALL_INSTRUCTIONS()){ out << endl; out << heading("All Instructions Profile"); out << endl; printSorted(out, m_programCounterAccessTrace, "pc_address"); out << endl; } if (m_retryProfileHisto.size() > 0) { out << heading("Retry Profile"); out << endl; out << "retry_histogram_absolute: " << m_retryProfileHisto << endl; out << "retry_histogram_write: " << m_retryProfileHistoWrite << endl; out << "retry_histogram_read: " << m_retryProfileHistoRead << endl; out << "retry_histogram_percent: "; m_retryProfileHisto.printPercent(out); out << endl; out << "retry_histogram_per_instruction: "; m_retryProfileHisto.printWithMultiplier(out, 1.0 / double(g_system_ptr->getProfiler()->getTotalInstructionsExecuted())); out << endl; printSorted(out, m_retryProfileMap, "block_address"); out << endl; } }
char Interface::askToSort() { char answer; cout << "Do you want to sort the list? (y/n)" << endl; cin >> answer; if(answer == 'y') printSorted(); return answer; }
/* ----------------------------------------------------------------------------- Prints a symbol table. If sorted output is requested but there is insufficent memory then unsorted output results. Returns: (int) 1 if table is output as requested 0 if memory for sorted table could not be allocated. ----------------------------------------------------------------------------- */ bool HashTable::print(FILE *fp, bool sortFlag) { if (sortFlag) { if (!printSorted(fp)) printUnsorted(fp); else return true; } else printUnsorted(fp); return false; }
int main(){ char str1[10][1024]; char *str[10]; int i = 0; printf("Input string\n"); while (scanf("%s", str1[i]) != EOF && i < 10){ str[i] = str1[i]; i++; } printf("a.print the first string\nb.print as ASCII\n" "c.print increasely\nd.print the length of the first word in string\n" "e.quit\n"); fflush(stdin); char ch; while((ch = getchar()) != 'e'){ switch(ch){ case 'a': puts(str[0]); break; case 'b': printSorted(str, 10); break; case 'c': printIncrease(str, 10); break; case 'd': print1stWord(str , 10); break; default: printf("Invalid character,try again!\n"); break; } fflush(stdin); } system("pause"); return 0; }
int main() { int arr[] = {10,3,5,2,9,100}; int i, len, key; TreePointer bst, ret; len = arr_length(arr,int); //create tree using ADT bst = createBST(arr[0]); for(i=1; i < len ; i ++) insert(bst,arr[i]); key = 2; ret = BSTsearch(bst,key); assert(ret!=NULL); assert(key==getBSTData(ret)); //print all by order printSorted(bst); return 0; }
void Interface::start() //Keyrir forritið. { programInfo(); //Opnunarskilaboð til notanda. int numb; while(true) { pickOption(); cin >> numb; if(cin.fail()) { cin.clear(); cin.ignore(100,'\n'); } if(numb < 1 || numb > 4) { cout << "The input you entered is not a valid option. Pick again!" << endl; //Villuskilaboð fyrir valmynd. } switch(numb) { case 1: { Person p = getPersoninfo(); //sækja upplýsingar um persónu. m_worker.createPerson(p); //býr til eintak af persónu. break; } case 2: { vector<Person> list = m_worker.getList(); // Sækja lista. printList(list); int sos_ans = askSearchOrSort(); while(sos_ans != 3) { if(sos_ans == 1) { int sort_ans = sortMenu(); if(sort_ans == 1) { printSorted(); } if(sort_ans == 2) { printSortedReverse(); } if(sort_ans == 3) { printSortedYear(); } if(sort_ans == 4) { printSortedYearReverse(); } } if(sos_ans == 2) { string remove; cout << "Enter name to remove: "; cin >> remove; vector<Person> removelist = m_worker.removeScientist(remove); // printList(removelist); } if(sos_ans == 3) { break; } sos_ans = askSearchOrSort(); } break; } case 3: // Search list { string search; cout << "Enter search word: "; cin >> search; vector<Person> searchlist = m_worker.searchScientist(search); printList(searchlist); break; } case 4: { m_worker.saveAllData(); // Geymum öll gögn áður en forriti er lokað. return; } } }
int main(){ struct Node *root = NULL; root = insert(root,10); insert(root,7); insert(root,16); insert(root,9); insert(root,8); insert(root,5); insert(root,22); printf("\nInorder : "); inorder(root); int key = 10; struct Node *keyNode = search(root,key); printf("\nSearch for %d : %d and parent: %d",key, keyNode == NULL ? -1 : keyNode->data); //printf("\nDeleting key %d : %d",key,delete_key(root,key)->data); printf("\nInorder : "); inorder(root); printf("\nIs tree BST : %d",isBst(root)); int key1 = 8; int key2 = 22; struct Node *lca = lowestCommonAncestor(root,key1,key2); printf("\nLowest Common Ancestor for %d and %d is : %d",key1,key2,lca == NULL ? -1 : lca->data); int a[] = {4, 2, 5, 1, 3}; int sa = sizeof(a)/sizeof(a[0]); printf("\nArray is printed in ascending order : "); printSorted(a,0,sa); int item = 5; struct Node* succ = inorder_successor(root,item); printf("\nInorder Successor for %d is : %d",item,succ == NULL ? -1 : succ->data); int k = 5; printf("\n%d smallest key in BST is %d",k,kth_smallest_bst(root,k)); { int key1 = 9,key2 = 22; printf("\nPrinting keys in range of %d and %d : ",key1,key2); printKeysInRange(root,key1,key2); } { int a[] = {1, 2, 3, 4, 5, 6, 7}; int sa = sizeof(a)/sizeof(a[0]); struct Node *root = sortedArrayToBalancedBst(a,0,sa-1); printf("\nInorder : "); inorder(root); } { Node* root = NULL; root = insert(root, 6); root = insert(root, -13); root = insert(root, 14); root = insert(root, -8); root = insert(root, 15); root = insert(root, 13); root = insert(root, 7); printf("\nInorder traversal of the given tree is: "); inorder(root); root = removeKeysOutsideRange(root, -10, 13); printf("\nInorder traversal of the modified tree is: "); inorder(root); } return 0; }