简体   繁体   English

从 B 树中删除叶节点

[英]Deletion of the leaf node from B-tree

What are the rules of deleting nodes from a leaf nodes in a B tree. B树的叶子节点删除节点的规则是什么? I have given an example below.我在下面举了一个例子。 I need to delete keys, J,K,U from leaf nodes.我需要从叶节点中删除键 J、K、U。 the 't' of the B tree is 3. so the minimum number of keys in a node should be 2. B 树的 't' 是 3。所以节点中的最小键数应该是 2。

J can be deleted without any issue. J 可以毫无问题地删除。

But when J is deleted, the remaining would be K,L.但是当J被删除时,剩下的就是K,L。 Next when deleting K, since the node contains 2 nodes, K cannot be deleted directly.接下来删除K时,由于节点包含2个节点,不能直接删除K。

Since its sibling node, which is N,O also contains its minimum nodes what should I perform here?由于它的兄弟节点 N,O 也包含它的最小节点,我应该在这里执行什么? Is it a merge?是合并吗?

How can I delete K and also U.如何删除 K 和 U。

Please help.请帮忙。

在此处输入图片说明

I referred this book Introduction-to-algorithms - 3rd-edition by Thomas H Cormen and he explained it very well.我参考了Thomas H Cormen的《算法导论-第 3 版》这本书,他解释得很好。 Here are the 3 steps that include all the cases.Hope it helps.以下是包含所有案例的 3 个步骤。希望对您有所帮助。

  1. If the key k is in node x and x is a leaf, delete the key k from x.如果键 k 在节点 x 中并且 x 是叶子,则从 x 中删除键 k。

  2. If the key k is in node x and x is an internal node, do the following:如果键 k 在节点 x 中并且 x 是内部节点,请执行以下操作:

    a. a. If the child y that precedes k in node x has at least t keys, then find the predecessor k' of k in the subtree rooted at y.如果节点 x 中 k 之前的子节点 y 至少有 t 个键,则在以 y 为根的子树中找到 k 的前驱 k'。 Recursively delete k0,and replace k by k' in x.递归删除k0,并用x中的k'替换k。 (We can find k0 and delete it in a single downward pass.) (我们可以找到 k0 并在一次向下传递中将其删除。)

    b.If y has fewer than t keys, then, symmetrically, examine the child z that follows k in node x.如果 y 的键少于 t 个,则对称地检查节点 x 中 k 之后的子节点 z。 If z has at least t keys, then find the successor k' of k in the subtree rooted at z.如果 z 至少有 t 个键,则在以 z 为根的子树中找到 k 的后继 k'。 Recursively delete k' ,and replace k by k' in x.递归删除k',并用x中的k'替换k。 (We can find k' and delete it in a single downward pass.) (我们可以找到 k' 并在一次向下传递中将其删除。)

    c. C. Otherwise, if both y and z have only t-1 keys, merge k and all of z into y, so that x loses both k and the pointer to z, and y now contains 2t-1 keys.否则,如果 y 和 z 都只有 t-1 个键,则将 k 和所有 z 合并到 y 中,这样 x 就会丢失 k 和指向 z 的指针,而 y 现在包含 2t-1 个键。 Then free z and recursively delete k from y.然后释放 z 并从 y 中递归删除 k。

在此处输入图片说明

  1. If the key k is not present in internal node x, determine the root x.ci of the appropriate subtree that must contain k, if k is in the tree at all.如果内部节点 x 中不存在键 k,则确定必须包含 k 的适当子树的根 x.ci,如果 k 根本不在树中。 If x.ci has only t-1 keys, execute step 3a or 3b as necessary to guarantee that we descend to a node containing at least t keys.如果 x.ci 只有 t-1 个键,则根据需要执行步骤 3a 或 3b,以确保我们下降到至少包含 t 个键的节点。 Then finish by recursing on the appropriate child of x.然后通过对 x 的适当子级进行递归来完成。

    a. a. If x.ci has only t-1 keys but has an immediate sibling with at least t keys, give x.ci an extra key by moving a key from x down into x.ci, moving a key from x.ci's immediate left or right sibling up into x, and moving the appropriate child pointer from the sibling into x.ci.如果 x.ci 只有 t-1 个键,但有至少 t 个键的直接兄弟,通过将键从 x 向下移动到 x.ci,将键从 x.ci 的紧邻左侧移动或右兄弟向上移动到 x,并将适当的子指针从兄弟移动到 x.ci。

    b.If x.ci and both of x.ci's immediate siblings have t-1 keys, merge x.ci with one sibling, which involves moving a key from x down into the new merged node to become the median key for that node.如果 x.ci 和 x.ci 的两个直接兄弟都有 t-1 个键,则将 x.ci 与一个兄弟合并,这涉及将一个键从 x 向下移动到新的合并节点,以成为该节点的中间键。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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