Fix memory leak in balancing.

This commit is contained in:
2026-07-28 18:16:01 +01:00
parent 9c84118e63
commit dbbe05c582
2 changed files with 11 additions and 6 deletions
+7 -6
View File
@@ -21,18 +21,19 @@ int main() {
vase.insert(100, "gr\ntt", 5);
vase.insert(20, "gr\ntt", 5);
vase.type(5, 'k');
vase.type(6, 'l');
vase.type(7, 'm');
vase.type(8, 'n');
vase.type(5, '1');
vase.type(6, '2');
vase.type(7, '3');
vase.type(8, '4');
vase.erase(9, -2);
print_shard(vase.root);
LineIterator it(vase.root, 3);
std::string line;
while (it.next(line)) {
while (it.next(line))
std::cout << line << "\n";
}
std::cout << "\n\n"
<< vase.to_string();
+4
View File
@@ -43,10 +43,12 @@ Shard *balance(Shard *node) {
if (bf > 1) {
Branch *left = (Branch *)b->left;
if (balance_factor(left) < 0) {
Shard::retain(left);
auto new_left = rotate_left(left);
auto rebuilt = new Branch(new_left, b->right);
auto result = rotate_right((Branch *)rebuilt);
Shard::release(new_left);
Shard::release(b);
return result;
}
return rotate_right(b);
@@ -55,10 +57,12 @@ Shard *balance(Shard *node) {
if (bf < -1) {
Branch *right = (Branch *)b->right;
if (balance_factor(right) > 0) {
Shard::retain(right);
auto new_right = rotate_right(right);
auto rebuilt = new Branch(b->left, new_right);
auto result = rotate_left((Branch *)rebuilt);
Shard::release(new_right);
Shard::release(b);
return result;
}
return rotate_left(b);