-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrbt.h
50 lines (36 loc) · 1.32 KB
/
rbt.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
//Defining the two colors of RBT
enum color {RED, BLACK};
struct rbtnode{
int bnum;
bool color;
rbtnode *left, *right, *parent;
heapnode* twin; //Pointer connecting corresponding elements of RBT and Min heap
rbtnode(int bnum){
this->bnum = bnum;
parent=left=right=NULL;
}
};
// class of redblack tree
class rbt{
private:
rbtnode *root;
protected:
void rotateleft(rbtnode *&, rbtnode *&); // rotate left operation of rbt
void rotateright(rbtnode *&, rbtnode *&); // rotate right operation of rbt
void fixtree(rbtnode *&, rbtnode *&); // fix the tree to regain rbt properties
public:
rbt(); // constructor
rbtnode* insert(const int &n); // insert new node into the tree
rbtnode* findnode(int bnum); // find a node in the tree
void nextnode(int bnum); // find the next lowest node wrt inorder traversal
void prevnode(int bnum); // find the previous largest node wrt inorder traversal
void inorder(int, int); // find a range of building numbers in range low,high
void deletenode(rbtnode*); // delete a node from tree
void fixviolation(rbtnode*); // fix violation caused due to delete
void rightrotate(rbtnode *p); // rotate right after delete
void leftrotate(rbtnode *p); // rotate left after delete
};
//constructor
rbt::rbt(){
root = NULL;
}