-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBSTree.h
59 lines (46 loc) · 1.49 KB
/
BSTree.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
#include "Node.h"
#ifndef _BSTREE_
#define _BSTREE_
class BSTree {
public:
BSTree() : treeSize(0), root(nullptr) {}
virtual ~BSTree() {}
virtual Node* insert(const std::string& key, int val);
virtual Node* remove(const std::string& key);
Node* getRoot() { return root; }
Node* findMinNode(Node* root);
Node* findNode(const std::string &key);
vector<Node *> inOrderTraverse();
vector<Node *> getLeaves();
int size() { return treeSize; };
protected:
Node* root;
int treeSize;
pair<Node*, Node*> insertNode(Node* recurseRoot, const std::string& key, int val);
Node* removeNode(Node* recurseRoot, const std::string& key);
};
class RBTree : public BSTree {
public:
RBTree() { root = nullptr; };
RBTree(Node &node);
Node *insert(const std::string& key, int val) override;
Node *remove(const std::string& key) override;
private:
void updateColors(Node* node);
bool tryRecolor(Node* node);
Node *rotateLeft(Node* node);
Node *rotateRight(Node* node);
void checkPointers();
bool checkColorInvariant(); // check invariant that parent of RED node should be BLACK
bool checkDescendantBlackHeights(); // check invariant that all leaves have same BLACK height
};
// class NodeNotFoundException : std::exception {
// public:
// NodeNotFoundException(string key) : key(key) { }
// const char * what () const noexcept override {
// return "node with ";
// }
// private:
// string key;
// };
#endif