-3

I am writing a symulator of a B-tree. I read here stackoverflow that the best way is use a queue to make a level order traversal. But i have no idea how to do it. I work on implementation in c++ from geeksforgeeks. Perhaps someone knows how to rebuild inorder traversal(code below) to level order traversal.

Classes and constuctors :

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);
};

Inorder traversal :

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();
}
Community
  • 1
  • 1
Jonatan23
  • 33
  • 1
  • 4
  • 5
    If you are having a problem with your code, please explain what it is. If you are asking for someone to write code for you, go somewhere else. – Scott Hunter Nov 05 '15 at 12:17

1 Answers1

0

Here you can see the Depth-First-Search algorithm (wiki) recursive implementation.
For level-by-level traversal you probably need the Breadth-First-Search (wiki).

To achieve this, we will perform 2 steps.
First step: write recursion-free 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]);
    }
}

Second step: use queue instead of stack:

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]);
    }
}

So, it's done!

alexeykuzmin0
  • 6,344
  • 2
  • 28
  • 51
  • thanks for response. I tried it and it works only for one key in node, when node has more keys it write only first one. Example : Insert (10,20,5,6) for degree = 2, write : 10,5,20. I analyzied it and saw that leaf is always false and in leaf-node doesn't go to true. – Jonatan23 Nov 05 '15 at 18:10