Question

PYTHON: Conan is writing a module to contain different implementations of trees. After his first tree,...

PYTHON: Conan is writing a module to contain different implementations of trees. After his first tree, the BinaryTreeclass, he wrote test code and is having problems understanding the error. Locate his problem and explain how you would fix it.

class Node(object):
def __init__(self, data=None):
self.data = data
def __str__(self):
return "NODE: " + str(self.data)
  
class Tree(object):
def __init__(self):
self.root_node = None
self.size = 0
  
def __len__(self):
return self.size
  
def add(self, data):
raise NotImplementedError("Add method not implemented.")
  
def inorder_traversal(self):
raise NotImplementedError("inorder_traversal not implemented.")
  

class BinaryTreeNode(Node):
def __init__(self, data=None):
Node.__init__(self, data)
self.left_child = None
self.right_child = None
  
class BinaryTree(Tree):
def __init__(self):
super().__init__()
  
def add(self, data):
if self.root_node == None:
self.root_node = BinaryTreeNode(data)
print(self.root_node)
else:
parent = None
current = self.root_node
while current != None:
if data < current.data:
parent = current
current = current.left_child
elif data > current.data:
parent = current
current = current.right_child
else:
raise ValueError("Value already in Tree.")
if data < parent.data:
parent.left_child = BinaryTreeNode(data)
elif data > parent.data:
parent.right_child = BinaryTreeNode(data)
else:
raise ValueError("Reached insert statement but value is already inserted.")
print(parent.left_child, parent.right_child)
Tree.size += 1
  
  
if __name__ == "__main__":
btree = BinaryTree()
btree.add(5)
btree.add(3)
btree.add(7)
btree.add(4)
btree.add(9)
btree.add(2)
print(len(btree))

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The error occurs at the line Tree.size += 1. Here the code is referencing to an object known as Tree with attribute size. But we haven't instantiated a Tree object in the class. Because the BinaryTree class is a subclass of Tree class the size attribute is a part of BinaryTree class itself, so we have to refer to the size attribute by using self.size. Replacing Tree.size+=1 with self.size+=1 gives us the desired result.

Add a comment
Know the answer?
Add Answer to:
PYTHON: Conan is writing a module to contain different implementations of trees. After his first tree,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python 3: Python 3: Write a LinkedList class that has recursive implementations of the add, display,...

    Python 3: Python 3: Write a LinkedList class that has recursive implementations of the add, display, remove methods. You may use default arguments and/or helper functions. class Node: """ Represents a node in a linked list """ def __init__(self, data): self.data = data self.next = None class LinkedList: """ A linked list implementation of the List ADT """ def __init__(self): self.head = None def add(self, val): """ Adds a node containing val to the linked list """ if self.head is...

  • In Python 3 Write a LinkedList class that has recursive implementations of the display, remove, contains,...

    In Python 3 Write a LinkedList class that has recursive implementations of the display, remove, contains, insert, and normal_list methods. You may use default arguments and/or helper functions. The file must be named: LinkedList.py Here is what I have for my code so far. The methods I need the recursive implementations for will be bolded: class Node: """ Represents a node in a linked list (parent class) """ def __init__(self, data): self.data = data self.next = None class LinkedList: """...

  • Python 3: Write a LinkedList method named contains, that takes a value as a parameter and...

    Python 3: Write a LinkedList method named contains, that takes a value as a parameter and returns True if that value is in the linked list, but returns False otherwise. class Node: """ Represents a node in a linked list """ def __init__(self, data): self.data = data self.next = None class LinkedList: """ A linked list implementation of the List ADT """ def __init__(self): self.head = None def add(self, val): """ Adds a node containing val to the linked list...

  • Python Program Only: Write the function definition only of the "get(self, index)" function. Plug your method...

    Python Program Only: Write the function definition only of the "get(self, index)" function. Plug your method in the code file shared with you and test it in the main to get the element at index 3 of mylist2 . class Node: def __init__(self, e): self.element = e self.next = None class LinkedList: def __init__(self): self.head = None self.tail = None self.size = 0 def addFirst(self, e): newNode = Node(e) newNode.next = self.head self.head = newNode self.size = self.size + 1...

  • python pls and noticed the output added "" One way to represent a binary tree is using the nested list format Consider the following binary tree: 24 72 78 8 51 25 This binary tree could be...

    python pls and noticed the output added "" One way to represent a binary tree is using the nested list format Consider the following binary tree: 24 72 78 8 51 25 This binary tree could be represented using a nested list as follows [55, [24, [8, None, None], [51, [25, None, None], None]], [72, None, [78, None, None ]]] The nested list format always uses a list of length three to represent a binary tree. The first item in...

  • If I wanted to create a method called getNum(n) that should return the data item from...

    If I wanted to create a method called getNum(n) that should return the data item from the node in the nth spot in the list. I am stuck and getting errors when I write my method. I have two files already, the first file linkedlist.py has the code .... ''' class Node(object): def __init__(self, data=None, nextNode=None): self.data = data self.nextNode = nextNode class linkedList(object): def __init__(self, head=None): self.head = head self.size = 0 def insert(self, node): if not self.head: self.head...

  • PYTHON QUESTION... Building a Binary Tree with extended Binary Search Tree and AVL tree. Create a...

    PYTHON QUESTION... Building a Binary Tree with extended Binary Search Tree and AVL tree. Create a class called MyTree with the methods __init__(x), getLeft(), getRight(), getData(), insert(x) and getHeight(). Each child should itself be a MyTree object. The height of a leaf node should be zero. The insert(x) method should return the node that occupies the original node's position in the tree. Create a class called MyBST that extends MyTree. Override the method insert(x) to meet the definitions of a...

  • Python question. i have to start from an empty linked list, using the method addNodeEnd() to...

    Python question. i have to start from an empty linked list, using the method addNodeEnd() to add the nodes containing the values (3*i+5)%17, where i is from 0 to 10. Then print the values of all the nodes in this linked list to the screen. This is the code that i created right here and i need help checking if i made any mistakes thanks! The code is below: class Node: def __init__(self, data): self.data = data self.next = None...

  • Given a binary tree, it is useful to be able to display all of its data values. For this task, define a function called basic_print() which prints out all of the data values in a binary tree. The nat...

    Given a binary tree, it is useful to be able to display all of its data values. For this task, define a function called basic_print() which prints out all of the data values in a binary tree. The natural way to solve this problem is to use recursion. The diagram below illustrates a recursive solution to the problem, which consists of three simple steps: Step1: Print the root node Step 3: Print the right sub-tree 10 Step 2: Print the...

  • class BinaryTree: def __init__(self, data, left=None, right=None): self.__data = data self.__left = left self.__right = right...

    class BinaryTree: def __init__(self, data, left=None, right=None): self.__data = data self.__left = left self.__right = right def insert_left(self, new_data): if self.__left == None: self.__left = BinaryTree(new_data) else: t = BinaryTree(new_data, left=self.__left) self.__left = t def insert_right(self, new_data): if self.__right == None: self.__right = BinaryTree(new_data) else: t = BinaryTree(new_data, right=self.__right) self.__right = t def get_left(self): return self.__left def get_right(self): return self.__right def set_data(self, data): self.__data = data def get_data(self): return self.__data def set_left(self, left): self.__left = left def set_right(self, right): self.__right...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT