logo

Pretty print a binary tree in C

#include <math.h> #include <stddef.h> #include <stdlib.h> #include <stdio.h> #include <string.h> typedef struct TreeNode { int val; struct TreeNode* left; struct TreeNode* right; } TreeNode; TreeNode* new_node(int val) { TreeNode* n = malloc(sizeof(TreeNode)); n->val = val; n->left = NULL; n->right = NULL; return n; } void print_depth_first(TreeNode* root) { if (root == NULL) { return; } TreeNode* process_stack[100]; size_t process_stack_num = 0; process_stack[0] = root; process_stack_num++; while (process_stack_num > 0) { TreeNode* current = process_stack[process_stack_num - 1]; process_stack_num--; // if (current->val == 4) { // printf("%d(yay we found the node 4) ", current->val); // break; // } else { printf("%d ", current->val); // } if (current->right != NULL) { process_stack[process_stack_num] = current->right; process_stack_num++; } if (current->left != NULL) { process_stack[process_stack_num] = current->left; process_stack_num++; } } } void print_breadth_first(TreeNode* root) { if (root == NULL) { return; } TreeNode* process_queue[100]; size_t process_queue_num = 0; process_queue[0] = root; process_queue_num++; while (process_queue_num > 0) { TreeNode* current = process_queue[0]; int i; for (i = 0; i < process_queue_num - 1; i++) { process_queue[i] = process_queue[i + 1]; } process_queue_num--; // if (current->val == 4) { // printf("%d (yay, we found 4) ", current->val); // } else { printf("%d ", current->val); // } if (current->left != NULL) { process_queue[process_queue_num] = current->left; process_queue_num++; } if (current->right != NULL) { process_queue[process_queue_num] = current->right; process_queue_num++; } } } void free_binary_tree_queue(TreeNode* root) { TreeNode* process_queue[100]; size_t process_queue_num = 0; process_queue[0] = root; process_queue_num++; int i = 0; while (i < process_queue_num) { TreeNode* current = process_queue[i++]; if (current->left != NULL) { process_queue[process_queue_num] = current->left; process_queue_num++; } if (current->right != NULL) { process_queue[process_queue_num] = current->right; process_queue_num++; } } printf("\nFreeing: "); for (i = process_queue_num - 1; i >= 0; i--) { printf("%d ", process_queue[i]->val); free(process_queue[i]); } } void free_binary_tree_stack(TreeNode* root) { TreeNode* process_stack[100]; size_t process_stack_num = 0; process_stack[0] = root; process_stack_num++; while (process_stack_num > 0) { TreeNode* current = process_stack[process_stack_num - 1]; process_stack_num--; if (current->right != NULL) { process_stack[process_stack_num] = current->right; process_stack_num++; } if (current->left != NULL) { process_stack[process_stack_num] = current->left; process_stack_num++; } free(current); } } void free_binary_tree_recursive(TreeNode* node) { if (node == NULL) { return; } if (node->left != NULL) { free_binary_tree_recursive(node->left); } if (node->right != NULL) { free_binary_tree_recursive(node->right); } free(node); } void insert_node(TreeNode** root, int val) { if (*root == NULL) { *root = new_node(val); return; } TreeNode* process_queue[100]; size_t num_process_queue = 0; process_queue[0] = *root; num_process_queue++; while (num_process_queue > 0) { TreeNode* current = process_queue[0]; for (size_t i = 0; i < num_process_queue - 1; i++) { process_queue[i] = process_queue[i + 1]; } num_process_queue--; // checking and inserting if (current->left == NULL) { current->left = new_node(val); return; } if (current->right == NULL) { current->right = new_node(val); return; } if (current->left != NULL) { process_queue[num_process_queue++] = current->left; } if (current->right != NULL) { process_queue[num_process_queue++] = current->right; } } } size_t calculate_size(TreeNode* root) { if (root == NULL) { return 0; } size_t result = 0; TreeNode* process_stack[100]; size_t num_process_stack = 0; process_stack[num_process_stack++] = root; while (num_process_stack > 0) { TreeNode* node = process_stack[num_process_stack - 1]; num_process_stack--; result++; if (node->left != NULL) { process_stack[num_process_stack++] = node->left; } if (node->right != NULL) { process_stack[num_process_stack++] = node->right; } } return result; } size_t calculate_size_recursive(TreeNode* node) { if (node == NULL) { return 0; } return 1 + calculate_size_recursive(node->left) + calculate_size_recursive(node->right); } size_t calculate_height_recursive(TreeNode* node) { if (node == NULL) { return 0; } size_t left_height = calculate_height_recursive(node->left); size_t right_height = calculate_height_recursive(node->right); return 1 + (left_height > right_height ? left_height : right_height); } size_t calculate_height(TreeNode* root) { if (root == NULL) { return 0; } size_t result = 0; TreeNode* process_stack[100]; size_t num_process_stack = 0; process_stack[num_process_stack++] = root; while (num_process_stack > 0) { size_t initial_size = num_process_stack; for (size_t i = 0; i < initial_size; i++) { TreeNode* current = process_stack[i]; if (current->left != NULL) { process_stack[num_process_stack++] = current->left; } if (current->right != NULL) { process_stack[num_process_stack++] = current->right; } } memmove(process_stack, process_stack + initial_size, sizeof(TreeNode*) * (num_process_stack - initial_size)); num_process_stack -= initial_size; result++; } return result - 1; } void simple_print_binary_tree(TreeNode* root) { if (root == NULL) { return; } TreeNode* process_stack[100]; size_t num_process_stack = 0; process_stack[num_process_stack++] = root; while (num_process_stack > 0) { TreeNode* current = process_stack[num_process_stack - 1]; num_process_stack--; if (current->left != NULL) { process_stack[num_process_stack++] = current->left; printf("%d -l> %d\n", current->val, current->left->val); } if (current->right != NULL) { process_stack[num_process_stack++] = current->right; printf("%d -r> %d\n", current->val, current->right->val); } } } void pretty_print_binary_tree(TreeNode* root) { if (root == NULL) { return; } size_t height = calculate_height(root); TreeNode* process_stack[100]; size_t num_process_stack = 0; process_stack[num_process_stack++] = root; int layer = 0; while (layer <= height) { size_t initial_size = num_process_stack; for (int i = 0; i < initial_size; i++) { TreeNode* current = process_stack[i]; int k = height - layer; int sk = pow(2, k) + (pow(2, k) - 1) * 3; int sk_plus_one = pow(2, k + 1) + (pow(2, k + 1) - 1) * 3; if (i == 0) { if (current == NULL) { printf("%*s---", sk, ""); } else { printf("%*s%03d", sk, "", current->val); } } else { if (current == NULL) { printf("%*s---", sk_plus_one, ""); } else { printf("%*s%03d", sk_plus_one, "", current->val); } } process_stack[num_process_stack++] = current == NULL ? NULL : current->left; process_stack[num_process_stack++] = current == NULL ? NULL : current->right; } printf("\n"); memmove(process_stack, process_stack + initial_size, (num_process_stack - initial_size) * sizeof(TreeNode*)); num_process_stack -= initial_size; layer++; } } // 0 // / \ // 1 2 // / \ / \ //3 4 5 6 int main(int argc, char* argv[]) { TreeNode* root = NULL; insert_node(&root, 0); insert_node(&root, 1); insert_node(&root, 2); insert_node(&root, 3); insert_node(&root, 4); insert_node(&root, 5); insert_node(&root, 6); insert_node(&root, 7); insert_node(&root, 8); insert_node(&root, 9); // for (int i = 11; i < 32; i++) { // insert_node(&root, i); // } root->left->right = NULL; printf("Size of the binary tree is %zu\n", calculate_size_recursive(root)); printf("Height of the binary tree is %zu\n", calculate_height(root)); simple_print_binary_tree(root); pretty_print_binary_tree(root); free_binary_tree_recursive(root); return 0; }