[英]Functional Programming Binary Search Tree Homework
So I am having to write an insert function for a binary tree to make it a binary search tree, but I'm having some trouble. 因此,我必须为二叉树编写一个插入函数以使其成为二叉搜索树,但是我遇到了一些麻烦。 Everything is functions, so I understand that there is no concept of a state. 一切都是功能,所以我知道没有状态的概念。 Therefore, I need to recursively create the tree over and over when inserting. 因此,在插入时,我需要一遍又一遍地递归地创建树。 I'm having trouble wrapping my head around this idea. 我很难把这个想法包扎起来。
treenode -> procedure(val, left, right) procedure(some) if some then -(some, 1) then right else left else val
This allows me to create nodes and access the value, the left sub-tree and right sub-tree like so (0 stands for an empty tree): 这使我可以创建节点并访问值,如下所示(左子树和右子树)(0代表空树):
.treenode(4, 0, 0)
to create a more complex tree, I can do: 要创建更复杂的树,我可以这样做:
.treenode(4, .treenode(3, 0, 0), .treenode(6, .treenode(2, 0, 0), 0))
I have gotten as far as inserting into an empty tree: 我已经深入到插入一棵空树中了:
insert -> procedure(root, value) if empty(root) then .treenode(value, 0, 0) else insert_recursive(root, .treenode(value, 0, 0)
This is where I cannot figure out how to insert into a tree like this. 这是我无法弄清楚如何插入这样的树的地方。 There is not concept of a state in this language so I need to somehow recursively append the new node with the value onto the current tree. 用这种语言没有状态的概念,因此我需要以某种方式将带有值的新节点递归地附加到当前树上。 If someone could give me a hint,, I would really appreciate it. 如果有人可以给我一个提示,我将不胜感激。 The way I'm supposed to call this is: 我应该这样称呼它:
tree = empty
tree = insert(tree, 4)
tree = insert(tree, 6)
....
and so on
I've never seen this grammar before, so bear with me if I got some of the syntax wrong. 我以前从未见过这种语法,所以如果我语法错误,请多多包涵。 Hopefully the code demonstrates what needs to be done - 希望代码演示了需要做的事情-
The bullet points correspond to the numbered comments below - 要点与以下编号的注释相对应-
insert -> procedure(root, value)
if empty(root) then #1
.treenode(value, 0, 0)
else if value < root.value #2
.treenode (root.value, insert(root.left, value), root.right)
else if value > root.value #3
.treenode (root.value, root.left, insert(root.right, value))
else #4
root
StackOverflow allows us to run JavaScript snippets directly in answer posts. StackOverflow允许我们直接在答案文章中运行JavaScript代码段。 Here's a functioning snippet that allows you to see this concept in action - 这是一个可正常运行的代码段,可让您实际了解这一概念-
const empty = {} const treenode = (value, left = empty, right = empty) => ({ value, left, right }) const insert = (t, value) => t === empty ? treenode (value, empty, empty) : value < t.value ? treenode (t.value, insert (t.left, value), t.right) : value > t.value ? treenode (t.value, t.left, insert (t.right, value)) : t const print = (t, pre = '', child = '') => t === empty ? pre + '∅\\n' : print ( t.right , child + '┌── ' , child + '. ' ) + pre + String (t.value) + '\\n' + print ( t.left , child + '└── ' , child + '. ' ) let tree = empty tree = insert (tree, 4) tree = insert (tree, 6) tree = insert (tree, 9) tree = insert (tree, 3) tree = insert (tree, 5) tree = insert (tree, 1) console.log (print (tree))
The program prints the constructed tree
. 该程序将打印出所构造的tree
。 The ∅
symbol represents empty - ∅
符号代表空 -
. . . ┌── ∅
. . ┌── 11
. . . └── ∅
. ┌── 9
. . └── ∅
┌── 6
. . ┌── ∅
. └── 5
. . └── ∅
4
. ┌── ∅
└── 3
. . ┌── ∅
. └── 1
. . └── ∅
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.