简体   繁体   中英

How should I approach the task of visualizing a B+ tree in C++?

me and my friends have to implement a simple DBMS for our class on databases.

The main part of DBMS is ready to use, which means that all the commands(insert, delete, update, select) have been tested thoroughly and seem to work pretty fine.

Now for the last feature we have to implement a B+ tree, which is very difficult in my honest opinion.

What we tried to do is first to implement a simple B+ tree that would work in the main memory(before implementing the file based version). After reading online about B+ theory and also studying various pseudocodes we have managed to create a recursive implementation and I used the debugger of VS2010 to test it and it seems like it works very well.

The thing is, I would like to somewhat visualize the tree that is created because I believe this will make our life easier in debugging. I mean if you can see the tree like it actually is, then you can know for certain if the result is correct or not.

So, let's take for example the simplest case. Suppose the B+ tree has on its nodes 2 integers as data and three pointers to child nodes.

Let's insert the numbers 40,50,48,20,57,49. From the following website: http://www.seanster.com/BplusTree/BplusTree.html

we get the following animation:

在此处输入图片说明

I added the arrows.

Now I would like to print this tree in C++ in the following way:

          [48|50]
  [20|40] [48|49] [50|57]

however I'm not sure how I should do that. I have read about tree traversals for example preorder, postorder, inorder however I don't think they would help me achieve what I want.

what I know is only the root node. From that root node, I must somewhat traverse the tree in the following way:

  1. visit root
  2. print root
  3. visit children of root
  4. print the values of each child
  5. for each child of the root, visit the children of it(which is the grandchildren of the root).
  6. print the values of the grand child
  7. do the same for the other grand children
  8. in the same way do the same for the children of the grand children etc

What would be the best way to approach this problem? Thanks in advance

Expanding on my comment about level-order traversal - something like this should work, if I understand what you're asking correctly? Well, it won't actually work because myNode and whatnot are not defined, but hopefully it shows the idea.

std::vector<std::vector<std::string> > reprVect;

void visit(myNode n) { 
    if (reprVect.size() < myNode.level)
        reprVect.push_back(std::vector<std::string>());
    }
    reprVect[myNode.level].push_back(myNode.string_repr());
}

// [...]

std::queue<myNode> q;
q.push_back(rootNode);
while (!q.empty()) {
    myNode curNode = q.front();
    q.pop_front();
    visit(q);
    if (curNode.left != NULL) {
        q.push_back(curNode.left);
    } else { /*insert blank into the reprVect */ }
    if (curNode.right != NULL) {
        q.push_back(curNode.right);
    } else { /*insert blank into the reprVect */ }
}

// use cout to print contents of reprVect

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM