-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRBTree.h
98 lines (81 loc) · 2.47 KB
/
RBTree.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
//
// Created by evyat on 10/10/2019.
//
#ifndef RBTREE_RBTREE_H
#define RBTREE_RBTREE_H
// a color of a Node.
typedef enum Color
{
RED, BLACK
} Color;
/**
* a function to sort the tree items.
* @a, @b: two items.
* @return: equal to 0 iff a == b. lower than 0 if a < b. Greater than 0 iff b < a.
*/
typedef int (*CompareFunc)(const void *a, const void *b);
/**
* a function to apply on all tree items.
* @object: a pointer to an item of the tree.
* @args: pointer to other arguments for the function.
* @return: 0 on failure, other on success.
*/
typedef int (*forEachFunc)(const void *object, void *args);
/**
* a function to free a data item
* @object: a pointer to an item of the tree.
*/
typedef void (*FreeFunc)(void *data);
/*
* a node of the tree.
*/
typedef struct Node
{
struct Node *parent, *left, *right;
Color color;
void *data;
} Node;
/**
* represents the tree
*/
typedef struct RBTree
{
Node *root;
CompareFunc compFunc;
FreeFunc freeFunc;
int size;
} RBTree;
/**
* constructs a new RBTree with the given CompareFunc.
* comp: a function two compare two variables.
*/
RBTree *newRBTree(CompareFunc compFunc, FreeFunc freeFunc); // implement it in RBTree.c
/**
* add an item to the tree
* @param tree: the tree to add an item to.
* @param data: item to add to the tree.
* @return: 0 on failure, other on success. (if the item is already in the tree - failure).
*/
int addToRBTree(RBTree *tree, void *data); // implement it in RBTree.c
/**
* check whether the tree contains this item.
* @param tree: the tree to add an item to.
* @param data: item to check.
* @return: 0 if the item is not in the tree, other if it is.
*/
int containsRBTree(RBTree *tree, void *data); // implement it in RBTree.c
/**
* Activate a function on each item of the tree. the order is an ascending order. if one of the activations of the
* function returns 0, the process stops.
* @param tree: the tree with all the items.
* @param func: the function to activate on all items.
* @param args: more optional arguments to the function (may be null if the given function support it).
* @return: 0 on failure, other on success.
*/
int forEachRBTree(RBTree *tree, forEachFunc func, void *args); // implement it in RBTree.c
/**
* free all memory of the data structure.
* @param tree: the tree to free.
*/
void freeRBTree(RBTree *tree); // implement it in RBTree.c
#endif //RBTREE_RBTREE_H