How do I delete from a binary search tree in Lisp

南笙酒味 提交于 2020-01-15 18:32:28

问题


How can I delete a node from a BST?

I need an algorithm to do that in Dr. Scheme.


回答1:


You basically toss the BST you have now, and create a new one sans the element.

You can do this by recursively descending the tree. If your item is less than the root datum, create a BST whose root and greater-than branch is copied from what you have now, but whose less-than branch is the result from a recursive call.

It's very similar to how you add a node, but when you get to the one you were searching for, merge the two BSTs below it and return the result. There are surely existing questions about how to do that already.




回答2:


Assuming your binary search tree uses straight forward cons cells with content only at the leaves, and assuming you are working on a homework assignment: You can use set-car! or set-cdr! to change the content of a cons cell.



来源:https://stackoverflow.com/questions/4374530/how-do-i-delete-from-a-binary-search-tree-in-lisp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!