-
Notifications
You must be signed in to change notification settings - Fork 0
/
test1.cpp
209 lines (179 loc) · 4.35 KB
/
test1.cpp
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <sstream>
using namespace std;
int test(int* array, int start, int end, int target){
//if(target > array[end] || target < array[start]) return 0;
int mid = (start + end)/2;
if(start == end || start == end - 1){
if(abs(target - array[end]) > abs(target - array[start]))return start;
else return end;
}
cout << start << " " << end << endl;
if(target < array[end] && target > array[mid])return test(array, mid, end, target);
else return test(array, start, mid, target);
//return test(array, start, (end + start)/2, target) + test(array, (end + start)/2, end, target);
}
class DBST{
private:
int x;
int y;
DBST* leftTop;
DBST* rightTop;
DBST* leftBot;
DBST* rightBot;
public:
DBST(){
x = 0;
y = 0;
leftTop = NULL;
rightTop = NULL;
leftBot = NULL;
rightBot = NULL;
}
DBST(int x_, int y_){
x = x_;
y = y_;
leftTop = NULL;
rightTop = NULL;
leftBot = NULL;
rightBot = NULL;
}
~DBST(){
if(leftTop != NULL)delete leftTop;
if(rightTop != NULL)delete rightTop;
if(leftBot != NULL)delete leftBot;
if(rightBot != NULL)delete rightBot;
}
int getX(){return x;}
int getY(){return y;}
DBST* getLeftTop(){return leftTop;}
DBST* getRightTop(){return rightTop;}
DBST* getLeftBot(){return leftBot;}
DBST* getRightBot(){return rightBot;}
void setX(int x_){x = x_;}
void setY(int y_){y = y_;}
void setLeftTop(DBST* leftTop_){leftTop = leftTop_;}
void setRightTop(DBST* rightTop_){rightTop = rightTop_;}
void setLeftBot(DBST* leftBot_){leftBot = leftBot_;}
void setRightBot(DBST* rightBot_){rightBot = rightBot_;}
};
void printNode(DBST* node){
cout << "x is " << node->getX() << endl;
cout << "y is " << node->getY() << endl;
cout << "-----------------------" << endl;
}
void printDBST(DBST* root){
if(root->getLeftTop() != NULL) printDBST(root->getLeftTop());
if(root->getRightTop() != NULL) printDBST(root->getRightTop());
printNode(root);
if(root->getLeftBot() != NULL) printDBST(root->getLeftBot());
if(root->getRightBot() != NULL) printDBST(root->getRightBot());
}
void insert(DBST* root, DBST* newNode){
while(1){
//left half
if(newNode->getX() < root->getX()){
//left bot
if(newNode->getY() < root->getY()){
//no children
if(root->getLeftBot() == NULL){
root->setLeftBot(newNode);
cout << 444 << endl;
break;
}
else root = root->getLeftBot();
}
//left top
else{
//no children
if(root->getLeftTop() == NULL){
root->setLeftTop(newNode);
cout << 333 << endl;
break;
}
else root = root->getLeftTop();
}
}
//right half
else{
//right bot
if(newNode->getY() < root->getY()){
//no children
if(root->getRightBot() == NULL){
root->setRightBot(newNode);
cout << 222 << endl;
break;
}
else root = root->getRightBot();
}
//right top
else{
//no children
if(root->getRightTop() == NULL){
root->setRightTop(newNode);
cout << 111 << endl;
break;
}
else root = root->getRightTop();
}
}
}
}
DBST* findClosetPoint(DBST* root, DBST* target){
while(1){
//letf half
if(target->getX() < root->getX()){
//left bot
if(target->getY() < root->getY()){
if(root->getLeftBot() == NULL) return root;
else root = root->getLeftBot();
}
//left top
else{
if(root->getLeftTop() == NULL) return root;
else root = root->getLeftTop();
}
}
else if (target->getX() < root->getX() && target->getY() < root->getY()) return root;
//right half
else{
//right bot
if(target->getY() < root->getY()){
if(root->getRightBot() == NULL) return root;
else root = root->getRightBot();
}
//right top
else{
if(root->getRightTop() == NULL) return root;
else root = root->getRightTop();
}
}
}
}
int main(int argc, char const *argv[]){
DBST* root = new DBST(1,2);
DBST* newNode = new DBST(2,2);
insert(root, newNode);
newNode = new DBST(0,0);
insert(root, newNode);
newNode = new DBST(12,11);
insert(root, newNode);
newNode = new DBST(7,10);
insert(root, newNode);
newNode = new DBST(1, 100);
insert(root, newNode);
newNode = new DBST(99, 7);
insert(root, newNode);
printDBST(root);
newNode = new DBST(50, 50);
DBST* test1 = findClosetPoint(root, newNode);
cout << "find !" << endl;
printDBST(test1);
delete newNode;
delete root;
return 0;
}