-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfatnode.cpp
More file actions
88 lines (75 loc) · 2.17 KB
/
Copy pathfatnode.cpp
File metadata and controls
88 lines (75 loc) · 2.17 KB
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
#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
class Node {
public:
int value;
std::vector<std::pair<int, Node*>> left, right;
Node(int timestamp, int value, Node *left, Node *right) {
this->value = value;
(this->left).push_back(std::make_pair(timestamp, nullptr));
(this->right).push_back(std::make_pair(timestamp, nullptr));
}
Node *get_last_left() {
if ((this->left).size() == 0) {
return nullptr;
}
return ((this->left)[(this->left).size() - 1]).second;
}
Node *get_last_right() {
if ((this->right).size() == 0) {
return nullptr;
}
return ((this->right)[(this->right).size() - 1]).second;
}
};
class Tree {
public:
Node *root;
int current_version;
Tree() {
this->root = nullptr;
this->current_version = 0;
}
void insert(int value) {
this->current_version++;
this->root = insert(this->root, this->current_version, value);
}
bool find(int timestamp, int value) {
return find(this->root, timestamp, value);
}
private:
Node *insert(Node *node, int timestamp, int value) {
if (node == nullptr) {
return new Node(timestamp, value, nullptr, nullptr);
} else {
if (value < node->value) {
node->left.push_back(std::make_pair(timestamp, insert(node->get_last_left(), timestamp, value)));
} else { // value > node->value
node->right.push_back(std::make_pair(timestamp, insert(node->get_last_right(), timestamp, value)));
}
return node;
}
}
bool find(Node *node, int timestamp, int value) {
if (node == nullptr) {
return false;
} else {
if (node->value == value) {
return true;
} else {
if (value < node->value) {
auto it = std::upper_bound((node->left).begin(), (node->left).end(), std::make_pair(timestamp, nullptr));
return find((*std::prev(it)).second, timestamp, value);
} else { // value > node->value
auto it = std::upper_bound((node->right).begin(), (node->right).end(), std::make_pair(timestamp, nullptr));
return find((*std::prev(it)).second, timestamp, value);
}
}
}
}
};
int main() {
return 0;
}