/* Prints the structure of |node|, which is |level| levels from the top of the tree. */ static void print_tree_structure (const struct prb_node *node, int level) { /* You can set the maximum level as high as you like. Most of the time, you'll want to debug code using small trees, so that a large |level| indicates a ``loop'', which is a bug. */ if (level > 16) { printf ("[...]"); return; } if (node == NULL) return; printf ("%d", *(int *) node->prb_data); if (node->prb_link[0] != NULL || node->prb_link[1] != NULL) { putchar ('('); print_tree_structure (node->prb_link[0], level + 1); if (node->prb_link[1] != NULL) { putchar (','); print_tree_structure (node->prb_link[1], level + 1); } putchar (')'); } }
/* This function is modified from: http://adtinfo.org/libavl.html/Displaying-BST-Structures.html#119 Prints the structure of node, which is "level" levels from the top of the tree. */ void print_tree_structure (Tree *T, Node *node, int level, FILE *ofp) { /* You can set the maximum level as high as you like. Most of the time, you'll want to debug code using small trees, so that a large level indicates a “loop”, which is a bug. */ if (level > MAX_LEVEL) { fprintf(ofp, "[...]"); // printf ("[...]"); return; } if (node == T->sentinel) { return; } fprintf(ofp, "%d:%u", *((int*)node->key), node->rd); // printf ("%d:%u", *((int*)node->key), node->rd); if (node->child[L] != T->sentinel || node->child[R] != T->sentinel) { fprintf(ofp, "("); // putchar ('('); print_tree_structure (T, node->child[L], level + 1, ofp); if (node->child[R] != T->sentinel) { fprintf(ofp, ","); // putchar (','); print_tree_structure (T, node->child[R], level + 1, ofp); } fprintf(ofp, ")"); // putchar (')'); } }
/* Prints the entire structure of |tree| with the given |title|. */ void print_whole_tree (const struct prb_table *tree, const char *title) { printf ("%s: ", title); print_tree_structure (tree->prb_root, 0); putchar ('\n'); }
/* Prints the structure of |node|, which is |level| levels from the top of the tree. */ void print_tree_structure (struct rtrb_node *node, int level) { /* You can set the maximum level as high as you like. Most of the time, you'll want to debug code using small trees, so that a large |level| indicates a ``loop'', which is a bug. */ if (level > 16) { printf ("[...]"); return; } if (node == NULL) { printf ("<nil>"); return; } printf ("%d(", node->rtrb_data ? *(int *) node->rtrb_data : -1); if (node->rtrb_link[0] != NULL) print_tree_structure (node->rtrb_link[0], level + 1); fputs (", ", stdout); if (node->rtrb_rtag == RTRB_CHILD) { if (node->rtrb_link[1] == node) printf ("loop"); else print_tree_structure (node->rtrb_link[1], level + 1); } else if (node->rtrb_link[1] != NULL) printf (">%d", (node->rtrb_link[1]->rtrb_data ? *(int *) node->rtrb_link[1]->rtrb_data : -1)); else printf (">>"); putchar (')'); }
/* Can't have a datum longer than one line or carries over into another line. */ void read_data (FILE *fp, FILE *ofp, Tree *T) { char line[MAX_LENGTH] = {0}; char *token, *next_token; char *delim = " `~!@#$%^&*(){}[]|\\:;.,<>/?\"\n"; while (fgets (line, sizeof(line), fp) != NULL) { token = strtok_r (line, delim, &next_token); while (token) { parse_input (T, token); print_tree_structure (T, T->head->child[R], LEVEL, ofp); token = strtok_r (NULL, delim, &next_token); } } free (token); }
/* Prints the structure of |node|, which is |level| levels from the top of the tree. */ void print_tree_structure (struct tavl_node *node, int level) { int i; /* You can set the maximum level as high as you like. Most of the time, you'll want to debug code using small trees, so that a large |level| indicates a ``loop'', which is a bug. */ if (level > 16) { printf ("[...]"); return; } if (node == NULL) { printf ("<nil>"); return; } printf ("%d(", node->tavl_data ? *(int *) node->tavl_data : -1); for (i = 0; i <= 1; i++) { if (node->tavl_tag[i] == TAVL_CHILD) { if (node->tavl_link[i] == node) printf ("loop"); else print_tree_structure (node->tavl_link[i], level + 1); } else if (node->tavl_link[i] != NULL) printf (">%d", (node->tavl_link[i]->tavl_data ? *(int *) node->tavl_link[i]->tavl_data : -1)); else printf (">>"); if (i == 0) fputs (", ", stdout); } putchar (')'); }