1: /* Copyright (c) 2012 the authors listed at the following URL, and/or
2: the authors of referenced articles or incorporated external code:
3: http://en.literateprograms.org/Binary_search_tree_(C)?action=history&offset=20090103192554
4:
5: Permission is hereby granted, free of charge, to any person obtaining
6: a copy of this software and associated documentation files (the
7: "Software"), to deal in the Software without restriction, including
8: without limitation the rights to use, copy, modify, merge, publish,
9: distribute, sublicense, and/or sell copies of the Software, and to
10: permit persons to whom the Software is furnished to do so, subject to
11: the following conditions:
12:
13: The above copyright notice and this permission notice shall be
14: included in all copies or substantial portions of the Software.
15:
16: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19: IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20: CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21: TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22: SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23:
24: Retrieved from: http://en.literateprograms.org/Binary_search_tree_(C)?oldid=15737
25: */
26:
27: #ifndef _BST_H_
28:
29: /* Returns negative (left<right), zero (left==right), or positive (left>right). */
30: typedef int comparator(void* left, void* right);
31: struct bst_node {
32: void* data;
33: struct bst_node* left;
34: struct bst_node* right;
35: };
36:
37: struct bst_node* new_node(void* data);
38: void free_node(struct bst_node* node);
39: struct bst_node** search(struct bst_node** root, comparator compare, void* data);
40: void insert(struct bst_node** root, comparator compare, void* data);
41: void delete(struct bst_node** node);
42:
43: #endif
44:
bst.c
1: /* Copyright (c) 2012 the authors listed at the following URL, and/or
2: the authors of referenced articles or incorporated external code:
3: http://en.literateprograms.org/Binary_search_tree_(C)?action=history&offset=20090103192554
4:
5: Permission is hereby granted, free of charge, to any person obtaining
6: a copy of this software and associated documentation files (the
7: "Software"), to deal in the Software without restriction, including
8: without limitation the rights to use, copy, modify, merge, publish,
9: distribute, sublicense, and/or sell copies of the Software, and to
10: permit persons to whom the Software is furnished to do so, subject to
11: the following conditions:
12:
13: The above copyright notice and this permission notice shall be
14: included in all copies or substantial portions of the Software.
15:
16: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19: IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20: CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21: TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22: SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23:
25: */
24: Retrieved from: http://en.literateprograms.org/Binary_search_tree_(C)?oldid=15737
26:
27: #include <stdlib.h>
28: #include "bst.h"
29:
30: struct bst_node** search(struct bst_node** root, comparator compare, void* data) {
31: struct bst_node** node = root;
32: while (*node != NULL) {
33: int compare_result = compare(data, (*node)->data);
34: if (compare_result < 0)
35: node = &(*node)->left;
36: else if (compare_result > 0)
37: node = &(*node)->right;
38: else
39: break;
40: }
41: return node;
42: }
43: void insert(struct bst_node** root, comparator compare, void* data) {
44: struct bst_node** node = search(root, compare, data);
45: if (*node == NULL) {
46: *node = new_node(data);
47: }
48: }
49: void delete(struct bst_node** node) {
50: struct bst_node* old_node = *node;
51: if ((*node)->left == NULL) {
52: *node = (*node)->right;
53: free_node(old_node);
54: } else if ((*node)->right == NULL) {
55: *node = (*node)->left;
56: free_node(old_node);
57: } else {
58: struct bst_node** pred = &(*node)->left;
59: while ((*pred)->right != NULL) {
60: pred = &(*pred)->right;
61: }
62:
63: /* Swap values */
64: void* temp = (*pred)->data;
65: (*pred)->data = (*node)->data;
66: (*node)->data = temp;
67:
68: delete(pred);
69: }
70: }