void BST::printRecursive(Node* node) { if(node->left) printRecursive(node->left); std::cout << "Node val : " << node->val << std::endl; if (node->right) printRecursive(node->right); }
void printRecursive(pnode root, int path[], int len) { int i = 0; if (root->left == NULL && root->right == NULL) { // leaf node for (i = 0; i < len; i++) { printf("%d--", path[i]); } printf("%d\n", root->data); } else { path[len++] = root->data; if (root->left) printRecursive(root->left, path, len); if (root->right) printRecursive(root->right, path, len); } }
string Context::printGraphToString() { stringstream stream; set<NodeRef> traversedNodes; printRecursive( stream, getOutput(), 0, traversedNodes ); if( ! mAutoPulledNodes.empty() ) { stream << "(auto-pulled:)" << endl; for( const auto& node : mAutoPulledNodes ) printRecursive( stream, node, 0, traversedNodes ); } return stream.str(); }
void printParseTree(parseTree PT, char *outfile) { if(PT == NULL) { printf("The source file contains errors and hence parse tree cannot be printed.\n"); return; } FILE *fp = fopen(outfile, "w"); fprintf(fp,"lexemeCurrentNode lineno token valueIfNumber parentNodeSymbol isLeafNode NodeSymbol\n"); printRecursive(PT, fp); /*parseTree curNode = PT; fprintf(fp, "----------------- - ---- - ROOT no <%s>\n",grammarSymbols[curNode->tk->sym]); do { curNode = curNode->child; while(curNode->left != NULL) curNode = curNode->left; printGeneral(curNode, fp); } while(curNode->child != NULL); while(curNode->right != NULL) { curNode = curNode->right; } fclose(fp); */ }
void BinarySearchTree<T>::printRecursive(BinaryTreeNode<T> *node){ if(!node){ return; } if(node->getLeft()) { printRecursive(node->getLeft()); } std::cout << node->getData() << std::endl; if(node->getRight()){ printRecursive(node->getRight()); } };
void singleQueue<t>::printRecursive(node* n) { if(n) { std::cout << n -> data << std::endl; printRecursive(n -> next); } }
void singleQueue<t>::printLink() { if (front != NULL) { printRecursive(front); } else std::cout << "Queue is EMPTY." << std::endl; }
//solution 1 void printRecursive(std::vector<bool> & bits, int index, int depth, int left) { if(left){ bits[index] = true; printRecursive(bits, index + 1, depth + 1, left - 1); if(depth > 0){ bits[index] = false; printRecursive(bits, index + 1, depth - 1, left); } }else{ assert(index <= int(bits.size())); for(int i = 0;i < index;++i) std::cout<<(bits[i] ? '(' : ')'); for(size_t i = index;i < bits.size();++i) std::cout<<')'; std::cout<<"\n"; } }
void printParanthesis(int n) { if(!n) return; assert(n > 0); std::vector<bool> bits(n * 2); bits[0] = true; printRecursive(bits, 1, 1, n - 1); }
void printRecursive(int *cascadingIndex, Table *stringTable) { Pair *pair; pair = stringTable->table[*cascadingIndex]; (pair->useCount)++; if (pair->prefix != 0) //if still prefixes left { *cascadingIndex = pair->prefix; printRecursive(cascadingIndex, stringTable); } printf("%c", pair->c); }
int printRecursive(int EV_num,int EV_rec) { if ((EV_rec==0)) { return EV_num; } else { printf("%d\n",EV_num); EV_rec = (EV_rec-1); return printRecursive(EV_num, EV_rec); } }
void printRecursive(parseTree curNode, FILE* fp) { printGeneral(curNode, fp); if(curNode->child == NULL) { return; } curNode = curNode->child; while(curNode->left != NULL) curNode = curNode->left; while(curNode != NULL) { printRecursive(curNode, fp); curNode = curNode->right; } }
int main() { int EV_current; int EV_count; int EV_total; struct EV_LameStruct * EV_digits; EV_digits = (struct EV_LameStruct*)malloc(sizeof(struct EV_LameStruct)); EV_count = 0; EV_digits->EV_one = 0; EV_digits->EV_two = 0; EV_digits->EV_three = 0; EV_digits->EV_four = 0; EV_digits->EV_five = 0; EV_digits->EV_six = 0; EV_digits->EV_seven = 0; EV_digits->EV_eight = 0; EV_digits->EV_nine = 0; scanf("%d", &EV_current); while ((EV_current!=0)) { if ((EV_current==1)) { EV_digits->EV_one = (EV_digits->EV_one+1); } else { if ((EV_current==2)) { EV_digits->EV_two = (EV_digits->EV_two+1); } else { if ((EV_current==3)) { EV_digits->EV_three = (EV_digits->EV_three+1); } else { if ((EV_current==4)) { EV_digits->EV_four = (EV_digits->EV_four+1); } else { if ((EV_current==5)) { EV_digits->EV_five = (EV_digits->EV_five+1); } else { if ((EV_current==6)) { EV_digits->EV_six = (EV_digits->EV_six+1); } else { if ((EV_current==7)) { EV_digits->EV_seven = (EV_digits->EV_seven+1); } else { if ((EV_current==8)) { EV_digits->EV_eight = (EV_digits->EV_eight+1); } else { EV_digits->EV_nine = (EV_digits->EV_nine+1); } } } } } } } } scanf("%d", &EV_current); } printf("%d\n",countTotal(EV_digits)); printRecursive(convertBinary(1), EV_digits->EV_one); printRecursive(convertBinary(2), EV_digits->EV_two); printRecursive(convertBinary(3), EV_digits->EV_three); printRecursive(convertBinary(4), EV_digits->EV_four); printRecursive(convertBinary(5), EV_digits->EV_five); printRecursive(convertBinary(6), EV_digits->EV_six); printRecursive(convertBinary(7), EV_digits->EV_seven); printRecursive(convertBinary(8), EV_digits->EV_eight); printRecursive(convertBinary(9), EV_digits->EV_nine); return 0; }
void BST::printTree() { printRecursive(root); }
int main(int argc, char **argv) { clock_t begin, end; double time_spent; begin = clock(); int NUM_FINDCODE_CALLS = 0; int NUM_HASHCELLS_VISITED = 0; int c = 0; //char in (pref, char) int prefix = EMPTY_CODE; int index = EMPTY_CODE; int MAXBITS = 15; //default char DUMP_TO[PATH_MAX]; char INIT_FROM[PATH_MAX]; int PRUNE = -1; Table *stringTable = initStringTable(MAXBITS); //setting params from cmd line argv's STARTING FROM 2 BC ENCODE/DECODE for (int i = 2; i < argc; ++i) { if (strcmp(argv[i], "-m") == 0) { //ERROR CHECK FOR IF NEXT ARGV IS NOT INTEGER STRING??? MAXBITS = atoi(argv[i + 1]); i++; } else if (strcmp(argv[i], "-o") == 0) { if(i + 1 == argc) { fprintf(stderr, "usage: encode [-m MAXBITS | -o NAME | -i NAME | -p USED]* OR decode [-o NAME]*\n"); exit(EXIT_FAILURE); //EDGE CASE: IF -O -O HI, THEN DO WE DUMP TO '-O' AND SIGNAL ERROR ON HI? OR DO WE DUMP TO -O, AND OVERRIDE WITH DUMPING TO 'HI'? } strncpy(DUMP_TO, argv[i + 1], strlen(argv[i + 1])); i++; } else if (strcmp(argv[i], "-i") == 0) { if(i + 1 == argc) { fprintf(stderr, "usage: encode [-m MAXBITS | -o NAME | -i NAME | -p USED]* OR decode [-o NAME]*\n"); exit(EXIT_FAILURE); } strncpy(INIT_FROM, argv[i + 1], strlen(argv[i + 1])); i++; } else if (strcmp(argv[i], "-p") == 0) { if(i + 1 == argc) { fprintf(stderr, "usage: encode [-m MAXBITS | -o NAME | -i NAME | -p USED]* OR decode [-o NAME]*\n"); exit(EXIT_FAILURE); } PRUNE = atoi(argv[i + 1]); //test if not an integer; what if prune is 0? w i++; if(PRUNE){} } else { fprintf(stderr, "usage: encode [-m MAXBITS | -o NAME | -i NAME | -p USED]* OR decode [-o NAME]*\n"); exit(EXIT_FAILURE); } } fprintf(stderr, "argv[0]: %s\n", argv[0]); if(strcmp(argv[1], "encode") == 0) { //ENCODE while((c = getchar()) != EOF) { index = findCode(prefix, c, stringTable, &NUM_FINDCODE_CALLS, &NUM_HASHCELLS_VISITED); if (index != -1) //(pref, char) found in stringTable { prefix = index; (stringTable->table[prefix]->useCount)++; continue; } else { //not found in stringTable putBits(stringTable->nBits, prefix); insertToTable(prefix, c, &stringTable, PRUNE); prefix = findCode(0, c, stringTable, &NUM_FINDCODE_CALLS, &NUM_HASHCELLS_VISITED); //start search again at finalChar of newly found sequence (stringTable->table[prefix]->useCount)++; } } putBits(stringTable->nBits, prefix); flushBits(); // printTable(stringTable, "encode"); // fprintf(stderr, "\nNUM_FINDCODE_CALLS: %d\nNUM_HASHCELLS_VISITED: %d\nAVG_SEARCH_RATIO: %d\n", NUM_FINDCODE_CALLS, NUM_HASHCELLS_VISITED, NUM_HASHCELLS_VISITED / NUM_FINDCODE_CALLS); } else if(strcmp(argv[1], "decode") == 0) { //DECODE char ENCODE_OR_DECODE[25] = "decode"; int oldIndex = 0, firstCharOfNewIndex = 0; //used to build stringTable, 1 step behind encode int newIndex = 0; int cascadingIndex = 0; //used to print a code string recursively int kwkwk = 0; //used in kwkwk case int prunedThisPass = 0; int HIT_THE_LIMIT = 0; while( (newIndex = cascadingIndex = getBits(stringTable->nBits)) != EOF) { //nBits was incremented just before to accomodate increased nBits in ENCODE that hasn't been automatically detected in DECODE //and we decrement it now bc it will be automatically incremented in INSERTTABLE //but if == MAXBITS, then we did not artificially increment nBits last round if(stringTable->last + 1 == stringTable->size && !HIT_THE_LIMIT ) {//2nd condition happens when table is already maxSize, in which case no more doubling or pruning happens if(!prunedThisPass) //when pruning results in an at-limit table stringTable->nBits--; if(stringTable->size != 1<<stringTable->nBits){ fprintf(stderr, "size: %d, nBits: %d\n", stringTable->size, stringTable->nBits); exit(EXIT_FAILURE); } else { } } if(newIndex > stringTable->last) //newIndex is an unknown code (kwkwk abnormal case) { kwkwk = 1; if(newIndex < stringTable->size) stringTable->table[newIndex]->useCount++; //useCount usually incremented in printRecursive function, which will not receive newIndex in this case cascadingIndex = oldIndex; } printRecursive(&cascadingIndex, stringTable); if (kwkwk == 1) { printf("%c", firstCharOfNewIndex); //output should be oldCode, oldCode, firstCharOfOldCode kwkwk = 0; } if(cascadingIndex >= stringTable->size) { fprintf(stderr, "cascadingIndex: %d, size: %d\n", cascadingIndex, stringTable->size); exit(EXIT_FAILURE); } firstCharOfNewIndex = stringTable->table[cascadingIndex]->c; //finalK = char(c) if(oldIndex != 0 && !prunedThisPass) { //every time except first time, and after pruning, add new code to table insertToTable(oldIndex, firstCharOfNewIndex, &stringTable, PRUNE); } prunedThisPass = 0; oldIndex = newIndex; if(stringTable->last + 1 >= stringTable->size) { //decode is one step behind encode; deocde inserts new code every time it reads a code, starting from 2nd code //encode adds a code at every code starting from 1st code. CURRENT CODE + firstChar of NEXT CODE was the last code //encode tried to add to table and PRUNED or DOUBLE'd //=>PRUNING or DOUBLING:: //if DOUBLING: next code will increment nBits //if PRUNING: next code is encoded from a PRUNED table, not CURRENT table (and we don't insert next round) if(stringTable->nBits != stringTable->MAXBITS) { if(stringTable->nBits == 8){ exit(EXIT_FAILURE); } stringTable->nBits++; } else { //next insert exceeds table size, and table size is MAXBITS size, so PRUNE HIT_THE_LIMIT = 1; if(PRUNE != -1) { prune(&stringTable, PRUNE, ENCODE_OR_DECODE); prunedThisPass = 1; if (stringTable->last + 1 == stringTable->size) { fprintf(stderr, "HERE!!! last: %d, size: %d\n", stringTable->last, stringTable->size); char temp[10] = "encode"; printTable(stringTable, temp); } if(stringTable->last + 1 != 1<<stringTable->MAXBITS) HIT_THE_LIMIT = 0; } } } } // printTable(stringTable, "decode"); } moses(stringTable); end = clock(); time_spent = (double)(end - begin) / CLOCKS_PER_SEC; fprintf(stderr, "time_spent: %f\n", time_spent); }
void printPaths(pnode root) { int path[1000]; if (!root) return; printf("paths in the tree\n"); printRecursive(root, path, 0); }
void BinarySearchTree<T>::print(){ printRecursive(root); };