ATTN: Phlip

Phlip phlip_cpp at my-deja.com
Mon Feb 19 01:15:27 CET 2001


Proclaimed Steve Mak from the mountaintops:

> Hey Phlip
> 
>     Yes, it's for class....the dictionary object would make things ALOT
> easier, but they want us to use a bst, so I can't help it! Can you help
> me?

Okay. Try this:

class Node:
        def __init__ (self, left, right, label):
                (self.left, self.right, self.label) = (left, right, label)
                return

a = Node (None, None, "a")
g = Node (None, None, "g")
p = Node (None, None, "p")
z = Node (None, None, "z")
f = Node (a, g, "f")
u = Node (p, z, "u")
o = Node (f, u, "o")

There. That's a tree balanced by alpha order. But it can't insert or 
rebalance by itself. But adding an in-order traversal method would be 
trivial.

The best thing now would be to write a Unit Test that expresses what your 
interface should do.

        http://c2.com/cgi/wiki?CodeUnitTestFirst

Then you run it over and over again until there are no error messages or 
assertion exceptions:

def testTree ():
        t = Tree ()
        assert (t.count () == 0)
        leaf = t.addLeaf ("leaf")
        assert (t.count () == 1)
        got = t.getLeaf ("leaf")
        assert (got == leaf)
        ...

Doing it test-first like this keeps the hugest projects locked on track.

-- 
  Phlip                          phlip_cpp at my-deja.com
============== http://phlip.webjump.com ==============
  --  Have a :-) day  -



More information about the Python-list mailing list