Hello mọi người, em đang làm bài B-Tree thì gặp bug ạ. Cái B-Tree em đang làm là Deegree 4 của cái visualization này: https://www.cs.usfca.edu/~galles/visualization/BTree.html
Ví dụ là:
9
15 10 5 30 50 80 90 60 40
Khi nó chạy tới 80 thì nó chạy vô điều kiện thứ 2, chạy vô split_node luôn, xong out ra hàm thì root, với root->parent bị xóa hết luôn. Hú hồn, em ko biết nó bị gì nữa, mong các anh xem và gợi ý giúp em với, em cảm ơn nhiều.
// B-Tree.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
// B-Tree.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include "pch.h"
#include <iostream>
#include <vector>
class B_Tree {
class Node {
public:
std::vector<int> data_;
std::vector<Node*> nextNode_;
Node* parent;
Node() {
parent = NULL;
}
};
public:
B_Tree() {
read_data();
}
void get_tree() {
}
private:
void split_node(Node* &root, Node* &parent) {
Node * neighbour = new Node();
//
neighbour->data_.push_back(root->data_[2]);
parent->data_.push_back(root->data_[1]);
root->data_.pop_back();
root->data_.pop_back();
//
parent->nextNode_.push_back(neighbour);
neighbour->parent = parent;
}
void update_tree(Node* &root, int value) {
// split root node
if (root->data_.size() == 3 && root->parent == NULL) {
Node * newParent = new Node();
root->parent = newParent;
newParent->nextNode_.push_back(root);
split_node(root, newParent);
root = newParent;
update_tree(root, value);
return;
}
// split none root node
else if (root->data_.size() == 3 && root->parent != NULL) {
split_node(root, root->parent);
update_tree(root->parent, value);
return;
}
// DFS
else if (root->nextNode_.size() != 0) {
for (int i = 0; i < root->data_.size(); i++) {
if (root->data_[i] > value) {
update_tree(root->nextNode_[i], value);
return;
}
else if (i == root->data_.size() - 1) {
update_tree(root->nextNode_[i + 1], value);
return;
}
}
}
// add value to the leaf
else {
for (int i = 0; i < root->data_.size(); i++) {
if (root->data_[i] > value) {
root->data_.insert(root->data_.begin() + i, value);
return;
}
}
root->data_.push_back(value);
return;
}
}
void read_data() {
int n, value;
std::cin >> n;
Node * root = new Node;
for (int i = 0; i < n; i++) {
std::cin >> value;
update_tree(root, value);
}
}
};
int main()
{
B_Tree tree;
}
Trong split_node nó đều chạy đúng hết cả.

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?