• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

B树的插入和删除

武飞扬头像
naturerun
帮助1

B树的删除(仅考虑阶数m>=3的情形)

在非叶节点中删除关键码,只需从关键码右侧指针指向的子树中寻找最小关键码(沿最左侧指针向下走到叶节点,叶节点最左侧关键码即是)替换该关键码,并从最小关键码所在叶节点删除该最小关键码即可。这样只讨论在叶节点中删除关键码。

在叶节点中删除给定关键码,先删去该关键码及其右侧空指针,然后若该叶节点为根节点,则删除操作结束(删除后的B树可能为空树),若该叶节点不为根节点且关键码数大于等于ceil(m/2)-1,则删除操作结束。若该叶节点不为根节点且关键码数等于ceil(m/2)-2,则令该叶节点为当前节点,然后

设删除过程中当前节点关键数等于ceil(m/2)-2.

若当前节点为根节点且关键码数为0,则令根节点指针指向根节点唯一子树,删除根节点,删除操作结束

若当前节点不为根节点,则若该节点有右兄弟节点且右兄弟节点关键码数大于等于ceil(m/2),则将父节点指向当前节点的指针右侧关键码下移至当前节点最右侧,将右兄弟节点最左侧指针移至当前节点最右侧,最后将右兄弟最左侧关键码上移至父节点指向其指针的左侧,删除操作结束。

否则若当前节点不为根节点,有左兄弟节点,且左兄弟节点关键码数大于等于ceil(m/2),则将父节点中指向当前节点的指针左侧关键码下移至当前节点最左端,左兄弟最右侧关键码上移至父节点中指向它的指针右侧关键码位置,左兄弟最右侧指针移至当前节点最左端,随后删除操作结束

否则若当前节点不为根节点,有右兄弟且右兄弟关键码数等于ceil(m/2)-1,则将父节点中指向当前节点的指针右侧关键码下移至当前节点最右侧,然后将右兄弟完整拼接至当前节点右侧,随后删除右兄弟节点和父节点中指向它的指针。此时父节点关键码子树数减一,若父节点为根节点且关键码数为0则回溯至父节点否则删除操作结束;若父节点不为根节点且关键码数大于等于ceil(m/2)-1,则删除操作结束;若父节点不为根节点且关键码数等于ceil(m/2)-2,则回溯至父节点

否则若当前节点不为根节点,有左兄弟,且左兄弟关键码数等于ceil(m/2)-1,则将父节点中指向当前节点的指针左侧关键码下移至当前节点最左端,左兄弟完整拼接至当前节点左侧,删除左兄弟和父节点中指向它的指针,此时父节点关键码子树数减一,若父节点为根节点且关键码数为0则回溯至父节点否则删除操作结束;若父节点不为根节点且关键码数大于等于ceil(m/2)-1,则删除操作结束;若父节点不为根节点且关键码数等于ceil(m/2)-2,则回溯至父节点

B树的插入(仅考虑阶数m>=3的情形)

在空树中插入直接新建根节点,两指针域置空,填入关键码,再令root指向根节点

若在非空树中插入

那么仅在叶节点上插入,通过搜索在叶节点中找到插入位置,将关键码和空指针构成的二元组插入至叶节点,若插入后叶节点关键码数小于等于m-1则插入结束,否则令叶节点为当前节点

现设插入过程中当前节点关键码数为m,若当前节点左兄弟节点关键码数小于m-1,则从当前节点取关键码子树放入左兄弟节点中,否则若当前节点右兄弟节点关键码数小于m-1则从当前节点取关键码子树放入右兄弟节点中

否则以当前节点从左至右第ceil(m/2)个关键码为分割点将当前节点分隔为左右两部分(即分割点左侧和右侧的部分),左侧分割部分为原当前节点,若原当前节点为根节点,则直接新建根节点,两指针域分别指向左右分割部分,中间关键码即为原当前节点第ceil(m/2)个关键码,令root指向新根节点插入结束。

若原当前节点不为根节点,则将右侧分割部分的指针和原当前节点第ceil(m/2)个关键码构成的二元组插入至父节点中原当前节点对应的二元组右侧,此时父节点关键码子树数增一,若父节点关键码数小于等于m-1则插入结束,若父节点关键码数等于m,则以父节点为当前节点,回溯处理

 插入和删除的C 实现(如有错误欢迎指出)

  1.  
    #include <iostream>
  2.  
    #include <utility>
  3.  
    #include <stack>
  4.  
    #include <vector>
  5.  
    #include <algorithm>
  6.  
    #include <ctime>
  7.  
    #include <random>
  8.  
    using namespace std;
  9.  
     
  10.  
    const int M = 4; //B树阶数
  11.  
    template <typename T>
  12.  
    struct BTreeNode
  13.  
    {
  14.  
    struct BranchNode
  15.  
    {
  16.  
    unsigned long long _key_num = 0;
  17.  
    BTreeNode* p;
  18.  
    vector<pair<T, BTreeNode<T>*>> keyptrmap; //节点数据域和指针域
  19.  
    BranchNode() :keyptrmap(), p(nullptr) {}
  20.  
    };
  21.  
     
  22.  
    union
  23.  
    {
  24.  
    vector<T>* leaf_data;
  25.  
    BranchNode* branch_data;
  26.  
    };
  27.  
     
  28.  
    enum flag { leaf, branch } NodeFlag; //节点标志叶节点or分支节点
  29.  
    BTreeNode(flag type_of_node)
  30.  
    {
  31.  
    NodeFlag = type_of_node;
  32.  
    if (type_of_node == branch)
  33.  
    {
  34.  
    branch_data = new BranchNode();
  35.  
    }
  36.  
    else
  37.  
    {
  38.  
    leaf_data = new vector<T>();
  39.  
    }
  40.  
    }
  41.  
    ~BTreeNode()
  42.  
    {
  43.  
    if (NodeFlag == branch)
  44.  
    {
  45.  
    delete branch_data;
  46.  
    }
  47.  
    else
  48.  
    {
  49.  
    delete leaf_data;
  50.  
    }
  51.  
    }
  52.  
    };
  53.  
     
  54.  
    template <typename T>
  55.  
    pair<typename vector<pair<T, BTreeNode<T>*>>::iterator, bool> SearchBTreeNode(BTreeNode<T>* ptr, pair<typename vector<pair<T, BTreeNode<T>*>>::iterator, bool> d) //返回值:尾后 fasle表示第一指针,尾后 true表示失败,非尾后 true即为对应指针
  56.  
    {
  57.  
    typename vector<pair<T, BTreeNode<T>*>>::iterator m; //实参pair:尾后 false表示从第一指针后一指针开始搜索,尾后 true表示从头搜索,非尾后 true表示从非尾后后一位置开始搜索
  58.  
     
  59.  
    if (d.first == ptr->branch_data->keyptrmap.end())
  60.  
    {
  61.  
    if (d.second)
  62.  
    {
  63.  
    if (ptr->branch_data->p != nullptr)
  64.  
    return { d.first, false };
  65.  
    return { ptr->branch_data->keyptrmap.begin(), false };
  66.  
    }
  67.  
    m = ptr->branch_data->keyptrmap.begin();
  68.  
    }
  69.  
    else
  70.  
    {
  71.  
    m = d.first;
  72.  
    m;
  73.  
    }
  74.  
     
  75.  
    if (m == ptr->branch_data->keyptrmap.end() || m->second != nullptr)
  76.  
    {
  77.  
    return { m, true };
  78.  
    }
  79.  
    return { ptr->branch_data->keyptrmap.begin(), false };
  80.  
    }
  81.  
     
  82.  
    template <typename T>
  83.  
    bool leafKeyFromSmallToBig(BTreeNode<T>* leaf)
  84.  
    {
  85.  
    typename vector<T>::iterator before = leaf->leaf_data->begin();
  86.  
    typename vector<T>::iterator after = before 1;
  87.  
    for (; after != leaf->leaf_data->end(); before = after, after)
  88.  
    {
  89.  
    if (*before >= *after)
  90.  
    return false;
  91.  
    }
  92.  
    return true;
  93.  
    }
  94.  
     
  95.  
    template <typename T>
  96.  
    bool isBTree(BTreeNode<T>* root) //判断给定多叉树是否为B树
  97.  
    {
  98.  
    struct memory
  99.  
    {
  100.  
    BTreeNode<T>* p;
  101.  
    pair<typename vector<pair<T, BTreeNode<T>*>>::iterator, bool> direction;
  102.  
    T nodemin; //节点代表子树中最小值
  103.  
    memory(BTreeNode<T>* p, const pair<typename vector<pair<T, BTreeNode<T>*>>::iterator, bool>& d) :p(p), direction(d) {}
  104.  
    };
  105.  
     
  106.  
    BTreeNode<T>* ptr = root;
  107.  
    pair<typename vector<pair<T, BTreeNode<T>*>>::iterator, bool> d;
  108.  
    if (ptr->NodeFlag != BTreeNode<T>::flag::leaf)
  109.  
    {
  110.  
    d.first = ptr->branch_data->keyptrmap.end();
  111.  
    d.second = true;
  112.  
    }
  113.  
     
  114.  
    T min; //节点某子树中最小节点值
  115.  
    T max; //节点某子树中最大节点值
  116.  
    BTreeNode<T>* const dest = ptr;
  117.  
    stack<memory> arrange;
  118.  
    bool TF = false;
  119.  
    int level = 0;
  120.  
    int beforelevel = 0;
  121.  
    while (true)
  122.  
    {
  123.  
    pair<typename vector<pair<T, BTreeNode<T>*>>::iterator, bool> result;
  124.  
    if (ptr->NodeFlag == BTreeNode<T>::flag::leaf ? true : (result = SearchBTreeNode(ptr, d)) == pair<typename vector<pair<T, BTreeNode<T>*>>::iterator, bool>(ptr->branch_data->keyptrmap.end(), true))
  125.  
    {
  126.  
    if (ptr == dest)
  127.  
    {
  128.  
    if (ptr->NodeFlag == BTreeNode<T>::flag::leaf)
  129.  
    {
  130.  
    if (1 <= ptr->leaf_data->size() && ptr->leaf_data->size() <= M - 1)
  131.  
    {
  132.  
    if (leafKeyFromSmallToBig(ptr))
  133.  
    return true;
  134.  
    cout << "根叶节点关键码没有从小到大排列,非B树" << endl;
  135.  
    return false;
  136.  
    }
  137.  
    else
  138.  
    {
  139.  
    cout << "当前树只有根节点,但根节点子树数量不符合要求,非B树" << endl;
  140.  
    return false;
  141.  
    }
  142.  
    }
  143.  
    else
  144.  
    {
  145.  
    if (1 <= ptr->branch_data->keyptrmap.size() && ptr->branch_data->keyptrmap.size() <= M - 1)
  146.  
    {
  147.  
    typename vector<pair<T, BTreeNode<T>*>>::iterator temp = ptr->branch_data->keyptrmap.end();
  148.  
    --temp;
  149.  
    if (min > temp->first)
  150.  
    {
  151.  
    return true;
  152.  
    }
  153.  
    else
  154.  
    {
  155.  
    cout << "当前树不是" << M << "路搜索树,非B树" << endl;
  156.  
    return false;
  157.  
    }
  158.  
    }
  159.  
    else
  160.  
    {
  161.  
    cout << "当前树根节点子树数量不符合要求,非B树";
  162.  
    return false;
  163.  
    }
  164.  
    }
  165.  
    }
  166.  
    else
  167.  
    {
  168.  
    if (ptr->NodeFlag == BTreeNode<T>::flag::leaf)
  169.  
    {
  170.  
    level;
  171.  
    if (TF == false)
  172.  
    {
  173.  
    beforelevel = level;
  174.  
    TF = true;
  175.  
    }
  176.  
    else
  177.  
    {
  178.  
    if (level != beforelevel)
  179.  
    {
  180.  
    cout << "失败节点不在同一层,非B树" << endl;
  181.  
    return false;
  182.  
    }
  183.  
    }
  184.  
     
  185.  
    if ((M 1) / 2 - 1 <= ptr->leaf_data->size() && ptr->leaf_data->size() <= M - 1)
  186.  
    {
  187.  
    if (leafKeyFromSmallToBig(ptr) == false)
  188.  
    {
  189.  
    cout << "当前树叶结点关键码没有从小到大排列,非B树" << endl;
  190.  
    return false;
  191.  
    }
  192.  
     
  193.  
    if (arrange.top().direction == pair<typename vector<pair<T, BTreeNode<T>*>>::iterator, bool>(arrange.top().p->branch_data->keyptrmap.end(), false))
  194.  
    {
  195.  
    arrange.top().nodemin = *(ptr->leaf_data->begin());
  196.  
    }
  197.  
    min = *(ptr->leaf_data->begin());
  198.  
    max = *(--ptr->leaf_data->end());
  199.  
    }
  200.  
    else
  201.  
    {
  202.  
    cout << "当前树叶节点数量不符合要求,非B树" << endl;
  203.  
    return false;
  204.  
    }
  205.  
    }
  206.  
    else
  207.  
    {
  208.  
    if ((M 1) / 2 - 1 <= ptr->branch_data->keyptrmap.size() && ptr->branch_data->keyptrmap.size() <= M - 1)
  209.  
    {
  210.  
    typename vector<pair<T, BTreeNode<T>*>>::iterator temp = ptr->branch_data->keyptrmap.end();
  211.  
    --temp;
  212.  
    if (min > temp->first)
  213.  
    {
  214.  
    min = arrange.top().nodemin;
  215.  
    arrange.pop();
  216.  
    if (arrange.top().direction == pair<typename vector<pair<T, BTreeNode<T>*>>::iterator, bool>(arrange.top().p->branch_data->keyptrmap.end(), false))
  217.  
    {
  218.  
    arrange.top().nodemin = min;
  219.  
    }
  220.  
    }
  221.  
    else
  222.  
    {
  223.  
    cout << "当前树不是" << M << "路搜索树,非B树" << endl;
  224.  
    return false;
  225.  
    }
  226.  
    }
  227.  
    else
  228.  
    {
  229.  
    cout << "当前树分支节点子树数量不符合要求,非B树" << endl;
  230.  
    return false;
  231.  
    }
  232.  
    }
  233.  
    --level;
  234.  
    ptr = arrange.top().p;
  235.  
    d = arrange.top().direction;
  236.  
    }
  237.  
    }
  238.  
    else
  239.  
    {
  240.  
    if (result == pair<typename vector<pair<T, BTreeNode<T>*>>::iterator, bool>(ptr->branch_data->keyptrmap.begin(), false))
  241.  
    {
  242.  
    cout << "分支节点存在空子树,非B树" << endl;
  243.  
    return false;
  244.  
    }
  245.  
     
  246.  
    if (d == pair<typename vector<pair<T, BTreeNode<T>*>>::iterator, bool>(ptr->branch_data->keyptrmap.end(), true))
  247.  
    {
  248.  
    arrange.push(memory(ptr, result));
  249.  
    ptr = ptr->branch_data->p;
  250.  
    level;
  251.  
    }
  252.  
    else
  253.  
    {
  254.  
    typename vector<pair<T, BTreeNode<T>*>>::iterator temp = result.first;
  255.  
    if (d.first == ptr->branch_data->keyptrmap.end())
  256.  
    {
  257.  
    if (!(max < temp->first))
  258.  
    {
  259.  
    cout << "当前树不是" << M << "路搜索树,非B树" << endl;
  260.  
    return false;
  261.  
    }
  262.  
    }
  263.  
    else
  264.  
    {
  265.  
    if (!(max < temp->first && min > d.first->first))
  266.  
    {
  267.  
    cout << "当前树不是" << M << "路搜索树,非B树" << endl;
  268.  
    return false;
  269.  
    }
  270.  
    }
  271.  
     
  272.  
    arrange.top().direction = pair<typename vector<pair<T, BTreeNode<T>*>>::iterator, bool>(temp, true);
  273.  
    ptr = temp->second;
  274.  
    }
  275.  
    if (ptr->NodeFlag != BTreeNode<T>::flag::leaf)
  276.  
    {
  277.  
    d = pair<typename vector<pair<T, BTreeNode<T>*>>::iterator, bool>(ptr->branch_data->keyptrmap.end(), true);
  278.  
    }
  279.  
    }
  280.  
    }
  281.  
    }
  282.  
     
  283.  
    template <typename T>
  284.  
    bool compare(const pair<T, BTreeNode<T>*>& left, const pair<T, BTreeNode<T>*>& right)
  285.  
    {
  286.  
    return left.first < right.first;
  287.  
    }
  288.  
     
  289.  
    template <typename T>
  290.  
    typename vector<pair<T, BTreeNode<T>*>>::iterator Upper_Bound(const T& key, vector<pair<T, BTreeNode<T>*>>& list)
  291.  
    {
  292.  
    typename vector<pair<T, BTreeNode<T>*>>::iterator left = list.begin();
  293.  
    typename vector<pair<T, BTreeNode<T>*>>::iterator right = list.end() - 1;
  294.  
    while (left <= right)
  295.  
    {
  296.  
    int d = right - left 1;
  297.  
    typename vector<pair<T, BTreeNode<T>*>>::iterator mid;
  298.  
    if (d % 2 == 1)
  299.  
    {
  300.  
    mid = left d / 2;
  301.  
    }
  302.  
    else
  303.  
    {
  304.  
    mid = left (d / 2 - 1);
  305.  
    }
  306.  
     
  307.  
    if (key <= (*mid).first)
  308.  
    {
  309.  
    if (mid == list.begin())
  310.  
    {
  311.  
    return left;
  312.  
    }
  313.  
    right = mid - 1;
  314.  
    }
  315.  
    else
  316.  
    {
  317.  
    left = mid 1;
  318.  
    }
  319.  
    }
  320.  
    return left;
  321.  
    }
  322.  
     
  323.  
    template <typename T>
  324.  
    pair<bool, typename vector<T>::iterator> BinarySearch(vector<T>& list, typename vector<T>::iterator left, typename vector<T>::iterator right, const T& key)
  325.  
    {
  326.  
    while (left <= right)
  327.  
    {
  328.  
    int d = right - left 1;
  329.  
    typename vector<T>::iterator mid;
  330.  
    if (d % 2 == 1)
  331.  
    {
  332.  
    mid = left d / 2;
  333.  
    }
  334.  
    else
  335.  
    {
  336.  
    mid = left (d / 2 - 1);
  337.  
    }
  338.  
     
  339.  
    if (key < *mid)
  340.  
    {
  341.  
    if (mid == list.begin())
  342.  
    {
  343.  
    return { false, left };
  344.  
    }
  345.  
    right = mid - 1;
  346.  
    }
  347.  
    else if (key > *mid)
  348.  
    {
  349.  
    left = mid 1;
  350.  
    }
  351.  
    else
  352.  
    {
  353.  
    return { true, mid };
  354.  
    }
  355.  
    }
  356.  
    return { false, left };
  357.  
    }
  358.  
     
  359.  
    template <typename T>
  360.  
    void increAncestorKeyNum(stack<pair<BTreeNode<T>*, typename vector<pair<T, BTreeNode<T>*>>::iterator>>& stackforback)
  361.  
    {
  362.  
    while (stackforback.empty() == false)
  363.  
    {
  364.  
    stackforback.top().first->branch_data->_key_num;
  365.  
    stackforback.pop();
  366.  
    }
  367.  
    }
  368.  
     
  369.  
    template <typename T>
  370.  
    void decreAncestorKeyNum(stack<pair<BTreeNode<T>*, typename vector<pair<T, BTreeNode<T>*>>::iterator>>& stackforback)
  371.  
    {
  372.  
    while (stackforback.empty() == false)
  373.  
    {
  374.  
    --stackforback.top().first->branch_data->_key_num;
  375.  
    stackforback.pop();
  376.  
    }
  377.  
    }
  378.  
    template <typename T>
  379.  
    unsigned long long getKeyNum(BTreeNode<T>* current)
  380.  
    {
  381.  
    if (current->NodeFlag == BTreeNode<T>::flag::leaf)
  382.  
    {
  383.  
    return current->leaf_data->size();
  384.  
    }
  385.  
    else
  386.  
    {
  387.  
    return current->branch_data->_key_num;
  388.  
    }
  389.  
    }
  390.  
     
  391.  
    template <typename T>
  392.  
    void decrKeyNum(BTreeNode<T>* current, const unsigned long long& decr_num)
  393.  
    {
  394.  
    current->branch_data->_key_num -= decr_num;
  395.  
    }
  396.  
     
  397.  
    template <typename T>
  398.  
    void incrKeyNum(BTreeNode<T>* current, const unsigned long long& decr_num)
  399.  
    {
  400.  
    current->branch_data->_key_num = decr_num;
  401.  
    }
  402.  
     
  403.  
    template <typename T>
  404.  
    bool afterAdjust(stack<pair<BTreeNode<T>*, typename vector<pair<T, BTreeNode<T>*>>::iterator>>& stackforback, BTreeNode<T>*& current)
  405.  
    {
  406.  
    if (stackforback.top().first->branch_data->keyptrmap.size() <= M - 1)
  407.  
    {
  408.  
    increAncestorKeyNum(stackforback);
  409.  
    return true;
  410.  
    }
  411.  
     
  412.  
    current = stackforback.top().first;
  413.  
    incrKeyNum(current, 1);
  414.  
    stackforback.pop();
  415.  
    return false;
  416.  
    }
  417.  
     
  418.  
    template <typename T>
  419.  
    void left_to_right_for_leaf(BTreeNode<T>* left, BTreeNode<T>* right, const typename vector<pair<T, BTreeNode<T>*>>::iterator& mid)
  420.  
    {
  421.  
    right->leaf_data->insert(right->leaf_data->begin(), mid->first);
  422.  
    mid->first = left->leaf_data->back();
  423.  
    left->leaf_data->pop_back();
  424.  
    }
  425.  
     
  426.  
    template <typename T>
  427.  
    void right_to_left_for_leaf(BTreeNode<T>* left, BTreeNode<T>* right, const typename vector<pair<T, BTreeNode<T>*>>::iterator& mid)
  428.  
    {
  429.  
    left->leaf_data->push_back(mid->first);
  430.  
    mid->first = *(right->leaf_data->begin());
  431.  
    right->leaf_data->erase(right->leaf_data->begin());
  432.  
    }
  433.  
     
  434.  
    template <typename T>
  435.  
    void left_to_right_for_branch(BTreeNode<T>* left, BTreeNode<T>* right, const typename vector<pair<T, BTreeNode<T>*>>::iterator& mid)
  436.  
    {
  437.  
    right->branch_data->keyptrmap.insert(right->branch_data->keyptrmap.begin(), make_pair(mid->first, right->branch_data->p));
  438.  
    right->branch_data->p = left->branch_data->keyptrmap.back().second;
  439.  
    mid->first = left->branch_data->keyptrmap.back().first;
  440.  
    left->branch_data->keyptrmap.pop_back();
  441.  
    }
  442.  
     
  443.  
    template <typename T>
  444.  
    void right_to_left_for_branch(BTreeNode<T>* left, BTreeNode<T>* right, const typename vector<pair<T, BTreeNode<T>*>>::iterator& mid)
  445.  
    {
  446.  
    left->branch_data->keyptrmap.push_back(make_pair(mid->first, right->branch_data->p));
  447.  
    mid->first = right->branch_data->keyptrmap.begin()->first;
  448.  
    right->branch_data->p = right->branch_data->keyptrmap.begin()->second;
  449.  
    right->branch_data->keyptrmap.erase(right->branch_data->keyptrmap.begin());
  450.  
    }
  451.  
     
  452.  
    template <typename T>
  453.  
    bool toLeft(stack<pair<BTreeNode<T>*, typename vector<pair<T, BTreeNode<T>*>>::iterator>>& stackforback, BTreeNode<T>* current)
  454.  
    {
  455.  
    if (stackforback.top().second != stackforback.top().first->branch_data->keyptrmap.begin())
  456.  
    {
  457.  
    if (stackforback.top().second - 1 != stackforback.top().first->branch_data->keyptrmap.begin())
  458.  
    {
  459.  
    if (current->NodeFlag == BTreeNode<T>::flag::leaf)
  460.  
    {
  461.  
    if ((stackforback.top().second - 2)->second->leaf_data->size() < M - 1)
  462.  
    {
  463.  
    right_to_left_for_leaf((stackforback.top().second - 2)->second, current, stackforback.top().second - 1);
  464.  
    increAncestorKeyNum(stackforback);
  465.  
    return true;
  466.  
    }
  467.  
    }
  468.  
    else
  469.  
    {
  470.  
    if ((stackforback.top().second - 2)->second->branch_data->keyptrmap.size() < M - 1)
  471.  
    {
  472.  
    decrKeyNum(current, getKeyNum(current->branch_data->p) 1);
  473.  
    incrKeyNum((stackforback.top().second - 2)->second, getKeyNum(current->branch_data->p) 1);
  474.  
     
  475.  
    right_to_left_for_branch((stackforback.top().second - 2)->second, current, stackforback.top().second - 1);
  476.  
    increAncestorKeyNum(stackforback);
  477.  
    return true;
  478.  
    }
  479.  
    }
  480.  
    }
  481.  
    else
  482.  
    {
  483.  
    if (current->NodeFlag == BTreeNode<T>::flag::leaf)
  484.  
    {
  485.  
    if (stackforback.top().first->branch_data->p->leaf_data->size() < M - 1)
  486.  
    {
  487.  
    right_to_left_for_leaf(stackforback.top().first->branch_data->p, current, stackforback.top().second - 1);
  488.  
    increAncestorKeyNum(stackforback);
  489.  
    return true;
  490.  
    }
  491.  
    }
  492.  
    else
  493.  
    {
  494.  
    if (stackforback.top().first->branch_data->p->branch_data->keyptrmap.size() < M - 1)
  495.  
    {
  496.  
    decrKeyNum(current, getKeyNum(current->branch_data->p) 1);
  497.  
    incrKeyNum(stackforback.top().first->branch_data->p, getKeyNum(current->branch_data->p) 1);
  498.  
     
  499.  
    right_to_left_for_branch(stackforback.top().first->branch_data->p, current, stackforback.top().second - 1);
  500.  
    increAncestorKeyNum(stackforback);
  501.  
    return true;
  502.  
    }
  503.  
    }
  504.  
    }
  505.  
    }
  506.  
    return false;
  507.  
    }
  508.  
     
  509.  
    template <typename T>
  510.  
    bool toRight(stack<pair<BTreeNode<T>*, typename vector<pair<T, BTreeNode<T>*>>::iterator>>& stackforback, BTreeNode<T>* current)
  511.  
    {
  512.  
    if (stackforback.top().second != stackforback.top().first->branch_data->keyptrmap.end())
  513.  
    {
  514.  
    if (current->NodeFlag == BTreeNode<T>::flag::leaf)
  515.  
    {
  516.  
    if (stackforback.top().second->second->leaf_data->size() < M - 1)
  517.  
    {
  518.  
    left_to_right_for_leaf(current, stackforback.top().second->second, stackforback.top().second);
  519.  
    increAncestorKeyNum(stackforback);
  520.  
    return true;
  521.  
    }
  522.  
    }
  523.  
    else
  524.  
    {
  525.  
    if (stackforback.top().second->second->branch_data->keyptrmap.size() < M - 1)
  526.  
    {
  527.  
    left_to_right_for_branch(current, stackforback.top().second->second, stackforback.top().second);
  528.  
    decrKeyNum(current, getKeyNum(stackforback.top().second->second->branch_data->p) 1);
  529.  
    incrKeyNum(stackforback.top().second->second, getKeyNum(stackforback.top().second->second->branch_data->p) 1);
  530.  
    increAncestorKeyNum(stackforback);
  531.  
    return true;
  532.  
    }
  533.  
    }
  534.  
    }
  535.  
    return false;
  536.  
    }
  537.  
    template <typename T>
  538.  
    bool toLeftOrRight(stack<pair<BTreeNode<T>*, typename vector<pair<T, BTreeNode<T>*>>::iterator>>& stackforback, BTreeNode<T>* current)
  539.  
    {
  540.  
    if (toRight(stackforback, current))
  541.  
    return true;
  542.  
    if (toLeft(stackforback, current))
  543.  
    return true;
  544.  
    return false;
  545.  
    }
  546.  
    template <typename T>
  547.  
    pair<BTreeNode<T>*, bool> InsertBTree(BTreeNode<T>* root, const T& key) //B树插入函数
  548.  
    {
  549.  
    if (root == nullptr)
  550.  
    {
  551.  
    root = new BTreeNode<T>(BTreeNode<T>::flag::leaf);
  552.  
    root->leaf_data->push_back(key);
  553.  
    return { root, true };
  554.  
    }
  555.  
    else
  556.  
    {
  557.  
    BTreeNode<T>* current = root;
  558.  
    stack<pair<BTreeNode<T>*, typename vector<pair<T, BTreeNode<T>*>>::iterator>> stackforback;
  559.  
     
  560.  
    while (current->NodeFlag == BTreeNode<T>::flag::branch)
  561.  
    {
  562.  
    typename vector<pair<T, BTreeNode<T>*>>::iterator scankey = Upper_Bound(key, current->branch_data->keyptrmap);
  563.  
    if (scankey != current->branch_data->keyptrmap.end())
  564.  
    {
  565.  
    if (scankey->first == key)
  566.  
    {
  567.  
    cout << "关键码" << key << "已存在,插入失败" << endl;
  568.  
    return { root, false };
  569.  
    }
  570.  
    else
  571.  
    {
  572.  
    stackforback.push(make_pair(current, scankey));
  573.  
    if (scankey != current->branch_data->keyptrmap.begin())
  574.  
    {
  575.  
    --scankey;
  576.  
    current = scankey->second;
  577.  
    }
  578.  
    else
  579.  
    {
  580.  
    current = current->branch_data->p;
  581.  
    }
  582.  
    }
  583.  
    }
  584.  
    else
  585.  
    {
  586.  
    stackforback.push(make_pair(current, scankey));
  587.  
    --scankey;
  588.  
    current = scankey->second;
  589.  
    }
  590.  
    }
  591.  
     
  592.  
    pair<bool, typename vector<T>::iterator> scankey = BinarySearch(*current->leaf_data, current->leaf_data->begin(), current->leaf_data->end() - 1, key);
  593.  
    if (scankey.first)
  594.  
    {
  595.  
    cout << "关键码" << key << "已存在,插入失败" << endl;
  596.  
    return { root, false };
  597.  
    }
  598.  
    else
  599.  
    {
  600.  
    current->leaf_data->insert(scankey.second, key);
  601.  
    }
  602.  
     
  603.  
    if (current->leaf_data->size() <= M - 1)
  604.  
    {
  605.  
    increAncestorKeyNum(stackforback);
  606.  
    return { root, true };
  607.  
    }
  608.  
    else
  609.  
    {
  610.  
    if (stackforback.empty() == false)
  611.  
    {
  612.  
    if (toLeftOrRight(stackforback, current))
  613.  
    return { root, true };
  614.  
    }
  615.  
     
  616.  
    unsigned long long Num = current->leaf_data->size();
  617.  
    BTreeNode<T>* ptr = new BTreeNode<T>(BTreeNode<T>::flag::leaf);
  618.  
    ptr->leaf_data->insert(ptr->leaf_data->end(), current->leaf_data->end() - M / 2, current->leaf_data->end());
  619.  
    current->leaf_data->erase(current->leaf_data->end() - M / 2, current->leaf_data->end());
  620.  
     
  621.  
    if (stackforback.empty() == true)
  622.  
    {
  623.  
    root = new BTreeNode<T>(BTreeNode<T>::flag::branch);
  624.  
    root->branch_data->_key_num = Num;
  625.  
    root->branch_data->p = current;
  626.  
    root->branch_data->keyptrmap.push_back(make_pair(current->leaf_data->back(), ptr));
  627.  
    current->leaf_data->pop_back();
  628.  
    return { root, true };
  629.  
    }
  630.  
    else
  631.  
    {
  632.  
    stackforback.top().first->branch_data->keyptrmap.insert(stackforback.top().second, make_pair(current->leaf_data->back(), ptr));
  633.  
    current->leaf_data->pop_back();
  634.  
     
  635.  
    if (afterAdjust(stackforback, current))
  636.  
    return { root, true };
  637.  
    }
  638.  
     
  639.  
    while (true)
  640.  
    {
  641.  
    if (stackforback.empty() == false)
  642.  
    {
  643.  
    if (toLeftOrRight(stackforback, current))
  644.  
    return { root, true };
  645.  
    }
  646.  
     
  647.  
    BTreeNode<T>* ptr = new BTreeNode<T>(BTreeNode<T>::flag::branch);
  648.  
    ptr->branch_data->keyptrmap.insert(ptr->branch_data->keyptrmap.end(), current->branch_data->keyptrmap.end() - M / 2, current->branch_data->keyptrmap.end());
  649.  
    current->branch_data->keyptrmap.erase(current->branch_data->keyptrmap.end() - M / 2, current->branch_data->keyptrmap.end());
  650.  
    ptr->branch_data->p = current->branch_data->keyptrmap.back().second;
  651.  
     
  652.  
    incrKeyNum(ptr, getKeyNum(ptr->branch_data->p) ptr->branch_data->keyptrmap.size());
  653.  
    for (typename vector<pair<T, BTreeNode<T>*>>::iterator run = ptr->branch_data->keyptrmap.begin(); run != ptr->branch_data->keyptrmap.end(); run)
  654.  
    {
  655.  
    incrKeyNum(ptr, getKeyNum(run->second));
  656.  
    }
  657.  
    if (stackforback.empty() == true)
  658.  
    {
  659.  
    root = new BTreeNode<T>(BTreeNode<T>::flag::branch);
  660.  
    root->branch_data->_key_num = current->branch_data->_key_num;
  661.  
    decrKeyNum(current, getKeyNum(ptr) 1);
  662.  
    root->branch_data->p = current;
  663.  
    root->branch_data->keyptrmap.push_back(make_pair(current->branch_data->keyptrmap.back().first, ptr));
  664.  
    current->branch_data->keyptrmap.pop_back();
  665.  
    return { root, true };
  666.  
    }
  667.  
    else
  668.  
    {
  669.  
    stackforback.top().first->branch_data->keyptrmap.insert(stackforback.top().second, make_pair(current->branch_data->keyptrmap.back().first, ptr));
  670.  
    current->branch_data->keyptrmap.pop_back();
  671.  
    decrKeyNum(current, getKeyNum(ptr) 1);
  672.  
     
  673.  
    if (afterAdjust(stackforback, current))
  674.  
    return { root, true };
  675.  
    }
  676.  
    }
  677.  
    }
  678.  
    }
  679.  
    }
  680.  
     
  681.  
    template <typename T>
  682.  
    BTreeNode<T>* afterMerge(BTreeNode<T>* root, BTreeNode<T>*& current, stack<pair<BTreeNode<T>*, typename vector<pair<T, BTreeNode<T>*>>::iterator>>& stackforback)
  683.  
    {
  684.  
    if (stackforback.top().first == root)
  685.  
    {
  686.  
    if (stackforback.top().first->branch_data->keyptrmap.size() == 0)
  687.  
    {
  688.  
    delete root;
  689.  
    return current;
  690.  
    }
  691.  
    decrKeyNum(root, 1);
  692.  
    return root;
  693.  
    }
  694.  
    if (stackforback.top().first->branch_data->keyptrmap.size() >= (M 1) / 2 - 1)
  695.  
    {
  696.  
    decreAncestorKeyNum(stackforback);
  697.  
    return root;
  698.  
    }
  699.  
    current = stackforback.top().first;
  700.  
    decrKeyNum(current, 1);
  701.  
    stackforback.pop();
  702.  
    return nullptr;
  703.  
    }
  704.  
     
  705.  
    template <typename T>
  706.  
    void beforeAdjust(typename vector<pair<T, BTreeNode<T>*>>::iterator& temp, const stack<pair<BTreeNode<T>*, typename vector<pair<T, BTreeNode<T>*>>::iterator>>& stackforback, BTreeNode<T>*& ptr)
  707.  
    {
  708.  
    --temp;
  709.  
    if (temp != stackforback.top().first->branch_data->keyptrmap.begin())
  710.  
    {
  711.  
    typename vector<pair<T, BTreeNode<T>*>>::iterator before = temp - 1;
  712.  
    ptr = before->second;
  713.  
    }
  714.  
    else
  715.  
    {
  716.  
    ptr = stackforback.top().first->branch_data->p;
  717.  
    }
  718.  
    }
  719.  
     
  720.  
    template <typename T>
  721.  
    bool borrowFromLeft(stack<pair<BTreeNode<T>*, typename vector<pair<T, BTreeNode<T>*>>::iterator>>& stackforback, BTreeNode<T>* current)
  722.  
    {
  723.  
    typename vector<pair<T, BTreeNode<T>*>>::iterator temp2 = stackforback.top().second;
  724.  
     
  725.  
    if (temp2 != stackforback.top().first->branch_data->keyptrmap.begin())
  726.  
    {
  727.  
    BTreeNode<T>* ptr = nullptr;
  728.  
    if ((temp2 - 1) == stackforback.top().first->branch_data->keyptrmap.begin())
  729.  
    {
  730.  
    ptr = stackforback.top().first->branch_data->p;
  731.  
    }
  732.  
    else
  733.  
    {
  734.  
    ptr = (temp2 - 2)->second;
  735.  
    }
  736.  
     
  737.  
    if (current->NodeFlag == BTreeNode<T>::flag::leaf)
  738.  
    {
  739.  
    if (ptr->leaf_data->size() >= (M 1) / 2)
  740.  
    {
  741.  
    left_to_right_for_leaf(ptr, current, temp2 - 1);
  742.  
    decreAncestorKeyNum(stackforback);
  743.  
    return true;
  744.  
    }
  745.  
    }
  746.  
    else
  747.  
    {
  748.  
    if (ptr->branch_data->keyptrmap.size() >= (M 1) / 2)
  749.  
    {
  750.  
    left_to_right_for_branch(ptr, current, temp2 - 1);
  751.  
    incrKeyNum(current, getKeyNum(current->branch_data->p) 1);
  752.  
    decrKeyNum(ptr, getKeyNum(current->branch_data->p) 1);
  753.  
    decreAncestorKeyNum(stackforback);
  754.  
    return true;
  755.  
    }
  756.  
    }
  757.  
    }
  758.  
    return false;
  759.  
    }
  760.  
     
  761.  
    template <typename T>
  762.  
    bool borrowFromRight(stack<pair<BTreeNode<T>*, typename vector<pair<T, BTreeNode<T>*>>::iterator>>& stackforback, BTreeNode<T>* current)
  763.  
    {
  764.  
    typename vector<pair<T, BTreeNode<T>*>>::iterator temp = stackforback.top().second;
  765.  
     
  766.  
    if (temp != stackforback.top().first->branch_data->keyptrmap.end())
  767.  
    {
  768.  
    BTreeNode<T>* ptr = temp->second;
  769.  
    if (current->NodeFlag == BTreeNode<T>::flag::leaf)
  770.  
    {
  771.  
    if (ptr->leaf_data->size() >= (M 1) / 2)
  772.  
    {
  773.  
    right_to_left_for_leaf(current, ptr, temp);
  774.  
    decreAncestorKeyNum(stackforback);
  775.  
    return true;
  776.  
    }
  777.  
    }
  778.  
    else
  779.  
    {
  780.  
    if (ptr->branch_data->keyptrmap.size() >= (M 1) / 2)
  781.  
    {
  782.  
    incrKeyNum(current, getKeyNum(ptr->branch_data->p) 1);
  783.  
    decrKeyNum(ptr, getKeyNum(ptr->branch_data->p) 1);
  784.  
    right_to_left_for_branch(current, ptr, temp);
  785.  
    decreAncestorKeyNum(stackforback);
  786.  
    return true;
  787.  
    }
  788.  
    }
  789.  
    }
  790.  
    return false;
  791.  
    }
  792.  
     
  793.  
    template <typename T>
  794.  
    bool borrowFromLeftOrRight(stack<pair<BTreeNode<T>*, typename vector<pair<T, BTreeNode<T>*>>::iterator>>& stackforback, BTreeNode<T>* current)
  795.  
    {
  796.  
    if (borrowFromRight(stackforback, current))
  797.  
    return true;
  798.  
    if (borrowFromLeft(stackforback, current))
  799.  
    return true;
  800.  
    return false;
  801.  
    }
  802.  
     
  803.  
    template <typename T>
  804.  
    BTreeNode<T>* rebalanceAfterDelete(stack<pair<BTreeNode<T>*, typename vector<pair<T, BTreeNode<T>*>>::iterator>>& stackforback, BTreeNode<T>* current, BTreeNode<T>* root)
  805.  
    {
  806.  
    if (current == root)
  807.  
    {
  808.  
    if (current->leaf_data->empty() == true)
  809.  
    {
  810.  
    delete current;
  811.  
    return nullptr;
  812.  
    }
  813.  
    else
  814.  
    {
  815.  
    return current;
  816.  
    }
  817.  
    }
  818.  
    else
  819.  
    {
  820.  
    if (current->leaf_data->size() >= (M 1) / 2 - 1)
  821.  
    {
  822.  
    decreAncestorKeyNum(stackforback);
  823.  
    return root;
  824.  
    }
  825.  
    else
  826.  
    {
  827.  
    BTreeNode<T>* ptr = nullptr;
  828.  
    typename vector<pair<T, BTreeNode<T>*>>::iterator temp = stackforback.top().second;
  829.  
     
  830.  
    if (borrowFromLeftOrRight(stackforback, current))
  831.  
    return root;
  832.  
     
  833.  
    if (temp != stackforback.top().first->branch_data->keyptrmap.end())
  834.  
    {
  835.  
    current->leaf_data->push_back(temp->first);
  836.  
    current->leaf_data->insert(current->leaf_data->end(), temp->second->leaf_data->begin(), temp->second->leaf_data->end());
  837.  
    delete temp->second;
  838.  
    }
  839.  
    else
  840.  
    {
  841.  
    beforeAdjust(temp, stackforback, ptr);
  842.  
    ptr->leaf_data->push_back(temp->first);
  843.  
    ptr->leaf_data->insert(ptr->leaf_data->end(), current->leaf_data->begin(), current->leaf_data->end());
  844.  
    delete current;
  845.  
    current = ptr;
  846.  
    }
  847.  
    stackforback.top().first->branch_data->keyptrmap.erase(temp);
  848.  
     
  849.  
    ptr = afterMerge(root, current, stackforback);
  850.  
     
  851.  
    while (ptr == nullptr)
  852.  
    {
  853.  
    typename vector<pair<T, BTreeNode<T>*>>::iterator temp = stackforback.top().second;
  854.  
     
  855.  
    if (borrowFromLeftOrRight(stackforback, current))
  856.  
    return root;
  857.  
     
  858.  
    if (temp != stackforback.top().first->branch_data->keyptrmap.end())
  859.  
    {
  860.  
    current->branch_data->keyptrmap.push_back(make_pair(temp->first, temp->second->branch_data->p));
  861.  
    current->branch_data->keyptrmap.insert(current->branch_data->keyptrmap.end(), temp->second->branch_data->keyptrmap.begin(), temp->second->branch_data->keyptrmap.end());
  862.  
    incrKeyNum(current, getKeyNum(temp->second) 1);
  863.  
    delete temp->second;
  864.  
    }
  865.  
    else
  866.  
    {
  867.  
    beforeAdjust(temp, stackforback, ptr);
  868.  
    ptr->branch_data->keyptrmap.push_back(make_pair(temp->first, current->branch_data->p));
  869.  
    ptr->branch_data->keyptrmap.insert(ptr->branch_data->keyptrmap.end(), current->branch_data->keyptrmap.begin(), current->branch_data->keyptrmap.end());
  870.  
    incrKeyNum(ptr, getKeyNum(current) 1);
  871.  
    delete current;
  872.  
    current = ptr;
  873.  
    }
  874.  
    stackforback.top().first->branch_data->keyptrmap.erase(temp);
  875.  
     
  876.  
    ptr = afterMerge(root, current, stackforback);
  877.  
    }
  878.  
    return ptr;
  879.  
    }
  880.  
    }
  881.  
    }
  882.  
     
  883.  
    template <typename T>
  884.  
    pair<BTreeNode<T>*, bool> DelBTree(BTreeNode<T>* root, const T& key) //B树删除函数
  885.  
    {
  886.  
    BTreeNode<T>* current = root;
  887.  
    stack<pair<BTreeNode<T>*, typename vector<pair<T, BTreeNode<T>*>>::iterator>> stackforback;
  888.  
    bool replace = false;
  889.  
     
  890.  
    while (current->NodeFlag == BTreeNode<T>::flag::branch)
  891.  
    {
  892.  
    typename vector<pair<T, BTreeNode<T>*>>::iterator scankey = Upper_Bound(key, current->branch_data->keyptrmap);
  893.  
    if (scankey != current->branch_data->keyptrmap.end())
  894.  
    {
  895.  
    if (scankey->first == key)
  896.  
    {
  897.  
    stackforback.push(make_pair(current, scankey 1));
  898.  
    current = scankey->second;
  899.  
    while (current->NodeFlag == BTreeNode<T>::flag::branch)
  900.  
    {
  901.  
    stackforback.push(make_pair(current, current->branch_data->keyptrmap.begin()));
  902.  
    current = current->branch_data->p;
  903.  
    }
  904.  
    scankey->first = current->leaf_data->front();
  905.  
    current->leaf_data->erase(current->leaf_data->begin());
  906.  
    replace = true;
  907.  
    }
  908.  
    else
  909.  
    {
  910.  
    stackforback.push(make_pair(current, scankey));
  911.  
    if (scankey != current->branch_data->keyptrmap.begin())
  912.  
    {
  913.  
    --scankey;
  914.  
    current = scankey->second;
  915.  
    }
  916.  
    else
  917.  
    {
  918.  
    current = current->branch_data->p;
  919.  
    }
  920.  
    }
  921.  
    }
  922.  
    else
  923.  
    {
  924.  
    stackforback.push(make_pair(current, scankey));
  925.  
    --scankey;
  926.  
    current = scankey->second;
  927.  
    }
  928.  
    }
  929.  
     
  930.  
    if (replace == false)
  931.  
    {
  932.  
    pair<bool, typename vector<T>::iterator> scankey = BinarySearch(*current->leaf_data, current->leaf_data->begin(), current->leaf_data->end() - 1, key);
  933.  
    if (scankey.first == true)
  934.  
    {
  935.  
    current->leaf_data->erase(scankey.second);
  936.  
    }
  937.  
    else
  938.  
    {
  939.  
    cout << "关键字" << key << "不存在,删除失败" << endl;
  940.  
    return { root, false };
  941.  
    }
  942.  
    }
  943.  
     
  944.  
    return { rebalanceAfterDelete(stackforback, current, root), true };
  945.  
    }
  946.  
     
  947.  
    template <typename T>
  948.  
    pair<BTreeNode<T>*, bool> DelKthKey(BTreeNode<T>* root, unsigned long long k, T& be_deleted) //删除B树中第K小的关键字
  949.  
    {
  950.  
    if (root == nullptr || getKeyNum(root) < k)
  951.  
    {
  952.  
    cout << "B树中不存在第" << k << "小元素,删除失败" << endl;
  953.  
    return { root, false };
  954.  
    }
  955.  
    BTreeNode<T>* current = root;
  956.  
    stack<pair<BTreeNode<T>*, typename vector<pair<T, BTreeNode<T>*>>::iterator>> stackforback;
  957.  
     
  958.  
    bool repalce = false;
  959.  
    while (current->NodeFlag == BTreeNode<T>::flag::branch)
  960.  
    {
  961.  
    if (k <= getKeyNum(current->branch_data->p))
  962.  
    {
  963.  
    stackforback.push(make_pair(current, current->branch_data->keyptrmap.begin()));
  964.  
    current = current->branch_data->p;
  965.  
    continue;
  966.  
    }
  967.  
    k -= getKeyNum(current->branch_data->p);
  968.  
     
  969.  
    typename vector<pair<T, BTreeNode<T>*>>::iterator scankey = current->branch_data->keyptrmap.begin();
  970.  
    while (scankey != current->branch_data->keyptrmap.end())
  971.  
    {
  972.  
    if (k == 1)
  973.  
    {
  974.  
    repalce = true;
  975.  
    be_deleted = scankey->first;
  976.  
    stackforback.push(make_pair(current, scankey 1));
  977.  
    current = scankey->second;
  978.  
    while (current->NodeFlag == BTreeNode<T>::flag::branch)
  979.  
    {
  980.  
    stackforback.push(make_pair(current, current->branch_data->keyptrmap.begin()));
  981.  
    current = current->branch_data->p;
  982.  
    }
  983.  
    scankey->first = current->leaf_data->front();
  984.  
    break;
  985.  
    }
  986.  
    else
  987.  
    {
  988.  
    --k;
  989.  
    if (k <= getKeyNum(scankey->second))
  990.  
    {
  991.  
    stackforback.push(make_pair(current, scankey 1));
  992.  
    current = scankey->second;
  993.  
    break;
  994.  
    }
  995.  
    else
  996.  
    {
  997.  
    k -= getKeyNum(scankey->second);
  998.  
    scankey;
  999.  
    }
  1000.  
    }
  1001.  
    }
  1002.  
    }
  1003.  
     
  1004.  
    if (repalce == false)
  1005.  
    be_deleted = *(current->leaf_data->begin() k - 1);
  1006.  
    current->leaf_data->erase(current->leaf_data->begin() k - 1);
  1007.  
    return { rebalanceAfterDelete(stackforback, current, root), true };
  1008.  
    }
  1009.  
     
  1010.  
    int main()
  1011.  
    {
  1012.  
    const int N = 2000;
  1013.  
    vector<int> seq(N);
  1014.  
    for (int i = 0; i < N; i)
  1015.  
    {
  1016.  
    seq[i] = i 1;
  1017.  
    }
  1018.  
    shuffle(seq.begin(), seq.end(), default_random_engine());
  1019.  
     
  1020.  
    BTreeNode<int>* root = nullptr;
  1021.  
    for (vector<int>::const_iterator p = seq.cbegin(); p != seq.cend(); p)
  1022.  
    {
  1023.  
    cout << "插入节点" << *p << endl;
  1024.  
    auto r = InsertBTree(root, *p);
  1025.  
    if (r.second)
  1026.  
    {
  1027.  
    cout << "插入成功" << endl;
  1028.  
    }
  1029.  
    else
  1030.  
    {
  1031.  
    cout << "插入失败" << endl;
  1032.  
    exit(-1);
  1033.  
    }
  1034.  
    cout << endl;
  1035.  
    root = r.first;
  1036.  
    if (isBTree(root) == true)
  1037.  
    {
  1038.  
    cout << "当前树是B树";
  1039.  
    cout << endl;
  1040.  
    }
  1041.  
    else
  1042.  
    {
  1043.  
    cerr << "错误当前树不是B树!" << endl;
  1044.  
    exit(-1);
  1045.  
    }
  1046.  
    }
  1047.  
     
  1048.  
    /*while (root != nullptr)
  1049.  
    {
  1050.  
    unsigned long long _size = getKeyNum(root);
  1051.  
    unsigned long long test_k = rand() % (_size 1);
  1052.  
    if (test_k == 0)
  1053.  
    test_k = 1;
  1054.  
    cout << "删除第" << test_k << "小的元素" << endl;
  1055.  
    int key;
  1056.  
    auto r = DelKthKey(root, test_k, key);
  1057.  
    if (r.second)
  1058.  
    {
  1059.  
    cout << "第" << test_k << "小的元素" << key << "删除成功" << endl;
  1060.  
    }
  1061.  
    else
  1062.  
    {
  1063.  
    cout << "第" << test_k << "小的元素删除失败" << endl;
  1064.  
    exit(-1);
  1065.  
    }
  1066.  
    root = r.first;
  1067.  
    if (root != nullptr)
  1068.  
    {
  1069.  
    if (isBTree(root) == true)
  1070.  
    {
  1071.  
    cout << "当前树是B树";
  1072.  
    cout << endl;
  1073.  
    }
  1074.  
    else
  1075.  
    {
  1076.  
    cerr << "错误当前树不是B树!" << endl;
  1077.  
    exit(-1);
  1078.  
    }
  1079.  
    }
  1080.  
    else
  1081.  
    cout << "NULL";
  1082.  
    }*/
  1083.  
     
  1084.  
    /*for (int i = N; i >= 1; --i)
  1085.  
    {
  1086.  
    cout << "删除第" << i << "小的元素" << endl;
  1087.  
    int key;
  1088.  
    auto r = DelKthKey(root, i, key);
  1089.  
    if (r.second)
  1090.  
    {
  1091.  
    cout << "第" << i << "小的元素" << key << "删除成功" << endl;
  1092.  
    }
  1093.  
    else
  1094.  
    {
  1095.  
    cout << "第" << i << "小的元素删除失败" << endl;
  1096.  
    exit(-1);
  1097.  
    }
  1098.  
    root = r.first;
  1099.  
    if (root != nullptr)
  1100.  
    {
  1101.  
    if (isBTree(root) == true)
  1102.  
    {
  1103.  
    cout << "当前树是B树";
  1104.  
    cout << endl;
  1105.  
    }
  1106.  
    else
  1107.  
    {
  1108.  
    cerr << "错误当前树不是B树!" << endl;
  1109.  
    exit(-1);
  1110.  
    }
  1111.  
    }
  1112.  
    else
  1113.  
    {
  1114.  
    cout << "NULL";
  1115.  
    }
  1116.  
    }*/
  1117.  
     
  1118.  
    cout << endl;
  1119.  
    for (vector<int>::const_iterator p = seq.cbegin(); p != seq.cend(); p)
  1120.  
    {
  1121.  
    cout << "删除节点" << *p << endl;
  1122.  
    auto r = DelBTree(root, *p);
  1123.  
    if (r.second)
  1124.  
    {
  1125.  
    cout << "删除成功" << endl;
  1126.  
    }
  1127.  
    else
  1128.  
    {
  1129.  
    cout << "删除失败" << endl;
  1130.  
    exit(-1);
  1131.  
    }
  1132.  
    cout << endl;
  1133.  
    root = r.first;
  1134.  
    if (root != nullptr)
  1135.  
    {
  1136.  
    if (isBTree(root) == true)
  1137.  
    {
  1138.  
    cout << "当前树是B树";
  1139.  
    cout << endl;
  1140.  
    }
  1141.  
    else
  1142.  
    {
  1143.  
    cerr << "错误当前树不是B树!" << endl;
  1144.  
    exit(-1);
  1145.  
    }
  1146.  
    }
  1147.  
    else
  1148.  
    cout << "NULL";
  1149.  
    cout << endl;
  1150.  
    }
  1151.  
    return 0;
  1152.  
    }
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhggfggc
系列文章
更多 icon
同类精品
更多 icon
继续加载