简体   繁体   English

函数式编程二进制搜索树作业

[英]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 - 希望代码演示了需要做的事情-

  1. if the tree is empty, there is no value to compare against, create a singleton node with the value. 如果树为空,则没有可比较的值,请使用该值创建一个单例节点。 This is the part you already finished. 这是您已经完成的部分。
  2. otherwise, the tree is not empty, therefore we have a value to compare against. 否则,树不是空的,因此我们有一个要比较的值。 If the value to insert is less than the root's value, create a new node consisting of the root's value, insert the value into the root's left branch, and leave the root's right branch untouched 如果要插入的值小于根的值,则创建一个由根的值组成的新节点,将该值插入根的左分支,并保持根的右分支不变
  3. if the value is greater than the root's value, do the same as above, but insert the new value in the root's right branch, instead of the left 如果该值大于根的值,请执行与上述相同的操作,但是将新值插入根的分支(而不是分支)中
  4. the value is neither less-than nor greater-than the root's value, therefore it is equal to the root's value and there is nothing left to insert. 该值既不小于也不大于根的值,因此它等于根的值,没有任何可插入的内容。 In this case, return the unmodified root 在这种情况下,返回未修改的根

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.

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