-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbt.h
170 lines (130 loc) · 3.52 KB
/
bt.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#ifndef NAT_BT_BT_H_
#define NAT_BT_BT_H_
#include "../record.h"
/**
* @brief Node of a binary tree
*
*/
typedef struct b_node {
// unique id for node
int id;
// node data
NAT_Record *record;
int child_count;
int depth_left, depth_right;
struct b_node *left, *right;
} B_Node;
/**
* @brief Binary tree
*
*/
typedef struct b_tree {
// unique id for tree
int id;
// root node of tree
B_Node *root_node;
// function to compare two records
int (*recordsCompare) (NAT_Record *r1, NAT_Record *r2);
} B_Tree;
/**
* @brief Create new tree with comparison function f
*
* @param f The "recordsCompare" field of created tree
* @return B_Tree* Pointer to created tree
*/
B_Tree* BT_createTree(int (*f) (NAT_Record *r1, NAT_Record *r2));
/**
* @brief Create new node with record r
*
* @param r The "record" field of created record
* @return B_Node* Pointer to created node
*/
B_Node* BT_createNode(NAT_Record *r);
/**
* @brief Free tree t, along with all associated child nodes, records
*
* @param t Pointer to tree
*/
void BT_freeTree(B_Tree *t);
/**
* @brief Free (sub)tree with root u, along with all associated child nodes, records
*
* @param u Pointer to (sub)tree root node
*/
void BT_freeNodesR(B_Node *u);
/**
* @brief Free node u, along with associated record
*
* @param u Pointer to node
*/
void BT_freeNode(B_Node *u);
/**
* @brief Get in-order minimum leaf node in (sub)tree with root u
*
* @param u Pointer to (sub)tree root node
* @return B_Node* Pointer to min leaf node
*/
B_Node* BT_getInOrderMin(B_Node *u);
/**
* @brief Get in-order maximum leaf node in (sub)tree with root u
*
* @param u Pointer to (sub)tree root
* @return B_Node* Pointer to max leaf node
*/
B_Node* BT_getInOrderMax(B_Node *u);
/**
* @brief Calculate depth of (sub)tree with root u, from scratch
*
* @param u Pointer to (sub)tree root
* @return int Depth of (sub)tree
*/
int BT_calcDepth(B_Node *u);
/**
* @brief Search for first node with record r in tree t
*
* @param t Pointer to tree
* @param r Pointer to record
* @return B_Node* Pointer to node if found else NULL
*/
B_Node* BT_search(B_Tree *t, NAT_Record *r);
/**
* @brief Search for first node with record r in (sub)tree with root u
*
* @param u Pointer to (sub)tree root
* @param r Pointer to record
* @param f Pointer to recordsCompare function
* @return B_Node* Pointer to node if found else NULL
*/
B_Node* BT_searchR(B_Node *u, NAT_Record *r, int (*f) (NAT_Record *r1, NAT_Record *r2));
/**
* @brief Insert node with record r as random leaf in tree t. Duplicates allowed.
*
* @param t Pointer to tree
* @param r Pointer to record
*/
void BT_insert(B_Tree *t, NAT_Record *r);
/**
* @brief (Create and) Insert node with record r as a random leaf node in (sub)tree with root u. Duplicates allowed.
*
* @param u Pointer to (sub)tree root
* @param r Pointer to record
* @return B_Node* Pointer to new (sub)tree root
*/
B_Node* BT_insertR(B_Node *u, NAT_Record *r);
/**
* @brief Delete (and free) all nodes with record r in tree t
*
* @param t Pointer to tree
* @param r Pointer to record
*/
void BT_delete(B_Tree *t, NAT_Record *r);
/**
* @brief Delete (and free) all nodes with record r in (sub)tree with root u
*
* @param u Pointer to (sub)tree root
* @param r Pointer to record
* @param f Pointer to recordsCompare function
* @return B_Node* Pointer to new (sub)tree root
*/
B_Node* BT_deleteR(B_Node *u, NAT_Record *r, int (*f) (NAT_Record *r1, NAT_Record *r2));
#endif