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)
Related
I have used a class for a program I have been working on. Unfortunately, I am struggling to return the value instead of the object memory location.
class Node:
def __init__(self, data):
self.data = data
class BinaryTree:
root = None
#classmethod
def InsertNode(cls, data):
newNode = Node(data)
if cls.root == None:
cls.root = newNode
return cls.root
else:
queue = []
queue.append(cls.root)
print(BinaryTree().InsertNode(4))
would return -> <__main__.Node object at 0x000001A8478CAE60>
I am a new user of Python annotation. I am trying to achieve dependency by annotation.
class Trie():
def __init__(self):
self.root = self.Node()
self.count = 0
class Node():
def __init__(self, value=None):
self.value = value
self.children = {}
def add(self, key, value):
path = key.split('.')
node = self.root
for token in path:
if token not in node.children:
node.children[token] = self.Node()
node = node.children[token]
node.value = value
self.count += 1
def get(self, key, default_value=None):
path = key.split('.')
node = self.root
for token in path:
if token not in node.children:
return default_value
node = node.children[token]
self.count -= 1
return node.value
_REGISTRY = Trie()
def register_cls(identifier):
def add_class(cls):
_REGISTRY.add(identifier, cls)
return cls
return add_class
def find_cls(identifier, default_value=None):
return _REGISTRY.get(identifier, default_value)
This is the code to register the class and find the class by class name.
But I do not know how to register a class from another file.
#register_cls('resnet18')
class ResNet18:
def __init__(self):
self.name = 'resnet18'
def get_name(self):
return self.name
if __name__ == '__main__':
name_18 = 'resnet18'
model = find_cls(name_18)
print(model().get_name())
assert model().get_name() == name_18
I can only use this function as this test. find_cls() and #register_cls() in the same file. But this code can store the path of the file, how can I use this function to read the class cross different files.
I'm trying to understand python and OOP along with data structures
I'm now looking at the implementation of a binary search tree
here is the class for the node structure
class Node():
def __init__(self, data):
self.data = data
self.leftChild = None
self.rightChild = None
the developer of this code has created the insert feature in Node class and in another class called tree
here is what it looks like in the node class :
def insert(self, data):
if self.data == data:
return False
elif data < self.data:
if self.leftChild:
return self.leftChild.insert(data)
else:
self.leftChild = Node(data)
return True
else:
if self.rightChild:
return self.rightChild.insert(data)
else:
self.rightChild = Node(data)
return True
however, he created a function with the same name in the tree class which looks like this
class Tree():
def __init__(self):
self.root = None
def insert(self, data):
if self.root:
return self.root.insert(data)
else:
self.root = Node(data)
return True
I do have some questions at this point, why are there 2 functions with the same name? and when I try to execute one of them without the other on this code it shows an error
if __name__ == '__main__':
tree = Tree()
tree.insert(10)
why does he made instance for tree not for node ?
can someone please explain those concepts for me, thanks!
When you creating tree object it doesn't creating any node. It's just defining a root variable with None. Node is creating when you call tree.insert() method.
insert() method in Tree class is checking if root is None it creating a new Node. Otherwise it calling the insert method in Node class.
Why does he made instance for tree not for node ?
Because insert() method in Node class only handles the case where tree has atleast one node or tree is not empty. That's why he creating the instance of Tree.
Code with insert() method in Tree class:
class Node():
def __init__(self, data):
self.data = data
self.leftChild = None
self.rightChild = None
class Tree():
def __init__(self):
self.root = None
def insert(self, root, data):
if root is None:
self.root = Node(data)
elif root.data < data:
if root.rightChild is None:
root.rightChild = Node(data)
else:
self.insert(root.rightChild, data)
else:
if root.leftChild is None:
root.leftChild = Node(data)
else:
self.insert(root.leftChild, data)
What is the difference between these constructors for a LinkedList, why is one better or worse than the other?
class Node:
def __init__(self, data):
self.data = data
self.next = None
class Singly_Linked_List:
def __init__(self):
self.head = None
versus:
class Singly_Linked_List:
def __init__(self):
self.head = Node(0)
Does this affect how the SLL or DLL would be implemented for the addAtIndex, removeAtIndex functions for example?
This is what I have so far. I am rather confused, what am I missing here? Is it standard to do Node.left = Node(5)? I think confusing. Should I have a addLeft function in the Tree class that does that for me? Little confused on standard tree implementation.
class Node:
def __init__(self,val):
self.val = val
self.right = None
self.left = None
class Tree:
def __init__(self):
self.root = None
t = Tree()
t.root = (Node(3))
print t.root.val