Find whether a tree is a binary search tree in Haskell

扶醉桌前 提交于 2019-12-08 16:19:36

问题


  type BSTree a = BinaryTree a

  data BinaryTree a = Null | Node (BinaryTree a) a (BinaryTree a)
                      deriving Show

  flattenTree :: BinaryTree a -> [a]
  flattenTree  tree = case tree of
      Null -> []
      Node left val right -> (flattenTree left) ++ [val] ++ (flattenTree right)

  isBSTree :: (Ord a) => BinaryTree a -> Bool
  isBSTree btree = case btree of
      Null -> False
      tree -> (flattenTree tree) == sort (flattenTree tree)

What I want to do is to write a function to determine whether the given tree is a binary search tree, my method is to group all of the values in a list and import Data.List and then sort the list to find whether they are equal, but it is a little complicated. Can we do this without importing other module?


回答1:


Here's a way to do it without flattening the tree.

From the definition, here,

data BinaryTree a = Null | Node (BinaryTree a) a (BinaryTree a)
     deriving Show

one can see that traversing the tree left to right, ignoring Node and parentheses, gives you an alternating sequence of Nulls and as. That is, between every two values, there is a Null.

My plan is to check that each subtree satisfies suitable requirements: we can refine the requirements at each Node, remembering which values we are between, then test them at each Null. As there is a Null between every in order pair of values, we will have tested that all in order (left-to-right) pairs are non-decreasing.

What is a requirement? It's a loose lower and upper bound on the values in the tree. To express requirements, including those at the leftmost and rightmost ends, we may extend any ordering with Bottom and Top elements, as follows:

data TopBot a = Bot | Val a | Top deriving (Show, Eq, Ord)

Now let us check that a given tree satisfies the requirements of being both in order and between given bounds.

ordBetween :: Ord a => TopBot a -> TopBot a -> BinaryTree a -> Bool
  -- tighten the demanded bounds, left and right of any Node
ordBetween lo hi (Node l x r) = ordBetween lo (Val x) l && ordBetween (Val x) hi r
  -- check that the demanded bounds are in order when we reach Null
ordBetween lo hi Null         = lo <= hi

A binary search tree is a tree that is in order and between Bot and Top.

isBSTree :: Ord a => BinaryTree a -> Bool
isBSTree = ordBetween Bot Top

Computing the actual extremal values in each subtree, bubbling them outwards, gives you more information than you need, and is fiddly in the edge cases where a left or right subtree is empty. Maintaining and checking the requirements, pushing them inwards, is rather more uniform.




回答2:


Here's a hint: make an auxiliary function

isBSTree' :: (Ord a) => BinaryTree a -> BSTResult a

where BSTResult a is defined as

data BSTResult a
   = NotBST             -- not a BST
   | EmptyBST           -- empty tree (hence a BST)
   | NonEmptyBST a a    -- nonempty BST with provided minimum and maximum

You should be able to proceed recursively, exploiting results on subtrees to drive the computation, in particular the minimum and maximum.

For instance, if you have tree = Node left 20 right, with isBSTree' left = NonEmptyBST 1 14 and isBSTree' right = NonEmptyBST 21 45, then isBSTree' tree should be NonEmptyBST 1 45.

In the same case except for tree = Node left 24 right, we should instead have isBSTree' tree = NotBST.

Converting the result to Bool is then trivial.




回答3:


Yes, you do not need to sort the list. You can check if every element is less than or equal to the next element. This is more efficient since we can do this in O(n), whereas evaluating the sorted list completely takes O(n log n).

We thus can check this with:

ordered :: Ord a => [a] -> Bool
ordered [] = True
ordered xa@(_:xs) = and (zipWith (<=) xa xs)

So we can check if the binary tree is a binary search tree with:

isBSTree :: Ord a => BinaryTree a -> Bool
isBSTree = ordered . flattenTree

I think one can claim that Null itself is a binary search tree, since it is an empty tree. This thus means that for every node (there are no nodes) the elements in the left subtree are less than or equal to the value in the node, and the elements in the right subtree are all greater than or equal to the value in the node.




回答4:


We can proceed left-to-right over the tree like this:

isBSTtreeG :: Ord a => BinaryTree a -> Bool
isBSTtreeG t = gopher Nothing [Right t]
    where
    gopher  _   []                        =  True
    gopher  x   (Right Null:ts)           =  gopher x ts
    gopher  x   (Right (Node lt v rt):ts) =  gopher x (Right lt:Left v:Right rt:ts)
    gopher Nothing   (Left v:ts)          =  gopher (Just v) ts
    gopher (Just y)  (Left v:ts)          =  y <= v && gopher (Just v) ts

Inspired by John McCarthy's gopher.

The explicit push-down list can be eliminated with continuation-passing,

isBSTtreeC :: Ord a => BinaryTree a -> Bool
isBSTtreeC t = gopher Nothing t (const True)
    where
    gopher  x   Null           g  =  g x 
    gopher  x   (Node lt v rt) g  =  gopher x lt (\case
                                       Nothing -> gopher (Just v) rt g
                                       Just y  -> y <= v && gopher (Just v) rt g)

Maintaining just one, largest-so-far element, is enough.



来源:https://stackoverflow.com/questions/58304077/find-whether-a-tree-is-a-binary-search-tree-in-haskell

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