删除 BST 内的任何节点 - Clojure
我正在学习算法,在课堂上我们被要求创建一个带有结构的 BST,我非常努力地创建一个删除函数,但我创建的函数效率不高并且不起作用。我在谷歌中搜索了类似的内容,但大多数问题都是关于向量而不是记录/结构。如果您有任何建议,我将非常感激。
This is the basic creating of the root and node:
(let [bst (make-bst)]
(bst-empty? bst)
(make-bst-node 10))
(defrecord BST [root])
(defn bst? [bst]
(= (class bst) BST))
(defn make-bst []
(BST. (ref nil)))
(defn bst-empty? [bst]
(nil? @(:root bst)))
(defrecord BSTnode [data left right])
(defn make-bst-node [val]
(BSTnode. val (ref nil) (ref nil)))
(defn bst-insert! [bst val]
(loop [node (:root bst)]
(if (nil? @node)
(dosync
(ref-set node (make-bst-node val)))
(let [data (:data bst)]
(if (< val data)
(recur (:left @node))
(if (val data)
(recur (:right @node))))))))
This is the delete function:
(defn bst-del [bst val]
(if (nil? @(:root bst))
false
(do
(if (= (:data @(:root bst)) val)
(if (nil? (and (:right bst) (:left bst)))
(dosync
(ref-set (:root bst) nil))
(if (not (nil? (:right bst)))
(dosync
(ref-set (:root bst) @(:right bst)))
(if (not (nil? (:left bst)))
(dosync
(ref-set (:root bst) @(:left bst)))
(if (not (nil? (and (:right bst) (:left bst))))
(dosync
(ref-set (:root bst) @(:left bst))
(ref-set (:root bst) (:right bst))) false))))))))
(defn node-del [bst val]
(loop [node @(:root bst)]
(if (nil? node)
false
(if (true? bst-del)
(println "somthing got deleted")
(if (< val (:data node))
(recur @(:left node))
(recur @(:right node)))))))
我尝试在谷歌中搜索,但所有功能或示例都是针对地图和矢量的,而不是我的情况,以及阅读有关该主题的理论材料和来自不同语言的参考文献。
I'm studying algorithms and at the class we were asked to create a BST with structures, I'm trying really hard to create a delete function but the one I created isn't efficient and doesn't work. I searched in google for something similar, but most of the questions are about vectors and not record/structures. If you have any recommendations, I would really appreciate it.
This is the basic creating of the root and node:
(let [bst (make-bst)]
(bst-empty? bst)
(make-bst-node 10))
(defrecord BST [root])
(defn bst? [bst]
(= (class bst) BST))
(defn make-bst []
(BST. (ref nil)))
(defn bst-empty? [bst]
(nil? @(:root bst)))
(defrecord BSTnode [data left right])
(defn make-bst-node [val]
(BSTnode. val (ref nil) (ref nil)))
(defn bst-insert! [bst val]
(loop [node (:root bst)]
(if (nil? @node)
(dosync
(ref-set node (make-bst-node val)))
(let [data (:data bst)]
(if (< val data)
(recur (:left @node))
(if (val data)
(recur (:right @node))))))))
This is the delete function:
(defn bst-del [bst val]
(if (nil? @(:root bst))
false
(do
(if (= (:data @(:root bst)) val)
(if (nil? (and (:right bst) (:left bst)))
(dosync
(ref-set (:root bst) nil))
(if (not (nil? (:right bst)))
(dosync
(ref-set (:root bst) @(:right bst)))
(if (not (nil? (:left bst)))
(dosync
(ref-set (:root bst) @(:left bst)))
(if (not (nil? (and (:right bst) (:left bst))))
(dosync
(ref-set (:root bst) @(:left bst))
(ref-set (:root bst) (:right bst))) false))))))))
(defn node-del [bst val]
(loop [node @(:root bst)]
(if (nil? node)
false
(if (true? bst-del)
(println "somthing got deleted")
(if (< val (:data node))
(recur @(:left node))
(recur @(:right node)))))))
I tried to search in google but all the function or examples were for maps and vectors, not my case, as well as, reading theoretical material about the subject and references from different languages.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的这段代码似乎过于复杂并且难以调试(或者事件理解算法)
我建议实现这个递归删除算法,它与你的可变结构配合得很好:
所以,我将从以下类型开始defs:
我们想要的第一件事是用于树创建/调试的插入+遍历函数。让我们为
Node
实现它们(稍后在Tree
中使用):所以,这似乎工作正常。
接下来我们来实现删除算法。
上述算法中有一个实用函数
minimum
,因此我们从该函数开始:之后删除实现看起来微不足道:
这似乎是工作可变算法。
然后让我们回到
Tree
结构。我将从BST
协议开始,供Node
和Tree
使用:就是这样。现在只需使用它:
this code of yours seems to be overly complicated and hard to debug (or event understand an algorithm)
I would propose implementing this recursive algorithm for deletion, which works quite nice with that mutable structure of yours:
so, i would start with the following type defs:
first thing we want is insertion + traversal functions for tree creating/debug. let's implement them for
Node
(and employ inTree
later):so, this seems to work ok.
Next, we will implement the deletion algorithm.
there's an utility function
minimum
in the aforementioned algorithm, so we start with that one:after that the deletion implementation looks trivial:
this seems to be working mutable algorithm.
Let's then go back to the
Tree
structure. I would start with theBST
protocol, to be used by bothNode
andTree
:and that's it. Now just use it:
课后,我了解了如何删除函数,并在字典二叉树中类似地实现了它。它确实很相似,唯一的区别是“key”值,但逻辑是相同的。
After the class, I understood how to delete a function and I implemented it similarly inside a dictionary binary tree. It is really similar, the only difference is with "key" value, but the logic is the same.