void print_pre(struct btree *root) { if(root == NULL){return;} printf("%d ",root->data); print_pre(root->left); print_pre(root->right); }
void print_pre (struct arvore * tree)//imprime em pre-ordem { if (tree==NULL) return; printf(" %d ",tree->chave); print_pre(tree->esq); print_pre(tree->dir); }
int main() { struct btree *n1, *root; int i; int a[10]={25,32,24,38,12,6,49,35,10,28}; // int a[10] = {5, 3, 7, 2, 4, 1, 6, 8, 9, 4}; root = NULL; for(i=0;i<10;i++) { n1 = malloc(sizeof(struct btree)); n1->data = a[i]; n1->right = n1->left = NULL; root = insert(root, n1); } printf("Tree data : "); print_pre(root); i = search(root,a[8]); if(i!=0) printf("\nNumber not found..\n"); else printf("\nNumber %d found..\n",a[8]); i = isBalanced(root); print_level_order(root); dfs(root); return 0; }
void backtrack( struct point p ) { struct point stacktop; struct point tmp; struct point branch_point; // 1. struct point stacktop = get the top item from stack stacktop = get_top(); printf( "top is (%d, %d) \n", stacktop.row, stacktop.col ); // 2. get stacktop 's father point = branch_point branch_point = pre[stacktop.row][stacktop.col]; printf( "father is (%d, %d) \n", branch_point.row, branch_point.col); // 3. from p to branch_point tmp = p; while( !(tmp.row==branch_point.row && tmp.col==branch_point.col) ) { struct point save; maze[tmp.row][tmp.col] = 0; save = tmp; tmp = pre[tmp.row][tmp.col]; pre[save.row][save.col].row = 0; pre[save.row][save.col].col = 0; print_maze(); print_pre(); //getchar(); } }
int main(int argc, char **argv) { FILE *f; char *fn, *hb; if(argc < 2){ fn = "bits.h"; hb = "__BITS_H__"; f = stdout; } else { char *p; fn = argv[1]; hb = malloc(strlen(fn) + 5); sprintf(hb, "__%s__", fn); for(p = hb; *p; p++){ if(!isalnum((int) *p)) *p = '_'; } f = fopen(argv[1], "w"); } print_pre(f); #ifndef HAVE_INT8_T try_signed (f, 8); #endif /* HAVE_INT8_T */ #ifndef HAVE_INT16_T try_signed (f, 16); #endif /* HAVE_INT16_T */ #ifndef HAVE_INT32_T try_signed (f, 32); #endif /* HAVE_INT32_T */ #ifndef HAVE_INT64_T try_signed (f, 64); #endif /* HAVE_INT64_T */ #ifndef HAVE_U_INT8_T try_unsigned (f, 8); #endif /* HAVE_INT8_T */ #ifndef HAVE_U_INT16_T try_unsigned (f, 16); #endif /* HAVE_U_INT16_T */ #ifndef HAVE_U_INT32_T try_unsigned (f, 32); #endif /* HAVE_U_INT32_T */ #ifndef HAVE_U_INT64_T try_unsigned (f, 64); #endif /* HAVE_U_INT64_T */ print_post(f); fclose(f); return 0; }
int menu ()//funcao que imprime o menu e executa a opcao escolhida { int i,j,n,a; printf("\nO que deseja fazer agora? Digite o numero com sua opcao:\n"); printf("1. Adicionar novos numeros.\n2. Remover um numero.\n3. Buscar um numero.\n4. Imprimir a arvore.\n5. Sair\nOpcao: "); scanf("%d",&i); switch (i)//pula para a opcao escolhida. { case 1: { printf("Quantos numeros pretende entrar? "); scanf("%d",&n); while (n<1) { printf("Entre com pelo menos um numero. Quantos valores deseja entrar? "); scanf("%d",&n); } printf("Entre com os numeros:\n"); for (j=0;j<n;j++) { scanf("%d",&a); adicionar (a); } printf("Numeros adicionados!\n"); return 0; } case 2: { printf("Qual numero voce deseja remover? "); scanf("%d",&a); if (busca(root,a)==1) { remover(a); printf("Item removido!\n"); return 0; } else { printf("O item nao pode ser removido pois nao se encontra na arvore.\n"); return 0; } } case 3: { printf("Qual numero deseja buscar? "); scanf("%d",&a); if (busca(root,a)==1) printf("Numero encontrado! Ele ja esta na arvore.\n"); else printf("Numero nao encontrado.\n"); return 0; } case 4://conta com um switch interno, para que se escolha como sera impressa a arvore { printf("Como voce deseja imprimi-los?\n1. Em ordem.\n2. Pre-ordem.\n3. Pos-ordem.\n4. Labelled bracketing.\nOpcao: "); scanf("%d",&j); switch (j) { case 1: { print_in(root); printf("\n"); return 0; } case 2: { print_pre(root); printf("\n"); return 0; } case 3: { print_pos(root); printf("\n"); return 0; } case 4: { print_col(root); printf("\n"); return 0; } default: { printf("Ocorreu um erro. Tente novamente.\n"); return 0; } } } case 5: return 1; default: { printf("Ocorreu um erro. Tente novamente.\n"); return 0; } } }
int main(void) { struct point p; int ways = 0; printf( "hello, maze! \n" ); print_maze(); /* init start situation */ p = start; maze[p.row][p.col] = 2; push(p); print_maze(); while( !is_empty() ) { print_stack(); /* get the saved point from stack */ p = pop(); /* judge if p is target point */ if( p.row == target.row && p.col == target.col ) { printf( "target is found! \n" ); print_stack(); /* print backward road */ printf( "%d solution as follows: \n", ++ways ); print_solution(); getchar(); // assume p is the target point backtrack( p ); continue; } int flag = 0; /* expend p to UP, LEFT, DOWN, RIGHT */ // look UP if( p.row-1 >= 0 && maze[p.row-1][p.col] == 0 ) flag = visit( p.row-1, p.col, p ); // look LEFT if( p.col-1 >= 0 && maze[p.row][p.col-1] == 0 ) flag = visit( p.row, p.col-1, p ); // look DOWN if( p.row+1 < MAX_ROW && maze[p.row+1][p.col] == 0 ) flag = visit( p.row+1, p.col, p ); // look RIGHT if( p.col+1 < MAX_COL && maze[p.row][p.col+1] == 0 ) flag = visit( p.row, p.col+1, p ); // backtrack clear 0 if( flag == 0 ) backtrack( p ); print_maze(); print_pre(); print_stack(); getchar(); } printf( "there is %d ways out! \n", ways ); return 0; }