-
Notifications
You must be signed in to change notification settings - Fork 2
/
btree.h
88 lines (79 loc) · 3.65 KB
/
btree.h
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
* The MIT License (MIT)
* Copyright (c) 2015 Peter Vanusanik <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the
* following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies
* or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
* OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* btree.h
* Created on: Jan 3, 2016
* Author: Peter Vanusanik
* Contents:
*/
#pragma once
#include "_commons.h"
#include "funcops.h"
#ifdef __cplusplus
extern "C" {
#endif
/* CONVENTIONS: All data structures for red-black trees have the prefix */
/* "rb_" to prevent name conflicts. */
/* */
/* Function names: Each word in a function name begins with */
/* a capital letter. An example funcntion name is */
/* CreateRedTree(a,b,c). Furthermore, each function name */
/* should begin with a capital letter to easily distinguish */
/* them from variables. */
/* */
/* Variable names: Each word in a variable name begins with */
/* a capital letter EXCEPT the first letter of the variable */
/* name. For example, int newLongInt. Global variables have */
/* names beginning with "g". An example of a global */
/* variable name is gNewtonsConstant. */
typedef struct rb_node {
void* key;
void* info;
int red; /* if red=0 then the node is black */
struct rb_node* left;
struct rb_node* right;
struct rb_node* parent;
} rb_node_t;
/* Compare(a,b) should return 1 if *a > *b, -1 if *a < *b, and 0 otherwise */
/* Destroy(a) takes a pointer to whatever key might be and frees it accordingly */
typedef struct rb_tree {
cmp_func_t cmp_func;
key_deallocator_func_t key_deallocator_func;
value_deallocator_func_t value_deallocator_func;
/* A sentinel is used for root and for nil. These sentinels are */
/* created when RBTreeCreate is caled. root->left should always */
/* point to the node which is the root of the tree. nil points to a */
/* node which should always be black but has aribtrary children and */
/* parent and no key or info. The point of using these sentinels is so */
/* that the root and nil nodes do not require special cases in the code */
rb_node_t* root;
rb_node_t* nil;
} rb_tree_t;
rb_tree_t* rb_create_tree(cmp_func_t, key_deallocator_func_t, value_deallocator_func_t);
rb_node_t * rb_insert(rb_tree_t*, void* key, void* info);
rb_node_t* tree_find(rb_tree_t*, void*);
void rb_delete(rb_tree_t*, rb_node_t* );
void rb_destroy(rb_tree_t*);
rb_node_t* tree_predecesor(rb_tree_t*,rb_node_t*);
rb_node_t* tree_successor(rb_tree_t*,rb_node_t*);
#ifdef __cplusplus
}
#endif