-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFF_AVL_Tree.cpp
178 lines (155 loc) · 4.11 KB
/
FF_AVL_Tree.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
#include "FF_AVL_Tree.h"
using namespace std;
FF_AVL_Tree::FF_AVL_Tree()
{
mRoot = nullptr;
bin_cnt = 0;
}
FF_AVL_Tree::~FF_AVL_Tree()
{
clear(mRoot);
}
int FF_AVL_Tree::balance(const FF_AVL_Node *root)
{
return (root == nullptr ? 0 : height(root->mLeft) - height(root->mRight));
}
int FF_AVL_Tree::height(const FF_AVL_Node *root)
{
return (root == nullptr ? -1 : root->mHeight);
}
double FF_AVL_Tree::get_brc(const FF_AVL_Node* root)
{
return (root == nullptr? 0.0 : root->mValue.get_BRC());
}
double FF_AVL_Tree::get_rc(const FF_AVL_Node* root)
{
return (root == nullptr? 0.0 : root->mValue.get_RC());
}
void FF_AVL_Tree::insert(const FF_Bin& value)
{
mRoot = insert(mRoot, value);
++bin_cnt;
mRoot->mHeight = std::max(height(mRoot->mRight), height(mRoot->mRight)) + 1;
}
FF_AVL_Node* FF_AVL_Tree::right_rotate(FF_AVL_Node *root)
{
FF_AVL_Node *pivot{root->mLeft};
FF_AVL_Node *r_child{pivot->mRight};
pivot->mRight = root;
root->mLeft = r_child;
// update brc of bin at old root
update_bin(root);
// update brc of bin at new root
update_bin(pivot);
root->mHeight = std::max(height(root->mLeft), height(root->mRight)) + 1;
pivot->mHeight = std::max(height(pivot->mLeft), height(pivot->mRight)) + 1;
return pivot;
}
FF_AVL_Node* FF_AVL_Tree::left_rotate(FF_AVL_Node *root)
{
FF_AVL_Node *pivot{root->mRight};
FF_AVL_Node *orphan{pivot->mLeft};
pivot->mLeft = root;
root->mRight = orphan;
// update brc of bin at old root
update_bin(root);
// update brc of bin at new root
update_bin(pivot);
root->mHeight = std::max(height(root->mLeft), height(root->mRight)) + 1;
pivot->mHeight = std::max(height(pivot->mLeft), height(pivot->mRight)) + 1;
return pivot;
}
FF_AVL_Node* FF_AVL_Tree::insert(FF_AVL_Node *root, const FF_Bin& value)
{
if (!root)
return(new FF_AVL_Node(value));
root->mRight = insert(root->mRight, value);
root->mHeight = 1 + std::max(height(root->mLeft), height(root->mRight));
if (balance(root) > 1) // left subtree is higher than right subtree
{
if (balance(root->mLeft) == 1) {
return right_rotate(root);
} else if (balance(root->mLeft) == -1) {
root->mLeft = left_rotate(root->mLeft);
return right_rotate(root);
}
}
else if (balance(root) < -1)
{
if (balance(root->mRight) == -1)
{
return left_rotate(root);
} else if (balance(root->mRight) == 1) {
root->mRight = right_rotate(root->mRight);
return left_rotate(root);
}
}
update_bin(root);
return root;
}
void FF_AVL_Tree::update_bin(FF_AVL_Node* root)
{
if(get_brc(root->mLeft) < get_brc(root->mRight))
{
root->mValue.set_BRC( std::max( get_brc(root->mRight), get_rc(root) ) );
} else
{
root->mValue.set_BRC( std::max( get_brc(root->mLeft), get_rc(root) ) );
}
}
int FF_AVL_Tree::insert_item(FF_AVL_Node* root, double weight)
{
int index;
if(get_brc(root->mLeft) >= weight)
{
index = insert_item(root->mLeft,weight);
update_bin(root);
return index;
} else if(get_rc(root) >= weight)
{
root->mValue.set_RC(get_rc(root) - weight);
update_bin(root);
return root->mValue.get_index();
} else
{
index = insert_item(root->mRight,weight);
update_bin(root);
return index;
}
}
int FF_AVL_Tree::insert_item(double weight)
{
return insert_item(mRoot, weight);
}
bool FF_AVL_Tree::is_fit_in_current_bins(double rc) const
{
return get_brc(mRoot) >= rc;
}
void FF_AVL_Tree::clear(FF_AVL_Node *root)
{
if (!root)
return;
if (root->mLeft)
clear(root->mLeft);
if (root->mRight)
clear(root->mRight);
delete root;
root = nullptr;
}
void FF_AVL_Tree::get_inorder(vector<double>& free_space, FF_AVL_Node* root)
{
if(root == nullptr)
return;
get_inorder(free_space, root->mLeft);
free_space.push_back(get_rc(root));
get_inorder(free_space, root->mRight);
}
void FF_AVL_Tree::get_free_space(vector<double>& free_space)
{
free_space = {};
get_inorder(free_space, mRoot);
}
int FF_AVL_Tree::get_bin_cnt() const
{
return bin_cnt;
}