Originally Posted by
Paul22000
Haha! That's pretty much exactly what I have to do!
For the first part of the project, we did a linked list representation, so I just printed every linked list in the trie out to screen.
For the second part, now we have to do trees.
Kind of off topic, but what's a good way to print a tree to command prompt?? Did you actually do spaces between the nodes, or just say like "left child: value" etc?
I indented the nodes with spaces to get a graphical representation. Something like this. (Which I just typed up in this box and may not work properly.)
Code:
struct data {
int data;
struct data *next;
size_t nexts;
};
void dump_node(struct data *tree, int indent);
void print_indent(int indent);
void dump_data(struct data *tree) {
dump_node(tree, 0);
}
void dump_node(struct data *tree, int indent) {
size_t x;
print_indent(indent);
std::cout << tree->data << std::endl;
for(x = 0; x < tree->nexts; x ++) {
dump_node(tree, indent + 1);
}
}
void print_indent(int indent) {
int x;
for(x = 0; x < indent; x ++) std::cout << " ";
}
That's for a sort of trie-like structure, not a binary tree (which is what you have, right?). It might generate printouts like this.
You can print a tree however you like . . . I like to use whitespace, however. Your eye easily passes over it, and you still get an idea of what the structure looks like.