B树的级别顺序遍历
Level order traversal of a B-tree
我正在写一个B-树的符号。我在这里读到stackoverflow,最好的方法是使用队列进行级别顺序遍历。但我不知道该怎么做。我致力于从极客到极客的c++实现。也许有人知道如何重新构建有序遍历(下面的代码)以实现有序遍历。
类别和构造:
class BTreeNode
{
int *keys; // An array of keys
int t; // Minimum degree (defines the range for number of keys)
BTreeNode **C; // An array of child pointers
int n; // Current number of keys
int j;
bool leaf; // Is true when node is leaf. Otherwise false
public:
BTreeNode(int _t, bool _leaf); // Constructor
// A utility function to insert a new key in the subtree rooted with
// this node. The assumption is, the node must be non-full when this
// function is called
void insertNonFull(int k);
// A utility function to split the child y of this node. i is index of y in
// child array C[]. The Child y must be full when this function is called
void splitChild(int i, BTreeNode *y);
// A function to traverse all nodes in a subtree rooted with this node
void traverse();
// A function to search a key in subtree rooted with this node.
BTreeNode *search(int k); // returns NULL if k is not present.
// Make BTree friend of this so that we can access private members of this
// class in BTree functions
friend class BTree;
};
// A BTree
class BTree
{
BTreeNode *root; // Pointer to root node
int t; // Minimum degree
public:
// Constructor (Initializes tree as empty)
BTree(int _t)
{ root = NULL; t = _t; }
// function to traverse the tree
void traverse()
{ if (root != NULL)
cout << "root";
root->traverse(); }
// function to search a key in this tree
BTreeNode* search(int k)
{ return (root == NULL)? NULL : root->search(k); }
// The main function that inserts a new key in this B-Tree
void insert(int k);
};
订单遍历:
void BTreeNode::traverse()
{
// There are n keys and n+1 children, travers through n keys
// and first n children
int i;
for (i = 0; i < n; i++)
{
// If this is not leaf, then before printing key[i],
// traverse the subtree rooted with child C[i].
if (leaf == false)
C[i]->traverse();
cout << " " << keys[i];
}
// Print the subtree rooted with last child
if (leaf == false)
C[i]->traverse();
}
在这里您可以看到深度优先搜索算法(wiki)的递归实现
对于逐层遍历,您可能需要广度优先搜索(wiki)。
为此,我们将执行两个步骤
第一步:编写无递归的DFS:
void BTreeNode::traverse()
{
std::stack<BTreeNode*> stack;
stack.push(this);
while (!stack.empty())
{
BTreeNode* current = stack.top();
stack.pop();
int i;
for (i = 0; i < n; i++)
{
if (leaf == false)
stack.push(current->C[i]);
cout << " " << current->keys[i];
}
if (leaf == false)
stack.push(current->C[i]);
}
}
第二步:使用队列而不是堆栈:
void BTreeNode::traverse()
{
std::queue<BTreeNode*> queue;
queue.push(this);
while (!stack.empty())
{
BTreeNode* current = queue.front();
queue.pop();
int i;
for (i = 0; i < n; i++)
{
if (leaf == false)
stack.push(current->C[i]);
cout << " " << current->keys[i];
}
if (leaf == false)
stack.push(current->C[i]);
}
}
所以,它完成了!
相关文章:
- 在循环中按顺序遍历成员变量
- 二叉树级别顺序遍历在leetcode中
- 使用二叉树的关卡顺序概念的垂直顺序遍历
- 按顺序遍历 AVL 树,将值保存在数组中
- 树木中的BFS(级别顺序遍历)
- 按特定顺序遍历 NxN 矩阵的上三角形
- 执行策略如何用于C++中的顺序遍历算法
- 二叉树级别顺序遍历 LeetCode
- 如何修复关卡顺序遍历问题(二叉树)的无限循环错误
- 使用队列在二进制树中的水平顺序遍历
- 二进制树的水平顺序遍历
- 二叉树顺序遍历在C++
- 二叉树顺序遍历错误:没有用于调用的匹配函数
- 二进制搜索树中按顺序遍历的复杂性(使用迭代器)
- 如何以相反的顺序遍历 boost::d ynamic_bitset
- 关卡顺序遍历:删除子树
- 使用级别顺序遍历将节点插入二叉树
- 二叉树顺序遍历导致堆栈溢出
- 二进制搜索树,按顺序遍历,步骤3逻辑辅助
- 线程内二叉树的插入或顺序遍历有什么问题