[Tutor] btree problems

Bruce Sass Bruce Sass <bsass@freenet.edmonton.ab.ca>
Mon, 13 Mar 2000 14:13:32 -0700 (MST)

Hello All,

I read through the Tutorial, scanned the Library Reference and Language
Reference, then decided to jump into it... I grabbed "Programming with
Data Structure (Pascal version)" by Robert L. Kruse off my book shelf
and started typing in the basic binary tree code.  Here is a snippet of
what I came up with:

----- tree-test2.py -----
class datapkg:
    def __init__(self):
        self.key = None
        self.data = None

class node:
    def __init__(self):
        self.entry = datapkg()
        self.left = None
        self.right = None

class btree:
    def __init__(self):
        self.tree = None

    def insert(self, newnode):
        self._InsertTree(self.tree, newnode)

    def _InsertTree(self, branch, newnode):
        if branch == None:
            branch = newnode
        elif newnode.entry.key < branch.entry.key:
            self._InsertTree(branch.left, newnode)
            self._InsertTree(branch.right, newnode)

tree = btree()
print "Created a tree: tree =", tree

n = node()
n.entry.key = "first"
n.entry.data = ['some', 'data']
print "Created a node: n =", n
print "  n.entry =", n.entry
print "  n.entry.key =", n.entry.key
print "  n.entry.data =", n.entry.data

print "Inserted the node into the tree:"
print "  tree =", tree
print "  tree.entry =", tree.entry
print "  tree.entry.key = ", tree.entry.key
----- end tree-test2.py -----

This is what it generates:
----- tree-test2.output.txt -----
Created a tree: tree = <__main__.btree instance at 83d7ce8>
Created a node: n = <__main__.node instance at 83b3d58>
  n.entry = <__main__.datapkg instance at 83dda48>
  n.entry.key = first
  n.entry.data = ['some', 'data']
Inserted the node into the tree:
  tree = <__main__.btree instance at 83d7ce8>
  tree.entry =
Traceback (innermost last):
  File "/home/bsass/usr/src/python/tree-test2.py", line 43, in ?
    print "  tree.entry =", tree.entry
AttributeError: entry
----- end trees-test2.output.txt -----

What am I doing wrong?