// File: /workspace/github_cpp_makefile_star2_O2/dvjdjvu[P]bntree/./src/bntree.cpp
// Function: bntree::erase(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
void bntree::erase(string key) {

    node_t *search_node = this->tree->root;
    uint64_t index_node = this->get_child_weight(search_node->left);

    for (;;) {
        if (key == search_node->data.key) {
            if (this->erase_simple(search_node)) {
                // pass
            } else if (search_node->left && search_node->right) {
                // Самый сложный случай, удаляемый узел имеет 2-х детей.
                
                node_t *del_node = search_node;
                uint64_t _index;
                // Маленькая балансировка, если правый вес больше левого то удаляем через право
                // иначе через лево
                if (search_node->right->weight > search_node->left->weight) {
                    _index = index_node + 1;
                } else {
                    _index = index_node - 1;
                }

                // Ищем узел со следующим индексом.
                for (;;) {
                    if (_index == index_node) {
                        // Узел найден
                        break;
                    } else if (_index > index_node) {
                        search_node->weight--;
                        search_node = search_node->right;
                        index_node += (this->get_child_weight(search_node->left) + 1);
                    } else {
                        search_node->weight--;

                        search_node = search_node->left;
                        index_node -= (this->get_child_weight(search_node->right) + 1);
                    }
                }

                del_node->data = search_node->data;
                this->erase_simple(search_node);
            }

            this->node_free(search_node);

            return;
        } else if (key > search_node->data.key) {
            search_node->weight--;
            search_node = search_node->right;
            index_node += (this->get_child_weight(search_node->left) + 1);
        } else {
            search_node->weight--;
            search_node = search_node->left;
            index_node -= (this->get_child_weight(search_node->right) + 1);
        }

    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r15
movq %rdi, %rbx
movq (%rdi), %rax
movq (%rax), %r14
movq 0x48(%r14), %rax
testq %rax, %rax
je 0x29c2
movq 0x40(%rax), %r12
jmp 0x29c5
xorl %r12d, %r12d
pushq $0x1
popq %r13
pushq $-0x1
popq %rbp
movq %r15, %rdi
movq %r14, %rsi
callq 0x2d76
testb %al, %al
jne 0x2a27
movq %r15, %rdi
movq %r14, %rsi
callq 0x2d99
decq 0x40(%r14)
testb %al, %al
je 0x2a04
movq 0x50(%r14), %r14
movq 0x48(%r14), %rax
testq %rax, %rax
je 0x2a1a
movq 0x40(%rax), %rax
incq %rax
jmp 0x2a22
movq 0x48(%r14), %r14
movq 0x50(%r14), %rax
testq %rax, %rax
je 0x2a1f
movq 0x40(%rax), %rax
notq %rax
jmp 0x2a22
movq %r13, %rax
jmp 0x2a22
movq %rbp, %rax
addq %rax, %r12
jmp 0x29cc
movq %rbx, %rdi
movq %r14, %rsi
callq 0x278a
testb %al, %al
jne 0x2aad
movq 0x48(%r14), %rax
testq %rax, %rax
je 0x2aad
movq 0x50(%r14), %rcx
testq %rcx, %rcx
je 0x2aad
movq 0x40(%rax), %rdx
xorl %eax, %eax
cmpq 0x40(%rcx), %rdx
adcq $-0x1, %rax
orq $0x1, %rax
addq %r12, %rax
pushq $0x1
popq %rcx
pushq $-0x1
popq %rdx
movq %r14, %r15
cmpq %r12, %rax
je 0x2ac6
decq 0x40(%r15)
cmpq %r12, %rax
jbe 0x2a8a
movq 0x50(%r15), %r15
movq 0x48(%r15), %rsi
testq %rsi, %rsi
je 0x2aa0
movq 0x40(%rsi), %rsi
incq %rsi
jmp 0x2aa8
movq 0x48(%r15), %r15
movq 0x50(%r15), %rsi
testq %rsi, %rsi
je 0x2aa5
movq 0x40(%rsi), %rsi
notq %rsi
jmp 0x2aa8
movq %rcx, %rsi
jmp 0x2aa8
movq %rdx, %rsi
addq %rsi, %r12
jmp 0x2a66
movq %r14, %r15
movq %r15, %rsi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x243a
movq %r14, %rdi
movq %r15, %rsi
callq 0x2dc2
movq %rbx, %rdi
movq %r15, %rsi
callq 0x278a
jmp 0x2ab0