-
Notifications
You must be signed in to change notification settings - Fork 0
/
PointBinaryTreeSort.cpp
107 lines (90 loc) · 1.92 KB
/
PointBinaryTreeSort.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
#include "PointBinaryTreeSort.h"
PointBinaryTreeSort::PointBinaryTreeSort()
{
}
PointBinaryTreeSort::~PointBinaryTreeSort()
{
}
std::list<Point>* PointBinaryTreeSort::SortPointsXAxis(std::vector<Point>* points)
{
Tree* root;
root = nullptr;
for (size_t i = 0; i < points->size(); i++)
{
insert(&root, points->begin()+i);
}
addToList(root);
return &sortedPoints;
}
void PointBinaryTreeSort::insert(Tree** root, std::vector<Point>::iterator newValue)
{
Tree* temp;
Tree* parent;
if ((*root) == nullptr)
{
(*root) = new Tree();
(*root)->point = &*newValue;
(*root)->left = nullptr;
(*root)->right = nullptr;
}
else
{
temp = (*root);
while (temp != nullptr)
{
if ((&*newValue)->x == temp->point->x)
{
if ((&*newValue)->y < temp->point->y)
{
parent = temp;
temp = temp->left;
}
else
{
parent = temp;
temp = temp->right;
}
}
else if ((&*newValue)->x < temp->point->x)
{
parent = temp;
temp = temp->left;
}
else
{
parent = temp;
temp = temp->right;
}
}
Tree* newNode = new Tree();
newNode->point = &*newValue;
newNode->left = nullptr;
newNode->right = nullptr;
if ((&*newValue)->x == parent->point->x)
{
if ((&*newValue)->y < parent->point->y)
{
parent->left = newNode;
}
else
{
parent->right = newNode;
}
}
else if ((&*newValue)->x <= parent->point->x)
parent->left = newNode;
else
parent->right = newNode;
}
}
void PointBinaryTreeSort::addToList(Tree* node)
{
if (node == nullptr)
return;
else
{
addToList(node->left);
sortedPoints.push_back(*node->point);
addToList(node->right);
}
}