|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <stdbool.h> |
| 4 | +#include <string.h> |
| 5 | +#include <time.h> |
| 6 | + |
| 7 | +enum child_dir { |
| 8 | + left_child, |
| 9 | + right_child, |
| 10 | + root, |
| 11 | +}; |
| 12 | + |
| 13 | +struct node { |
| 14 | + unsigned long data; |
| 15 | + struct node *left; |
| 16 | + struct node *right; |
| 17 | +}; |
| 18 | + |
| 19 | +struct root { |
| 20 | + struct node *r; |
| 21 | +}; |
| 22 | + |
| 23 | +void dump(struct node *node, int level, enum child_dir dir) |
| 24 | +{ |
| 25 | + if (!node) |
| 26 | + return; |
| 27 | + |
| 28 | + dump(node->right, level + 1, right_child); |
| 29 | + |
| 30 | + if (dir == left_child) |
| 31 | + printf("%*s\n", level*3, "|"); |
| 32 | + |
| 33 | + printf("%*s - %05lu\n", level*3, " ", node->data); |
| 34 | + |
| 35 | + if (dir == right_child) |
| 36 | + printf("%*s\n", level*3, "|"); |
| 37 | + |
| 38 | + dump(node->left, level + 1, left_child); |
| 39 | +} |
| 40 | + |
| 41 | +struct node* find(struct root *root, unsigned long data) |
| 42 | +{ |
| 43 | + struct node* n = root->r; |
| 44 | + |
| 45 | + while (n) { |
| 46 | + if (n->data == data) |
| 47 | + return n; |
| 48 | + if (data < n->data) |
| 49 | + n = n->left; |
| 50 | + else |
| 51 | + n = n->right; |
| 52 | + } |
| 53 | + |
| 54 | + return NULL; |
| 55 | +} |
| 56 | + |
| 57 | +struct node* new_node(unsigned long data) |
| 58 | +{ |
| 59 | + struct node *n; |
| 60 | + |
| 61 | + n = malloc(sizeof(struct node)); |
| 62 | + |
| 63 | + n->data = data; |
| 64 | + n->left = n->right = NULL; |
| 65 | + return n; |
| 66 | +} |
| 67 | + |
| 68 | +void insert(struct root *root, struct node *new) |
| 69 | +{ |
| 70 | + struct node *parent; |
| 71 | + |
| 72 | + if (!root->r) { |
| 73 | + root->r = new; |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + parent = root->r; |
| 78 | + |
| 79 | + while (true) { |
| 80 | + /* Don't support duplicate data */ |
| 81 | + if (new->data == parent->data) |
| 82 | + break; |
| 83 | + |
| 84 | + if (new->data < parent->data) { |
| 85 | + if (!parent->left) { |
| 86 | + parent->left = new; |
| 87 | + break; |
| 88 | + } |
| 89 | + parent = parent->left; |
| 90 | + } else { |
| 91 | + if (!parent->right) { |
| 92 | + parent->right = new; |
| 93 | + break; |
| 94 | + } |
| 95 | + parent = parent->right; |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +struct node* delete(struct root *root, unsigned long data) |
| 101 | +{ |
| 102 | + struct node *n = root->r, **p = &root->r; |
| 103 | + struct node *child; |
| 104 | + |
| 105 | + while (n && n->data != data) { |
| 106 | + if (data < n->data) { |
| 107 | + p = &n->left; |
| 108 | + n = n->left; |
| 109 | + } else { |
| 110 | + p = &n->right; |
| 111 | + n = n->right; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + if (!n) |
| 116 | + return NULL; |
| 117 | + |
| 118 | + if (n->left && n->right) { |
| 119 | + struct node *rn = n->right, **rp = &n->right; |
| 120 | + |
| 121 | + while (rn->left) { |
| 122 | + rp = &rn->left; |
| 123 | + rn = rn->left; |
| 124 | + } |
| 125 | + |
| 126 | + n->data = rn->data; |
| 127 | + n = rn; |
| 128 | + p = rp; |
| 129 | + } |
| 130 | + |
| 131 | + child = n->left ? n->left : n->right; |
| 132 | + *p = child; |
| 133 | + |
| 134 | + return NULL; |
| 135 | +} |
| 136 | + |
| 137 | +void insert_test() |
| 138 | +{ |
| 139 | + struct root tree; |
| 140 | + struct node* n; |
| 141 | + |
| 142 | + tree.r = NULL; |
| 143 | + |
| 144 | + insert(&tree, new_node(9)); |
| 145 | + |
| 146 | + insert(&tree, new_node(5)); |
| 147 | + insert(&tree, new_node(2)); |
| 148 | + insert(&tree, new_node(8)); |
| 149 | + |
| 150 | + insert(&tree, new_node(18)); |
| 151 | + insert(&tree, new_node(13)); |
| 152 | + insert(&tree, new_node(21)); |
| 153 | + insert(&tree, new_node(20)); |
| 154 | + |
| 155 | + dump(tree.r, 0, root); |
| 156 | + |
| 157 | + n = find(&tree, 18); |
| 158 | + if (n && n->data == 18) |
| 159 | + printf("Get 18\n"); |
| 160 | + |
| 161 | +} |
| 162 | + |
| 163 | +void delete_test() |
| 164 | +{ |
| 165 | + struct root tree; |
| 166 | + struct node* n; |
| 167 | + |
| 168 | + tree.r = NULL; |
| 169 | + |
| 170 | + insert(&tree, new_node(9)); |
| 171 | + |
| 172 | + insert(&tree, new_node(5)); |
| 173 | + insert(&tree, new_node(2)); |
| 174 | + insert(&tree, new_node(8)); |
| 175 | + |
| 176 | + insert(&tree, new_node(18)); |
| 177 | + insert(&tree, new_node(13)); |
| 178 | + insert(&tree, new_node(21)); |
| 179 | + insert(&tree, new_node(20)); |
| 180 | + |
| 181 | + dump(tree.r, 0, root); |
| 182 | + |
| 183 | + delete(&tree, 20); |
| 184 | + printf("Delete 20\n"); |
| 185 | + dump(tree.r, 0, root); |
| 186 | + |
| 187 | + delete(&tree, 9); |
| 188 | + printf("Delete 9\n"); |
| 189 | + dump(tree.r, 0, root); |
| 190 | +} |
| 191 | + |
| 192 | +int main() |
| 193 | +{ |
| 194 | + //insert_test(); |
| 195 | + delete_test(); |
| 196 | + return 0; |
| 197 | +} |
0 commit comments