December 12th, 2004, 11:42 AM
Help on implementing a binary search tree please
I have a homework assignment requiring the implementation of a binary search tree.
I have a problem with the "putta" method, which is supposed to add new nodes to the tree. It adds one node and then just says that the data of all new nodes are identical to the single existing nodes data.
I have low understanding of the "return" parameter, but I believe it has something to do with it.
The data I am trying to add is three letter words.
Please help me!!!
ny = node
ny.value = newvalue
ny.left = None
ny.right = None
if p is None:
p = ny
p.left = putta(p.left,newvalue)
p.right = putta(p.right,newvalue)
print "Ordet: ", newvalue,", fanns redan."
root = None
def put(self, newvalue):
self.root = putta(self.root,newvalue)
value = None
left = None
right = None
December 12th, 2004, 03:46 PM
It's actually a simple typo that's at fault; you forgot the parentheses in the declaration of ny:
If you make this change, and add the following test lines after the two test insertions, it should demonstrate the tree behavior quite nicely.
December 12th, 2004, 04:57 PM
Thank you so much Schol-R-LEA! You really saved the day, now everything works fine!
Today you brought some peace into this world!!!
Happy Holidays /Cooper