I am having a problem understanding why my code gives me this error
AttributeError: "Tree instance has no attribute 'root'"
I am trying to implement a binary search tree and here is my code.
class Node:
def __init__(self, value):
self.val = value
self.right = None
self.left = None
class Tree:
def __init__(self, val):
root = Node(val)
def main():
tree = Tree(100);
print tree.root.val
if __name__ == "__main__":
main()
I am new to python. Please let me know what is wrong with my code.
You should use self.root
to tell the interpreter that the Tree
class has a instance var named root
.
class Tree:
def __init__(self, val):
self.root = Node(val)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.