Download code

Jump to: navigation, search

Back to Binary_search_tree_(C)

Download for Windows: zip

Download for UNIX: zip, tar.gz, tar.bz2

bst.h

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Binary_search_tree_(C)?oldid=18734
14 */
15 
16 #ifndef _BST_H_
17 #define _BST_H_
18 
19 /* Returns negative (left<right), zero (left==right), or positive (left>right). */
20 typedef int comparator(void* left, void* right);
21 struct bst_node {
22     void* data;
23     struct bst_node* left;
24     struct bst_node* right;
25 };
26 
27 struct bst_node* new_node(void* data);
28 void free_node(struct bst_node* node);
29 struct bst_node** search(struct bst_node** root, comparator compare, void* data);
30 void insert(struct bst_node** root, comparator compare, void* data);
31 void delete(struct bst_node** node);
32 
33 #endif
34 


hijacker
hijacker
hijacker
hijacker

bst.c

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Binary_search_tree_(C)?oldid=18734
14 */
15 
16 #include <stdlib.h>
17 #include "bst.h"
18 
19 struct bst_node** search(struct bst_node** root, comparator compare, void* data) {
20     struct bst_node** node = root;
21     while (*node != NULL) {
22         int compare_result = compare(data, (*node)->data);
23         if (compare_result < 0)
24             node = &(*node)->left;
25         else if (compare_result > 0)
26             node = &(*node)->right;
27         else
28             break;
29     }
30     return node;
31 }
32 void insert(struct bst_node** root, comparator compare, void* data) {
33     struct bst_node** node = search(root, compare, data);
34     if (*node == NULL) {
35         *node = new_node(data);
36     }
37 }
38 void delete(struct bst_node** node) {
39     struct bst_node* old_node = *node;
40     if ((*node)->left == NULL) {
41         *node = (*node)->right;
42         free_node(old_node);
43     } else if ((*node)->right == NULL) {
44         *node = (*node)->left;
45         free_node(old_node);
46     } else {
47         struct bst_node** pred = &(*node)->left;
48 	while ((*pred)->right != NULL) {
49 	    pred = &(*pred)->right;
50 	}
51 
52 	/* Swap values */
53 	void* temp = (*pred)->data;
54 	(*pred)->data = (*node)->data;
55 	(*node)->data = temp;
56 
57 	delete(pred);
58     }
59 }


hijacker
hijacker
hijacker
hijacker