This is what I've got so far but it is not working:
class Node:
rChild,lChild,data = None,None,None
def __init__(self,key):
self.rChild = None
self.lChild = None
self.data = key
class Tree:
root,size = None,0
def __init__(self):
self.root = None
self.size = 0
def insert(self,node,someNumber):
if node is None:
node = Node(someNumber)
else:
if node.data > someNumber:
self.insert(node.rchild,someNumber)
else:
self.insert(node.rchild, someNumber)
return
def main():
t = Tree()
t.root = Node(4)
t.root.rchild = Node(5)
print t.root.data #this works
print t.root.rchild.data #this works too
t = Tree()
t.insert(t.root,4)
t.insert(t.root,5)
print t.root.data #this fails
print t.root.rchild.data #this fails too
if __name__ == '__main__':
main()
value
, thekey
is what is usually used to look up that value. – LeanneleanorWhat I am interested in knowing is not an implementation, but why my code is not working
. But your code is not working because your implementation is not correct. Not much sense withsearch
in your main. As a kind suggestion, perhaps just write first a simple tree creation and then some pre- and post-order traversals of it. Thanks – Gripeif
andelse
in the insert method are the same, if you decide to modify rather than rewrite your code make sure you give that a look. – Leanneleanor