-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbst.cc
180 lines (156 loc) · 3.54 KB
/
bst.cc
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <thread>
#include <atomic>
#include <iostream>
#include <vector>
struct Node
{
int iValue;
std::atomic<bool> iDeleted;
std::atomic<Node*> iLeft, iRight;
Node() : iValue(0), iDeleted(false), iLeft(nullptr), iRight(nullptr) {}
};
std::atomic<Node*> gAnchor;
bool Contains(std::atomic<Node*>& aRoot, int aSearch)
{
Node* curNode = aRoot.load();
if (curNode == nullptr)
{
return false;
}
else if (curNode->iValue == aSearch)
{
return !curNode->iDeleted;
}
else if (curNode->iValue < aSearch)
{
return Contains(curNode->iRight, aSearch);
}
else
{
return Contains(curNode->iLeft, aSearch);
}
}
void Add(std::atomic<Node*>& aRoot, int aValue)
{
Node* newNode = new Node();
newNode->iValue = aValue;
Node* curNode = aRoot.load();
if (curNode == nullptr)
{
// either root is still nullptr, then exchange with newnode
// or other thread has already added node. Start over.
bool updated = aRoot.compare_exchange_strong(curNode, newNode);
if (updated)
{
return;
}
}
// Either other thread modified root or root was not null; either way, tree is no longer empty.
// curNode contains actual non-null value of aRoot
for(;;)
{
// descend into tree
int curValue = curNode->iValue;
bool curDeleted = curNode->iDeleted.load();
if (curValue == aValue)
{
if (curDeleted)
{
curNode->iDeleted.compare_exchange_strong(curDeleted, false);
}
// no duplicates
delete newNode;
return;
}
if (aValue < curValue)
{
// Try to insert new node as left child of current node
Node* nullNode = nullptr;
bool updated = curNode->iLeft.compare_exchange_strong(nullNode, newNode);
if (updated)
{
return;
}
curNode = curNode->iLeft.load();
}
else if (aValue > curValue)
{
// Try to insert new node as right child of current node
Node* nullNode = nullptr;
bool updated = curNode->iRight.compare_exchange_strong(nullNode, newNode);
if (updated)
{
return;
}
curNode = curNode->iRight.load();
}
}
}
void Delete(std::atomic<Node*>& aRoot, int aValue)
{
Node* curNode = aRoot.load();
for (;;)
{
if (curNode == nullptr)
{
return;
}
int curValue = curNode->iValue;
if (curValue == aValue)
{
curNode->iDeleted.store(true);
return;
}
if (aValue < curValue)
{
curNode = curNode->iLeft.load();
}
else if (aValue > curValue)
{
curNode = curNode->iRight.load();
}
}
}
void Print(std::atomic<Node*>& aRoot)
{
Node* curNode = aRoot.load();
if (curNode == nullptr)
{
return;
}
Print(curNode->iLeft);
if (!curNode->iDeleted.load())
{
std::cout << curNode->iValue << " ";
}
Print(curNode->iRight);
}
int main(int argc, char** argv)
{
gAnchor.store(nullptr);
Add(gAnchor, 4);
std::vector<std::thread> threads;
for (int i = 0; i < 100; i++)
{
threads.push_back(std::thread(Add, std::ref(gAnchor), i));
}
for (int i = 0; i < 100; i++)
{
threads[i].join();
}
std::cout << Contains(gAnchor, 5) << std::endl;
std::cout << Contains(gAnchor, 3) << std::endl;
threads.clear();
for (int i = 0; i < 100; i++)
{
threads.push_back(std::thread(Delete, std::ref(gAnchor), i));
}
for (int i = 0; i < 100; i++)
{
threads[i].join();
}
std::cout << Contains(gAnchor, 5) << std::endl;
std::cout << Contains(gAnchor, 3) << std::endl;
Print(gAnchor);
std::cout << std::endl;
}