[Tutor] One other thing·...

Bruce Sass bsass@freenet.edmonton.ab.ca
Sat, 12 May 2001 12:09:02 -0600 (MDT)


On Fri, 11 May 2001, VanL wrote:

> One of the other things I was considering was extending this tree class
> to implement different types of trees.
>
> (Start imaginary python session)
>
>  >>>  from Tree import BinaryTree, AVLTree
>  >>> a1 = AVLTree('data1')
>  >>> a2 = AVLTree('data2')
>  >>> b1 = BinaryTree('root')
>  >>> b2 = BinaryTree('left')
>  >>> a1.treetype()
> 'AVL'
>  >>> b1.treetype()
> 'binary'
>  >>> a1 = a1 + a2
>  >>> b
>
> That last operation would test that the two types were compatible, and,
> where applicable, perform the tree merge.  All this stuff would be hard
> to do, if not impossible, if I can't figure out what I've got when I am
> passed in an object.

Hmmm, could it be that you, "can't see the forest for the trees"
(sorry).

The two ends of the spectrum are efficiency and portability;
at the portability extreme...

Does it really matter how a tree is
implemented, or is it enough to be able to get the data out.  I mean,
why do you need to know the internals of the tree (what class it is)
to do...

	newtree.insert(tree.inorder())

Ok, you probably want to test that "tree" has an "inorder" method (or
preorder, or postorder, or tranverse, whatever, especially if you are
inserting into a basic binary tree ;), but it would be best to test
for the method that does what you want -- just in case some class you
have never heard of comes along (maybe FasterAVLTree).

from an efficiency p.o.v...
You start off with "from Tree import BinaryTree, AVLTree", want to
operate on trees as a whole (+'em), and expect a treetype method, that
tells me you are the coder and can implement them anyway you want.  So
why not have a base node class that defines a data/key attribute,
common to all types of trees, you can then check:

	isinstance(tree, BasicNodeClass)

'cause:
>>> class A:
...    pass
...
>>> class B(A):
...    pass
...
>>> b = B()
>>> isinstance(b, A)
1

...and if you made it this far, you can access the data directly
(since you wrote BasicNodeClass).  If you are going to break the ADT,
you may as well go all the way.

Either way (efficient or portable), it is not necessary to know
whether you have an AVLTree or a BinaryTree instance, just how to get
at the contents of the tree.


- Bruce