English 中文(简体)
Haskell 树木帮助
原标题:Haskell Tree help
  • 时间:2010-08-17 03:21:42
  •  标签:
  • haskell

请允许我回答我的派任问题吗? 我已经做了大部分工作,但我仍对这3个问题 st。

这里的问题是:

审议以下类型: 研究树木和 平衡树木

data STree = Leaf | Node STree Int STree  
data Btree = Tip Int | Branch Btree Btree

他们的建筑受到以下限制:

  • Values of the form Node left n right must have all integers in left being at most n and all integers in right being greater than n.
  • Values of the form Branch left right must have a difference between the numbers of integers in left and right of at most one.

a) • 界定退学职能。 [Int] -> STree , 构造从ger树清单中的搜索树。

b) 界定休养职能:<条码>b [Int] -> BTree , 即从非排他性分类的树种中形成平衡。

c) 采用合并法,界定回收功能collapse BTree -> [Int] , 即 collapse树平衡,以提供分类的ger树清单。

请帮助我!

非常感谢!

问题回答

勿庸置疑,但这里我是部分回事。

stree :: [Int] -> Stree
stree []     = Leaf
stree (x:xs) = let (left, right) = partition (<= x) xs
               in Stree (stree left) x (stree right)

它只是取材于左边的成分,是正确的,而是为每一方重新造树。

假设某种用途是正当的,那么就在B部分而言,Im就预先确定这一工程。

btree :: [Int] -> Btree
btree (x:[]) = Tip x
btree xs     = let len = length xs `div` 2
                   ys = sort xs
               in Branch (btree (take len ys)) (btree (drop len ys))
stree = foldr insert Leaf
  where insert :: Int -> STree -> STree
        insert i (Node l i  r)  | i <= i    = Node (insert i l) i  r
                                | otherwise = Node l i  (insert i r)
        insert i (Leaf) = Node Leaf i Leaf

这不是一个非常有效的解决办法,也不是产生一个非常平衡的树木,而是在哈斯凯尔建立一个数据结构的良好范例。 我们利用<条码>、,为我们处理代谢问题,在树中添加一个元素,把新树推到建设下一个树的功能中。 我们 tree树,直到我们找到一页,取而代之。 Leaf with a Node to hold the given Value.

这些是复读的数据结构。 让我们开始寻找树:

data STree = Leaf | Node STree Int STree

左边的所有价值观都必须低于母,而母子必须低于右上的所有价值观。 您能否写下树[]和树[x]的答复? 你们可以走什么路?

页: 1

stree [] = Leaf
stree [x] = Node Leaf x Leaf
stree ([x,y]) = if x < y then Node Leaf x (Node Leaf y Leaf) else Node (Node Leaf y Leaf) x Leaf

如果建造和修缮工作要迅速进行,则这种修缮。 我们能够排除哪一种常见的次抑郁?

singleton x = Node Leaf x Leaf

这使得生活更加容易:

stree [] = Leaf
stree [x] = singleton x
stree ([x,y]) = if x < y then Node Leaf x (singleton y) else Node (singleton y) x Leaf

但是,如果是这样的话,它就没有解决nes问题。 清单的一个共同点是,在时间上填入。 我们能够为我们工作?

addToSTree :: Int -> STree -> STree
addToStree x Leaf = singleton x
addToStree x (Node left n right) | x < n = ...
                                 | otherwise = ...

您能否填写上述要点? 之后,现在就没有时间就名单的内容发表意见。

BTree也可以同样解决。





相关问题
Euler Problem in Haskell -- Can Someone Spot My Error

I m trying my hand at Euler Problem 4 in Haskell. It asks for that largest palindrome formed by multiplying two three-digit numbers. The problem was simple enough, and I thought my Haskell-fu was up ...

How does foldr work?

Can anybody explain how does foldr work? Take these examples: Prelude> foldr (-) 54 [10, 11] 53 Prelude> foldr (x y -> (x+y)/2) 54 [12, 4, 10, 6] 12.0 I am confused about these executions....

Efficient queue in Haskell

How can I efficiently implement a list data structure where I can have 2 views to the head and end of the list, that always point to a head a tail of a list without expensive calls to reverse. i.e: ...

Problem detecting cyclic numbers in Haskell

I am doing problem 61 at project Euler and came up with the following code (to test the case they give): p3 n = n*(n+1) `div` 2 p4 n = n*n p5 n = n*(3*n -1) `div` 2 p6 n = n*(2*n -1) p7 n = n*(5*n -3)...

Ways to get the middle of a list in Haskell?

I ve just started learning about Functional Programming, using Haskel. I m slowly getting through Erik Meijer s lectures on Channel 9 (I ve watched the first 4 so far) and in the 4th video Erik ...

haskell grouping problem

group :: Ord a => [(a, [b])] -> [(a, [b])] I want to look up all pairs that have the same fst, and merge them, by appending all the list of bs together where they have the same a and discarding ...

Closest equivalent to subprocess.communicate in Haskell

I want to do a popen() / python s subprocess.communicate from Haskell - start a program, give it stdin, and get its stdout/stderr. What s the most direct / Haskellish way to do this?