简体   繁体   中英

Splay Tree Deletion

I'm having trouble conceptualising the process of deletion from a splay tree. Given this intial, tree, I want to delete the node 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. However, this is not the case as shown on this visualiser here. https://www.cs.usfca.edu/~galles/visualization/SplayTree.html

在此处输入图像描述

The visualizer replaced 78 by its in order predecessor (70) instead and splayed that node. (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).

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

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. To delete a node in a splay tree do the following:

  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.
  2. The new root of the recombined tree will come from A. Splay the largest (rightmost) node of A tree to its root.
  3. Because A's new root has the greatest key in A, it has no right child. Set the right child of A's new root to B.
  4. A is the new combined tree.

This is what the visualization athttps://www.cs.usfca.edu/%7Egalles/visualization/SplayTree.html did.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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