RE: Binary Search Tree debugging

2000-04-19 Thread Mark P Jones

Hi Andrew,

| Hey all.. I was wondering if somebody might offer me some assistance in
| trying to debug some code I wrote to check whether a tree is a binary
| search tree.. For some reason it always comes back as false! :(  Thanks
| much!

One of the great things about functional programming is the
opportunities that it brings to explore old problems from new
angles.  The code that you wrote is an attempt to describe a
test for binary search trees as a recursive algorithm, which
is the kind of implementation that you'd expect to see in an
imperative language.  As your experience has shown, this kind
of code can be hard to read, and hard to get right.  I'd
encourage you to try a different approach.  Consider the simple
question: When is a tree a binary search tree?  Answer: When its
leaves are arranged in increasing order.  Translating that idea
directly to Haskell gives:

  isBST :: Ord a = Tree a - Bool
  isBST  = increasing . leaves

where:

  leaves :: Tree a - [a]
  increasing :: Ord a = [a] - Bool

The idea here is that leaves and increasing are general purpose
functions that you can use to enumerate the leaves of a tree
from left to right, and to determine whether the values in a
list are increasing, respectively.  I'll leave the definitions
to you, but it shouldn't be too difficult: a simple two line
recursive definition suffices for leaves, while a mildly cunning
one-liner will get you to increasing (hint: think zipWith (=)).
The whole definition is shorter and (IMHO) simpler than the code
you wrote, while at the same time providing useful functions that
might find applications elsewhere in a larger program.

I hope that you'll find my comments here interesting.  Perhaps I
should explain that I responded to your message because it reminded
me of some similar examples that got me excited when I was first
learning a functional programming language.  I'd been an imperative
programmer for some time before then, but as I looked at those
examples, I began to see new ways of solving all kinds programming
problems.  And, in fact, many of those ideas are still useful to me
today, whatever language I happen to be using.

Enjoy!
Mark





Re: Binary Search Tree debugging

2000-04-18 Thread Marcin 'Qrczak' Kowalczyk

Mon, 17 Apr 2000 14:47:49 -0400 (EDT), Sitzman [EMAIL PROTECTED] pisze:

| otherwise = False

  2
 /  should be a BST too.
1

 checkL = ((treeVal (leftSub  thetree))  (treeVal (thetree)))
 checkR = ((treeVal (rightSub thetree))  (treeVal (thetree)))

It's not enough:

3
   / \
  2   5
 / \
1   4

-- 
 __("Marcin Kowalczyk * [EMAIL PROTECTED] http://qrczak.ids.net.pl/
 \__/  GCS/M d- s+:-- a23 C+++$ UL++$ P+++ L++$ E-
  ^^  W++ N+++ o? K? w(---) O? M- V? PS-- PE++ Y? PGP+ t
QRCZAK  5? X- R tv-- b+++ DI D- G+ e h! r--%++ y-





Re: Binary Search Tree debugging

2000-04-18 Thread Malcolm Wallace

Assuming this isn't a homework exercise...

 1)  If current node is empty then this portion of tree is a BST
 2)  if the left subtree and right subtree's are both not empty then ...

The logical negation of your second clause (which is what is picked
up by the 'otherwise' clause of your code) is that if _either_
subtree is empty, then this portion of the tree is _not_ a BST.
(In other words, it negates the effect of the first clause.)  So all
non-Nil trees will yield False, since everything immediately above a
Nil node is not a BST.  You probably want to change the condition thus:

 isBST thetree
   | isNil thetree = True
   | (isBST (leftSub thetree))  (isBST (rightSub thetree) = checkL  checkR
   | otherwise = False

Regards,
Malcolm