-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinarySearchTree_2D_Node.cpp
96 lines (85 loc) · 2.63 KB
/
BinarySearchTree_2D_Node.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
//-----A BST with two values like order pair (x,y) and the name of shape placed at that location its node class will be
#include <iostream>
#include <string>
using namespace std;
class Node2dBST {
public:
int x, y;
string shapeName;
Node2dBST* lChild;
Node2dBST* rChild;
Node2dBST(int x, int y, string shapeName) {
this->x = x;
this->y = y;
this->shapeName = shapeName;
this->lChild = nullptr;
this->rChild = nullptr;
}
};
class BST {
public:
Node2dBST* root;
BST() {
root = nullptr;
}
bool Insert(int x, int y, string shapeName) {
if (root == nullptr) { // Tree is empty
Node2dBST* n = new Node2dBST(x, y, shapeName);
root = n;
return true;
}
else {
Node2dBST* curr = root;
Node2dBST* prev = nullptr;
int level = 0;
bool found = false;
while (curr != nullptr && !found) { // Iterative traversal for finding correct insertion point
if (curr->x == x && curr->y == y)
found = true;
else {
prev = curr;
if (level % 2 == 0) {
if (curr->x > x)
curr = curr->lChild;
else
curr = curr->rChild;
}
else {
if (curr->y > y)
curr = curr->lChild;
else
curr = curr->rChild;
}
}
level++;
}
if (!found) { // Node coordinates not in the tree, insert new node
Node2dBST* n = new Node2dBST(x, y, shapeName);
if ((level - 1) % 2 == 0) {
if (prev->x > x)
prev->lChild = n;
else
prev->rChild = n;
}
else {
if (prev->y > y)
prev->lChild = n;
else
prev->rChild = n;
}
return true;
}
}
return false;
}
};
int main() {
BST tree;
tree.Insert(5, 10, "Circle");
tree.Insert(15, 5, "Square");
tree.Insert(5, 15, "Triangle");
tree.Insert(10, 20, "Rectangle");
tree.Insert(20, 25, "Polygon");
// Additional testing and validation can be done here
return 0;
}