最新公告
  • 欢迎您光临码农资源网,本站秉承服务宗旨 履行“站长”责任,销售只是起点 服务永无止境!加入我们
  • Python实现B树插入算法的原理图解

    b树是高度平衡的二叉搜索树,进行插入操作,要先获取插入节点的位置,遵循节点比左子树大,比右子树小,在需要时拆分节点。

    一图看懂B树插入操作原理

    B树插入操作原理图解 Python实现B树插入算法

    B树插入算法

    <code>BreeInsertion(T, k)r  root[T]if n[r] = 2t - 1<br/>    s = AllocateNode()<br/>    root[T] = s<br/>    leaf[s] = FALSE<br/>    n[s] <- 0<br/>    c1[s] <- r<br/>    BtreeSplitChild(s, 1, r)<br/>    BtreeInsertNonFull(s, k)else BtreeInsertNonFull(r, k)BtreeInsertNonFull(x, k)i = n[x]if leaf[x]<br/>    while i ≥ 1 and k < keyi[x]<br/>        keyi+1 [x] = keyi[x]<br/>        i = i - 1<br/>    keyi+1[x] = k<br/>    n[x] = n[x] + 1else while i ≥ 1 and k < keyi[x]<br/>        i = i - 1<br/>    i = i + 1<br/>    if n[ci[x]] == 2t - 1<br/>        BtreeSplitChild(x, i, ci[x])<br/>        if k &rt; keyi[x]<br/>            i = i + 1<br/>    BtreeInsertNonFull(ci[x], k)BtreeSplitChild(x, i)BtreeSplitChild(x, i, y)z = AllocateNode()leaf[z] = leaf[y]n[z] = t - 1for j = 1 to t - 1<br/>    keyj[z] = keyj+t[y]if not leaf [y]<br/>    for j = 1 to t<br/>        cj[z] = cj + t[y]n[y] = t - 1for j = n[x] + 1 to i + 1<br/>    cj+1[x] = cj[x]ci+1[x] = zfor j = n[x] to i<br/>    keyj+1[x] = keyj[x]keyi[x] = keyt[y]n[x] = n[x] + 1</code>

    用Python实现B树插入算法

    <code>class BTreeNode:<br/>    def __init__(self, leaf=False):<br/>        self.leaf = leaf<br/>        self.keys = []<br/>        self.child = []<br/> <br/>class BTree:<br/>    def __init__(self, t):<br/>        self.root = BTreeNode(True)<br/>        self.t = t<br/> <br/>    def insert(self, k):<br/>        root = self.root<br/>        if len(root.keys) == (2 * self.t) - 1:<br/>            temp = BTreeNode()<br/>            self.root = temp<br/>            temp.child.insert(0, root)<br/>            self.split_child(temp, 0)<br/>            self.insert_non_full(temp, k)<br/>        else:<br/>            self.insert_non_full(root, k)<br/> <br/>    def insert_non_full(self, x, k):<br/>        i = len(x.keys) - 1<br/>        if x.leaf:<br/>            x.keys.append((None, None))<br/>            while i >= 0 and k[0] < x.keys[i][0]:<br/>                x.keys[i + 1] = x.keys[i]<br/>                i -= 1<br/>            x.keys[i + 1] = k<br/>        else:<br/>            while i >= 0 and k[0] < x.keys[i][0]:<br/>                i -= 1<br/>            i += 1<br/>            if len(x.child[i].keys) == (2 * self.t) - 1:<br/>                self.split_child(x, i)<br/>                if k[0] > x.keys[i][0]:<br/>                    i += 1<br/>            self.insert_non_full(x.child[i], k)<br/> <br/>    def split_child(self, x, i):<br/>        t = self.t<br/>        y = x.child[i]<br/>        z = BTreeNode(y.leaf)<br/>        x.child.insert(i + 1, z)<br/>        x.keys.insert(i, y.keys[t - 1])<br/>        z.keys = y.keys[t: (2 * t) - 1]<br/>        y.keys = y.keys[0: t - 1]<br/>        if not y.leaf:<br/>            z.child = y.child[t: 2 * t]<br/>            y.child = y.child[0: t - 1]<br/> <br/>    def print_tree(self, x, l=0):<br/>        print("Level ", l, " ", len(x.keys), end=":")<br/>        for i in x.keys:<br/>            print(i, end=" ")<br/>        print()<br/>        l += 1<br/>        if len(x.child) > 0:<br/>            for i in x.child:<br/>                self.print_tree(i, l)<br/> <br/>def main():<br/>    B = BTree(3)<br/> <br/>    for i in range(10):<br/>        B.insert((i, 2 * i))<br/> <br/>    B.print_tree(B.root)<br/> <br/>if __name__ == '__main__':<br/>    main()</code>
    想要了解更多内容,请持续关注码农资源网,一起探索发现编程世界的无限可能!
    本站部分资源来源于网络,仅限用于学习和研究目的,请勿用于其他用途。
    如有侵权请发送邮件至1943759704@qq.com删除

    码农资源网 » Python实现B树插入算法的原理图解
    • 7会员总数(位)
    • 25846资源总数(个)
    • 0本周发布(个)
    • 0 今日发布(个)
    • 293稳定运行(天)

    提供最优质的资源集合

    立即查看 了解详情