简体   繁体   English

Splay树删除

[英]Splay Tree Deletion

I'm having trouble conceptualising the process of deletion from a splay tree.我无法概念化从 splay 树中删除的过程。 Given this intial, tree, I want to delete the node 78.给定这个初始树,我想删除节点 78。

展开树初始

Based on the information from my course (derived from Goodrich, Tamassia and Goldwasser), the deleted node in a BST should be replaced by the next node reached by performing an in-order traversal from the node which should be 91. This node should then be splayed to the top of the tree.根据我的课程中的信息(来自 Goodrich、Tamassia 和 Goldwasser),BST 中删除的节点应该替换为通过执行从应该是 91 的节点开始的顺序遍历到达的下一个节点。然后这个节点应该展开到树的顶端。 However, this is not the case as shown on this visualiser here.但是,此处的可视化工具显示的情况并非如此。 https://www.cs.usfca.edu/~galles/visualization/SplayTree.html https://www.cs.usfca.edu/~galles/visualization/SplayTree.html

在此处输入图像描述

The visualizer replaced 78 by its in order predecessor (70) instead and splayed that node.可视化器将 78 替换为其前任 (70) 并展开该节点。 (The in order successor, ie, the next key in sorted order is 83, not 91.) In general, splay trees are wonderfully malleable and as long as you approximately halve the length of the path you just descended while making every other path at most a little bit longer, you're doing it right from an asymptotic performance standpoint (your professor may have different ideas, however). (有序后继,即排序后的下一个键是 83,而不是 91。)一般来说,张开的树具有极好的延展性,只要您将刚刚下降的路径的长度大约减半,同时使其他路径在大多数时间长一点,从渐近性能的角度来看,您是正确的(但是,您的教授可能有不同的想法)。

Your textbook description:你的教科书描述:

the deleted node in a BST should be replaced by the next node reached by performing an in-order traversal from the node which should be 91 BST 中删除的节点应该被从应该是 91 的节点执行顺序遍历到达的下一个节点替换

That description applies to unbalanced BST (binary search trees) but does not apply to most of the various kinds of balanced binary trees, and also does not apply to Splay Trees.该描述适用于非平衡 BST(二叉搜索树),但不适用于大多数各种平衡二叉树,也不适用于 Splay 树。 To delete a node in a splay tree do the following:要删除 splay 树中的节点,请执行以下操作:

  1. Splay the node to be deleted to the root and dispose of it.将要删除的节点展开到根节点并销毁。 This leaves two trees, call the left tree A and the right tree B.这留下了两棵树,称为左树 A 和右树 B。
  2. The new root of the recombined tree will come from A. Splay the largest (rightmost) node of A tree to its root.重组树的新根将来自 A。将 A 树的最大(最右边)节点展开到它的根。
  3. Because A's new root has the greatest key in A, it has no right child.因为 A 的新根拥有 A 中最大的键,所以它没有右孩子。 Set the right child of A's new root to B.将 A 的新根的右孩子设置为 B。
  4. A is the new combined tree. A是新的组合树。

This is what the visualization athttps://www.cs.usfca.edu/%7Egalles/visualization/SplayTree.html did.这就是https://www.cs.usfca.edu/%7Egalles/visualization/SplayTree.html上的可视化所做的。

You said in comments to the other answer:您在对另一个答案的评论中说:

So in practice, the node that you choose to replace the deleted node doesn't really matter, ie affect performance etc.所以在实践中,您选择的节点来替换已删除的节点并不重要,即影响性能等。

In the typical splay tree deletion algorithm the node to replace will be the predecessor or successor node, in key order.在典型的 splay 树删除算法中,要替换的节点将是前驱节点或后继节点,按密钥顺序排列。

The rule of thumb is to always splay whenever a specific node is accessed.经验法则是在访问特定节点时始终张开。 Find the node to delete, then splay it to the root.找到要删除的节点,然后展开到根节点。 Find its predecessor, then splay it to the root.找到它的前身,然后展开它到根。 There are variations where you can splay less aggressively, too.还有一些变化,您也可以不那么激进地张开。

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

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