简体   繁体   English

在修改后的二进制搜索树上搜索,插入和删除

[英]Search, Insert, and Deletion on modified binary search tree

Say that each node in a binary search tree x keeps x.successor instead of x.parent. 假设二叉搜索树x中的每个节点都保留x.successor而不是x.parent。 Describe Search, Insert, and Delete algorithms using pseudo code that operate in O(h) where h is the height of the tree. 使用在O(h)中操作的伪代码描述搜索,插入和删除算法,其中h是树的高度。 So far, I believe that the simple binary search tree algorithm for search still applies. 到目前为止,我相信用于搜索的简单二进制搜索树算法仍然适用。

TREE-SEARCH(x, k)
if x == NIL or k == x.key 
    return x
if k < x.key
    return TREE-SEARCH(x.left, k)
else return TREE-SEARCH(x.right, k)

This algorithm should be unaffected by the modification and clearly runs in O(h) time. 该算法应不受修改的影响,并且显然应在O(h)时间内运行。 However, for the Insert and Delete algorithms the modification changes the straightforward way to do these. 但是,对于插入和删除算法,修改会更改执行这些操作的直接方法。 For example, here is the algorithm for Insertion using a normal binary search tree, T. 例如,这是使用普通二进制搜索树T进行插入的算法。

TREE-INSERT(T,z)
y = NIL
x = T.root
while x =/= NIL
    y = x 
    if z.key < x.key
        x = x.left
    else x = x.right
z.p = y
if y == NIL
    T.root = z    //Tree T was empty
elseif z.key < y.key
    y.left = z
else y.right = z

Clearly we cannot use zp because of our modification to the binary search tree. 显然,由于对二进制搜索树的修改,我们无法使用zp。 It has been suggested that a subroutine that finds the parent be implemented, but I fail to see how this can be done. 已经建议实现一个可以找到父对象的子例程,但是我看不到如何做到这一点。

Searching will be unaffected 搜索不会受到影响

Sketch of an answer 答案草图

While inserting, we want the successor of the newly inserted node. 插入时,我们需要新插入的节点的后继节点。 If you know the algorithm for the successor in a normal binary search tree, you'd know that at the leaf node, the successor is the first ancestor whose left child is an ancestor of the node. 如果您知道普通二叉搜索树中后继的算法,那么您会知道在叶节点处,后继是第一个祖先,其左子节点是该节点的祖先。 So while doing normal insert the last node whose left child we follow will be the successor of the node. 因此,在正常插入时,我们跟随其左孩子的最后一个节点将是该节点的后继节点。 On this insert path the last node where you follow the right child will be the one whose successor is our current node. 在此插入路径上,您跟随正确子节点的最后一个节点将是其后继节点是我们当前节点的节点。

For deletion, if in x.successor you are storing the pointer to the successor node, it becomes simple, just replace the node-to-be-deleted's key with the successor's key and the node.successor pointer with the successors pointer. 对于删除,如果在x.successor中存储指向后继节点的指针,则变得很简单,只需将要删除的节点的密钥替换为后继的密钥,将node.successor指针替换为后继的指针即可。 If you're just storing the key, you need to update the node that had this node as the successor. 如果仅存储密钥,则需要更新以该节点为后继节点的节点。 the candidates are, if it has a left child, the rightmost node in this subtree. 如果有子节点,则候选者是此子树中最右边的节点。 If it doesn't, search for the node and the last node where you move to the right child will be the required node. 如果不是,请搜索该节点,然后将您移至右子节点的最后一个节点作为必需节点。

I don't know how to draw diagrams here, it's simpler with the diagrams. 我不知道如何在这里绘制图,使用图更简单。

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

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