-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBSTclass.h
111 lines (94 loc) · 2.2 KB
/
BSTclass.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
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: BSTclass.h
* Author: devesh
*
* Created on 13 April, 2016, 7:37 PM
*/
#ifndef BSTCLASS_H
#define BSTCLASS_H
#include <atomic>
#include <climits>
#include <vector>
const int inf1 = INT_MAX-1;
const int inf2 = INT_MAX;
using namespace std;
class BST
{
public:
struct infoRecord;
struct updateRecord {
bool isDirty;
infoRecord *info;
};
struct searchResult;
struct treeNode
{
int data;
bool isLeaf;
atomic<treeNode*> left;
atomic<treeNode*> right;
atomic<updateRecord> update;
treeNode(int d, bool b, treeNode *l = nullptr, treeNode *r = nullptr,
updateRecord *u = NULL)
: data(d), isLeaf(b)
{
left.store(l);
right.store(r);
if (u)
update.store(*u);
else {
updateRecord emptyUR = {false, nullptr};
update.store(emptyUR);
}
}
};
atomic<treeNode*> root;
BST() {
treeNode * rleft = new treeNode(inf1, true);
treeNode * rright = new treeNode(inf2, true);
root.store(new treeNode(inf2, false, rleft, rright));
};
void print_preorder();
void preorder(treeNode *);
struct searchResult * search(int);
struct infoRecord {
atomic<treeNode*> parent;
atomic<treeNode*> leaf;
atomic<treeNode*> subtree;
infoRecord(treeNode *p = nullptr, treeNode *l = nullptr, treeNode *s = nullptr)
{
parent.store(p);
leaf.store(l);
subtree.store(s);
}
};
};
typedef struct BST::searchResult {
atomic<treeNode *> p, l;
updateRecord pupdate;
searchResult(treeNode *pp, treeNode *ll, updateRecord uu)
: pupdate(uu)
{
p.store(pp);
l.store(ll);
}
} searchResult;
class SequentialBST: public BST
{
public:
void insert(int);
};
class NonBlockingBST: public BST
{
private:
void helpInsert(infoRecord *);
bool CASChild(treeNode *parent, treeNode *oldNode, treeNode *newNode);
public:
bool insert(int);
};
#endif /* BSTCLASS_H */