B树的级别顺序遍历

Level order traversal of a B-tree

本文关键字:顺序 遍历      更新时间:2023-10-16

我正在写一个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]);
    }
}

所以,它完成了!