我正在为我目前正在学习的一门课程研究 B 树(或者它是 BTree?)。我大部分都正确实施(我认为)。但是,我无法确定中序遍历。这是我的主要功能:
Tree<char, 5>* tree = new Tree<char, 5>();
char entries[] = {'a', 'g', 'f', 'b', 'k', 'd', 'h', 'm', 'j', 'e', 's',
'i', 'r', 'x', 'c', 'l', 'n', 't', 'u', 'p' };
for (int i = 0; i < 20; i++) {
tree->insert(entries[i]);
cout << i << ":\t";
tree->inorder();
cout << endl;
}
所以我创建了一个包含字符的 5 路 btree。我将每个字符插入到树中,然后显示每次迭代的中序遍历以进行调试。这是我得到的输出:
0: a
1: ag
2: afg
3: abfg
4: abffgk
5: abdgfgk
6: abdgfghk
7: abdgfghkm
8: abdgfghjjkm
9: abdefghjjkm
10: abdefghjjkms
11: abdefghimjkms
12: abdefghimjkmrs
13: abdefghimjkmrrsx
14: abccdefghimjkmrrsx
15: abccdefghimjklmsrsx
16: abccdefghimjklmnrsx
17: abccdefghimjklmnrstx
18: abccdefghimjklmnrstux
19: abccdefghimjjklmmnprstux
在几乎所有这些字符中,一些字符是重复的,但在插入之间不一致,所以(对我而言)似乎没有重复数据进入。我似乎无法理解它,但这是我的中序法:
template <class Record, int order>
void Tree<Record, order>::inorder()
{
inorder(root);
}
template <class Record, int order>
void Tree<Record, order>::inorder(Node<Record, order> *current)
{
for (int i = 0; i < current->count+1; i++) {
if (current->branch[i])
inorder(current->branch[i]);
if (i < order-1 && current->data[i])
cout << current->data[i];
}
}
在我的节点实现中,count 是树中“数据”(每个字符)的数量。count+1 将是非叶节点从节点上脱落的分支数。分支是下一组节点的数组,数据是字符数组。
这是我的节点实现:
template <class Record, int order>
struct Node
{
int count;
Record data[order - 1];
Node<Record, order>* branch[order];
Node() : count(0) {}
};
这是用于插入的所有内容:
template <class Record, int order>
ErrorCode Tree<Record, order>::insert(const Record& new_entry)
{
Record median;
Node<Record, order> *right_branch, *new_root;
ErrorCode result = push_down(root, new_entry, median, right_branch);
if (result == overflow) {
new_root = new Node<Record, order>();
new_root->count = 1;
new_root->data[0] = median;
new_root->branch[0] = root;
new_root->branch[1] = right_branch;
root = new_root;
result = success;
}
return result;
}
template <class Record, int order>
ErrorCode Tree<Record, order>::push_down(
Node<Record, order> *current,
const Record &new_entry,
Record &median,
Node<Record, order> *&right_branch)
{
ErrorCode result;
int position;
if (current == NULL) {
median = new_entry;
right_branch = NULL;
result = overflow;
}
else {
if (search_node(current, new_entry, position) == success)
result = duplicate_error;
else {
Record extra_entry;
Node<Record, order> *extra_branch;
result = push_down(current->branch[position], new_entry,
extra_entry, extra_branch);
if (result == overflow) {
if (current->count < order - 1) {
result = success;
push_in(current, extra_entry, extra_branch, position);
}
else
split_node(current, extra_entry, extra_branch, position,
right_branch, median);
}
}
}
return result;
}
template <class Record, int order>
void Tree<Record, order>::push_in(Node<Record, order> *current,
const Record &entry,
Node<Record, order> *right_branch,
int position)
{
for (int i = current->count; i > position; i--) {
current->data[i] = current->data[i-1];
current->branch[i+1] = current->branch[i];
}
current->data[position] = entry;
current->branch[position+1] = right_branch;
current->count++;
}