Search code examples
c++b-tree

How to do InOrder traversal with B-Tree?


struct BTreeNode {
    bool is_leaf=true;
    std::vector<int> elements;
    std::vector<BTreeNode*> children;
    BTreeNode() {}
    BTreeNode (std::vector<int> v) {
    this->elements = v;
    }
};

void traverse(BTreeNode* root) {
  for(int i = 0; i < (int)root->children.size(); ++i){
    traverse(root->children[i]);
    cout << root->elements[i] << endl;
  }
  traverse(root->children[root->children.size() -1]);
}

My method somehow segfaults. How do we write a correct inOrder Traversal for B-Tree?


Solution

  • It's probably the last traverse call when you are at a leaf. I don't think that this traverse is needed.