-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRBST.hpp
87 lines (67 loc) · 1.71 KB
/
RBST.hpp
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
#ifndef RBST_HPP_
#define RBST_HPP_
#include <iostream>
#include <assert.h>
#include <stdlib.h>
#include <vector>
#include <math.h>
#include "Key.hpp"
#include "DataStructure.hpp"
using namespace std;
class RBSTNode;
class RBSTNode: public Key {
public:
RBSTNode(const Key& key):Key(key),m_left(NULL),m_right(NULL) { }
virtual ~RBSTNode() {}
string getKey() {
return *this;
}
string setKey(const Key& key) {
*this = key;
return Key(key);
}
RBSTNode* left() {
return m_left;
}
RBSTNode* right() {
return m_right;
}
RBSTNode* setLeft (RBSTNode* left) {
m_left = left;
return this;
}
RBSTNode* setRight (RBSTNode* right) {
m_right =right;
return this;
}
private:
RBSTNode() {}
RBSTNode* m_left;
RBSTNode* m_right;
};
class RBST : public DataStructure {
public:
RBST():m_head(NULL),m_size(0){};
virtual ~RBST() {};
//ADD FUNCTIONS
int add(const Key& key, bool verbose=false);
//FIND FUNCTIONS
int find(const Key& key, bool verbose = false);
//DEL FUNCTIONS
int del(const Key& key, bool verbose=false);
//DUMP FUNCTIONS
int dump(char sep = ' ');
int dump(RBSTNode* target, char sep);
private:
RBSTNode* randomAdd(RBSTNode* target, const Key& key);
RBSTNode* addRoot(RBSTNode* target, const Key& key);
RBSTNode* rightRotate(RBSTNode* target);
RBSTNode* leftRotate(RBSTNode* target);
RBSTNode* del(RBSTNode* target, const Key& key);
RBSTNode* delHelper(RBSTNode* target);
RBSTNode* find(RBSTNode* target, const Key& key);
RBSTNode* m_head;
unsigned int m_size;
RBSTNode* del_store;
};
#endif /*RBST_HPP_*/