// File: 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
movq %rsp, %rbp
subq $0x40, %rsp
movq %rsi, -0x40(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x38(%rbp)
movq (%rdi), %rax
movq (%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rsi
callq 0x2700
movq %rax, -0x20(%rbp)
movq -0x40(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x3840
testb $0x1, %al
jne 0x314d
jmp 0x328a
movq -0x38(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x2c80
testb $0x1, %al
jne 0x3160
jmp 0x3165
jmp 0x3277
movq -0x18(%rbp), %rax
cmpq $0x0, 0x48(%rax)
je 0x3275
movq -0x18(%rbp), %rax
cmpq $0x0, 0x50(%rax)
je 0x3275
movq -0x18(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rax
movq 0x40(%rax), %rax
movq -0x18(%rbp), %rcx
movq 0x48(%rcx), %rcx
cmpq 0x40(%rcx), %rax
jbe 0x31b3
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x31bf
movq -0x20(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x31c1
movq -0x30(%rbp), %rax
cmpq -0x20(%rbp), %rax
jne 0x31d0
jmp 0x325b
movq -0x30(%rbp), %rax
cmpq -0x20(%rbp), %rax
jbe 0x3215
movq -0x38(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x40(%rax)
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rsi
callq 0x2700
addq $0x1, %rax
addq -0x20(%rbp), %rax
movq %rax, -0x20(%rbp)
jmp 0x3254
movq -0x38(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x40(%rax)
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rsi
callq 0x2700
movq %rax, %rcx
addq $0x1, %rcx
movq -0x20(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x20(%rbp)
jmp 0x3256
jmp 0x31c1
movq -0x18(%rbp), %rsi
movq -0x28(%rbp), %rdi
callq 0x3950
movq -0x38(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x2c80
jmp 0x3277
movq -0x38(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x25a0
addq $0x40, %rsp
popq %rbp
retq
movq -0x40(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x38d0
testb $0x1, %al
jne 0x329d
jmp 0x32d8
movq -0x38(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x40(%rax)
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rsi
callq 0x2700
addq $0x1, %rax
addq -0x20(%rbp), %rax
movq %rax, -0x20(%rbp)
jmp 0x3317
movq -0x38(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x40(%rax)
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rsi
callq 0x2700
movq %rax, %rcx
addq $0x1, %rcx
movq -0x20(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x20(%rbp)
jmp 0x3319
jmp 0x3137
nop