/** * print_binary_tree.c * print out a binary tree * **/ #include <stdio.h> #include <stdlib.h> /* structures */ struct Node { int id; struct Node *left; struct Node *right; }; struct Tree { struct Node *root; }; /* global variables */ static int ID = 0; /* for test purpose */ /* auxiliary functions */ struct Node* spawn_node() { struct Node *node = (struct Node*)malloc(sizeof(struct Node)); if (node == NULL) { perror("Spawn Node Failure"); exit(1); } node->id = ID++; node->left = node->right = NULL; return node; } #define N 20 struct Tree* construct_tree(int flag) { int i; struct Tree* tree = (struct Tree*)malloc(sizeof(struct Tree)); if (tree == NULL) { perror("Construct Tree Failure"); exit(1); } tree->root = NULL; struct Node *nodes[N]; for (i=0; i<N; i++) { nodes[i] = spawn_node(); } /* construct tree structure */ if (flag == 0) { for (i=0; i<N-1; i++) { nodes[i]->left = nodes[i+1]; } } else if (flag == 1) { for (i=0; i<N-1; i++) { if (2*i+1 <=N-1) nodes[i]->left = nodes[2*i+1]; if (2*i+2 <=N-1) nodes[i]->right = nodes[2*i+2]; } } else if (flag == 2) { for (i=0; i<N-1; i++) { nodes[i]->right = nodes[i+1]; } } tree->root = nodes[0]; return tree; } #define ROOT 0 #define LEFT 1 #define RIGHT 2 void print_indent(int indent) { int i; for (i=0; i<indent; i++) printf(" "); } void print_sep_bits(int depth, int sep_bits) { int i=0; for (i=0; i<depth; i++) { if ((1<<i) & sep_bits) printf("| "); else printf(" "); } } void print_node(struct Node* node, int flag, int depth, unsigned int sep_bits) { /* print sep list and indent */ print_sep_bits(depth, sep_bits); switch (flag) { case ROOT: printf("+(%d)\n", node->id); break; case LEFT: printf("+-L(%d)\n", node->id); break; case RIGHT: printf("+-R(%d)\n", node->id); break; default: perror("Unknown Flag"); exit(1); } if (node->left && node->right) { depth += 1; sep_bits = sep_bits | (1<<depth); print_node(node->left, LEFT, depth, sep_bits); sep_bits = sep_bits & (~(1<<depth)); print_node(node->right, RIGHT, depth, sep_bits); } else if (node->left) { depth += 1; print_node(node->left, LEFT, depth, sep_bits); } else if (node->right) { depth += 1; print_node(node->right, RIGHT, depth, sep_bits); } else { /* nothing */ } } /* print_binary_tree */ void print_binary_tree(struct Tree *tree) { print_node(tree->root, ROOT, 0, 0); } void test(int flag) { struct Tree* tree = construct_tree(flag); print_binary_tree(tree); } int main(int argc, char *argv[]) { if (argc != 2) { printf("usage: print_binary_tree FLAG\n"); exit(1); } int flag = atoi(argv[1]); test(flag); return 0; }
chenqi@chenqi-OptiPlex-760:~/mypro/algorithms/binary-tree$ ./print_binary_tree 1
+(0)
+-L(1)
| +-L(3)
| | +-L(7)
| | | +-L(15)
| | | +-R(16)
| | +-R(8)
| | +-L(17)
| | +-R(18)
| +-R(4)
| +-L(9)
| | +-L(19)
| +-R(10)
+-R(2)
+-L(5)
| +-L(11)
| +-R(12)
+-R(6)
+-L(13)
+-R(14)node
print a binary tree non-recursive with constant extra memory space in O(n) timethis
/** * print_binary_tree_non_recursive.c * print out a binary tree * **/ #include <stdio.h> #include <stdlib.h> /* structures */ struct Node { int id; struct Node *parent; /* this field is a must for O(n) time performance with constant extra space */ struct Node *left; struct Node *right; }; struct Tree { struct Node *root; }; /* global variables */ static int ID = 0; /* for test purpose */ /* auxiliary functions */ struct Node* spawn_node() { struct Node *node = (struct Node*)malloc(sizeof(struct Node)); if (node == NULL) { perror("Spawn Node Failure"); exit(1); } node->id = ID++; node->left = node->right = node->parent = NULL; return node; } #define N 20 struct Tree* construct_tree(int flag) { int i; struct Tree* tree = (struct Tree*)malloc(sizeof(struct Tree)); if (tree == NULL) { perror("Construct Tree Failure"); exit(1); } tree->root = NULL; struct Node *nodes[N]; for (i=0; i<N; i++) { nodes[i] = spawn_node(); } /* construct tree structure */ if (flag == 0) { for (i=0; i<N-1; i++) { nodes[i]->left = nodes[i+1]; nodes[i+1]->parent = nodes[i]; } } else if (flag == 1) { for (i=0; i<N-1; i++) { if (2*i+1 <=N-1) { nodes[i]->left = nodes[2*i+1]; nodes[2*i+1]->parent = nodes[i]; } if (2*i+2 <=N-1) { nodes[i]->right = nodes[2*i+2]; nodes[2*i+2]->parent = nodes[i]; } } } else if (flag == 2) { for (i=0; i<N-1; i++) { nodes[i]->right = nodes[i+1]; nodes[i+1]->parent = nodes[i]; } } nodes[0]->parent = NULL; tree->root = nodes[0]; return tree; } int is_leaf(struct Node *n) { return (n->left==NULL && n->right==NULL); } /* current old */ #define LP 0 #define LR 1 #define PR 2 #define RP 3 void print_binary_tree_non_recursive(struct Tree *tree) { struct Node *current = tree->root; int flag = LP; while (current->left != NULL) current = current->left; /* left right root */ while (1) { if (flag == LP) { if (current->left != NULL) current = current->left; else if (current->right != NULL) { current = current->right; flag = RP; } else { /* leaf */ printf("%d\t", current->id); if (current->parent == NULL) break; if (current->parent->right != NULL) { flag = LR; current = current->parent->right; } else { current = current->parent; flag = PR; } } } else if (flag == PR) { printf("%d\t", current->id); if (current->parent == NULL) break; else if (current->parent->left == current) { if (current->parent->right != NULL) { current = current->parent->right; flag = LR; } else { current = current->parent; flag = PR; } } else { flag = PR; current = current->parent; } } else if (flag == LR || flag == RP) { if (current->left != NULL) { current = current->left; flag = LP; } else if (current->right != NULL) { current = current->right; flag = RP; } else { printf("%d\t", current->id); current = current->parent; flag = PR; } } } } void test(int flag) { struct Tree* tree = construct_tree(flag); print_binary_tree_non_recursive(tree); printf("\n"); } int main(int argc, char *argv[]) { if (argc != 2) { printf("usage: print_binary_tree FLAG\n"); exit(1); } int flag = atoi(argv[1]); test(flag); return 0; }