void printlevel(struct node* root, int level) { if (root == NULL) return; if (level == 1) { printf("%d ", root->data); return; } else { printlevel(root->right, level-1); printlevel(root->left, level-1); } }
void printlevel(struct node* node, int n) { if(node == NULL) return; if(n == 0) { printf(" %d ", node->data); return; } printlevel(node->left, n-1); printlevel(node->right, n-1); }
/* For debugging, mainly. We print the tree with the head node unnumbered and the root node called level 0. In Knuth algorithms where we have p[k] when k is zero k refers to the head node. Handy as then the root node is not special at all. But here it just looks better as shown, perhaps. The ordering here is so that if you turned an output page with the left side at the top then the tree sort of just shows up nicely in what most think of as a normal way. */ static void tdump_inner(struct ts_entry *t, char *(keyprint)(const void *), const char *descr, int level) { char * keyv = ""; if(!t) { return; } tdump_inner(t->rlink,keyprint,"right",level+1); printlevel(level); if(t->keyptr) { keyv = keyprint(t->keyptr); } printf("0x%08lx <keyptr 0x%08lx> <%s %s> <bal %3d> <l 0x%08lx> <r 0x%08lx> %s\n", (unsigned long)t, (unsigned long)t->keyptr, t->keyptr?"key ":"null", keyv, t->balance, (unsigned long)t->llink,(unsigned long)t->rlink, descr); tdump_inner(t->llink,keyprint,"left ",level+1); }
/* only check "around" y */ static void checkboard(int y) { int a, b, c, r = 0, ey = y + 4; if (ey>=24) ey=24; for (b=y; b<ey; b++) { c = 0; for (a=4; a<WIDTH+4; a++) { if (board[a][b]) c++; } if (c == WIDTH) { setbg(white); horizontal(40-WIDTH, 40+WIDTH-1, b-3); moveto(1,24); flush(); usleep(100000); setbg(black); horizontal(40-WIDTH, 40+WIDTH-1, b-3); moveto(1,24); flush(); usleep(100000); removeline(b); r++; } } if (r==4) { /* slower if you make four rows at once! */ speed--; if (speed < minspeed) speed = minspeed; printlevel(); } }
static void incrlevel(int i) { level += i; speed += i; if (level > maxlevel) level = maxlevel; if (speed > maxspeed) speed = maxspeed; printlevel(); }
// prints a given level of a tree - O(2^n) -> O(sum(2^(l-1)) [l=level] void BinaryTree::printlevel(treenode* &node, int level, int start_lvl) { // tree is empty, nothing to print! if (node == 0) return; // we are on the wanted level, print item if (level == start_lvl) { std::cout << node->data << " "; return; } // we are not on the wanted level, go deeper if (node->left != 0) printlevel(node->left, level, start_lvl+1); if (node->right != 0) printlevel(node->right, level, start_lvl+1); }
void levelorder(struct node* root) { int h = depth(root); printf("Height of the tree is %d and Level Order is\n", h); for (int i=1; i <=h; i++) { printlevel(root, i); } printf("\n"); }
void printlevelorder(struct node* node) { int h = maxDepth(node); printf("\n height of tree = %d\n", h); int i; for(i = 0; i< h; i++) { printlevel(node, i); printf("\n"); } }
static void value_only_walk_entry(const void *data,DW_VISIT x,int level) { VALTYPE val = (VALTYPE)data; printlevel(level); printf("Walk on node %s 0x%lu\n", x == dwarf_preorder?"preorder": x == dwarf_postorder?"postorder": x == dwarf_endorder?"endorder": x == dwarf_leaf?"leaf": "unknown", (unsigned long)val); return; }
static void walk_entry(const void *mt_data,DW_VISIT x,int level) { const struct example_tentry *m = *(const struct example_tentry **)mt_data; printlevel(level); printf("Walk on node %s %u %s \n", x == dwarf_preorder?"preorder": x == dwarf_postorder?"postorder": x == dwarf_endorder?"endorder": x == dwarf_leaf?"leaf": "unknown", m->mt_key,m->mt_name); return; }
main() { struct Q *queue=createQ(MAX); struct BT *rt=NULL; int i; for(i=0;i<10;i++) insert(&rt,queue,i); printlevel(rt); printf("\n"); }
/* For debugging. This prints the nodes with the parent (in each case) in between the children. So it is a tree with root at the left. */ static void dumptree_inner(const struct ts_entry *t, char *(* keyprint)(const void *), const char *descr, int level) { char *v = ""; if(!t) { return; } dumptree_inner(t->rlink,keyprint,"left ",level+1); if(t->keyptr) { v = keyprint(t->keyptr); } printlevel(level); printf("0x%08x <keyptr 0x%08x> <%s %s> <l 0x%08x> <r 0x%08x> %s\n", (unsigned)t, (unsigned)t->keyptr, t->keyptr?"key ":"null", v, (unsigned)t->llink,(unsigned)t->rlink, descr); dumptree_inner(t->llink,keyprint,"right",level+1); }
static void game(void) { char c; int counter = sspeed, rs, a, blocks = 0, paused = 0; gamescreen(); clearboard(); nexts = rnd()%28; nextc = rnd()%7 + 1; newshape(); score = view = 0; if (view) drawview(nexts, nextc); level = 1; speed = sspeed; printscore(); printlevel(); for(;;) { if (paused) { gametick(); c = tgetchar(); switch(c) { case 'q': goto stopgame; case 'p': paused = 0; moveto(68,5); setbg(black); printf(" "); normal(); flush(); default: break; } continue; } if (counter <= 0) { counter = 14 - speed; if (shapefits(current.s, current.x, current.y+1)) { drawshape(current.s, 0, 40-WIDTH-8+current.x*2, current.y-3); current.y++; drawshape(current.s, current.color, 40-WIDTH-8+current.x*2, current.y-3); } else { markboard(current.s, current.color, current.x, current.y); checkboard(current.y); incrscore(1); newshape(); if (!shapefits(current.s, current.x, current.y)) goto stopgame; blocks++; if (blocks == blocksperlevel) { blocks = 0; incrlevel(1); } if (view) drawview(nexts, nextc); } } counter--; moveto(1,24); flush(); gametick(); c = tgetchar(); switch(c) { case 'q': goto stopgame; case 'j': if (shapefits(current.s, current.x-1, current.y)) { drawshape(current.s, 0, 40-WIDTH-8+current.x*2, current.y-3); current.x--; drawshape(current.s, current.color, 40-WIDTH-8+current.x*2, current.y-3); } break; case 'l': if (shapefits(current.s, current.x+1, current.y)) { drawshape(current.s, 0, 40-WIDTH-8+current.x*2, current.y-3); current.x++; drawshape(current.s, current.color, 40-WIDTH-8+current.x*2, current.y-3); } break; case 'k': rs = (current.s & ~0x03) | ((current.s + 1) & 0x03); if (shapefits(rs, current.x, current.y)) { drawshape(current.s, 0, 40-WIDTH-8+current.x*2, current.y-3); current.s = rs; drawshape(current.s, current.color, 40-WIDTH-8+current.x*2, current.y-3); } break; case ' ': for (a=1; ; a++) { if (!shapefits(current.s, current.x, current.y+a)) break; } if (shapefits(current.s, current.x, current.y+a-1)) { drawshape(current.s, 0, 40-WIDTH-8+current.x*2, current.y-3); current.y += a-1; drawshape(current.s, current.color, 40-WIDTH-8+current.x*2, current.y-3); } incrscore(view ? (a-1) * 3 / 4 : a-1); break; case 'n': view ^= 1; if (view) drawview(nexts, nextc); else clearview(); break; case 'p': paused = 1; moveto(68,5); setfg(white); setbg(blue); printf(" PAUSED!!! "); moveto(1,24); flush(); default: break; } } stopgame: setbg(white); rect(20,8,60,12); setbg(red); frect(21,9,59,11); setfg(white); bold(); moveto(30,10); printf(" G A M E O V E R "); moveto(1,24); flush(); for(;;) { c = tgetchar(); if (c > 0) return; } }