Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define pow2(n) (1 << (n))
- /* função recebe o nó raiz da árvore */
- void btree_print_node_graph(btree_node *node) {
- if (node == NULL) {
- return;
- }
- else {
- char fmt[9];
- btree_node **row1, **row2, **row_tmp;
- size_t rows, row, col;
- /* é necessário ter uma função que retorne a altura da árvore */
- rows = btree_height(node);
- row1 = malloc(sizeof(btree_node *) * pow2(rows));
- row2 = malloc(sizeof(btree_node *) * pow2(rows));
- row1[0] = node;
- for (row = 0; row < rows; row++) {
- size_t col2 = 0, cols = pow2(row);
- sprintf(fmt, "%%%dc", pow2(rows - (row + 1)));
- for (col = 0; col < cols; col++) {
- btree_node *n = row1[col];
- if (n != NULL) {
- /* altere aqui o nome dos campos */
- printf(fmt, *(char *) n->data);
- row2[col2++] = n->left;
- row2[col2++] = n->right;
- }
- else {
- printf(fmt, ' ');
- row2[col2++] = NULL;
- row2[col2++] = NULL;
- }
- if (!col) {
- sprintf(fmt, "%%%dc", pow2(rows - (size_t)(row)));
- }
- }
- printf("\n");
- row_tmp = row1;
- row1 = row2;
- row2 = row_tmp;
- }
- free(row1);
- free(row2);
- }
- }
Add Comment
Please, Sign In to add comment