1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#include <stdio.h> #include <stdlib.h> #include "node.h" struct _node *add_val(struct _node *node, int val) { if (node == NULL) { node = (struct _node *)malloc(sizeof (struct _node)); node->left = node->right = NULL; node->val = val; return node; } if (val < node->val) node->left = add_val(node->left, val); else node->right = add_val(node->right, val); return node; } void show_node(struct _node *node) { if (node == NULL) return; show_node(node->left); printf("%5d", node->val); show_node(node->right); } void free_node(struct _node *node) { if (node == NULL) return; free_node(node->left); free_node(node->right); free(node); }