Jan-20-2018, 05:46 PM
Hallo,
I have following code:
it asks me to add one more argument to _print_tree inner function. I concluded I need to use parameter value to get value on the node.
I DO NOT KNOW how to fix this. Honestly, I don't know how inner funcions work, but that is beside the point.
ALL I NEED is to fix this code. It requires one, two words to add to be working again but I don't know which ones.
I am getting reeeeealy frustrated here so let me plz be straight: I am not looking for funny hints like "yeah, it is a python code" or "there is problem on line 42" OK!?!?! :D If you don't have answer to my question, don't write anything at all, plz. It is great way to make sure I won't come to your house and burn it down from sheer anger I am experiencing here :D :D :D
I have following code:
class node: def __init__(self,value=None): self.value=value self.left_child=None self.right_child=None self.parent=None # pointer to parent node in tree self.height = None def set_parent(self, parent_node): self.parent = parent_node self.height = parent_node.height + 1 class binary_search_tree: def __init__(self): self.root=None def insert(self,value): if self.root==None: self.root=node(value) self.root.height = 0 else: self._insert(value,self.root) def _insert(self,value,cur_node): if value<cur_node.value: if cur_node.left_child==None: cur_node.left_child=node(value) cur_node.left_child.set_parent(cur_node) # set parent else: self._insert(value,cur_node.left_child) elif value>cur_node.value: if cur_node.right_child==None: cur_node.right_child=node(value) cur_node.right_child.set_parent(cur_node) # set parent else: self._insert(value,cur_node.right_child) else: print "Value already in tree!" def print_tree(self, max_height = float('Inf')): if self.root!=None: self._print_tree(self.root, max_height) def _print_tree(self, cur_node, value, max_height,accumulated_sum = 0 ): nodeValue = cur_node.value if cur_node!=None and cur_node.height < max_height: self._print_tree(cur_node.left_child, max_height) print str(cur_node.value ) self._print_tree(cur_node.right_child, max_height) accumulated_sum += nodeValue print "sum of values on all nodes with max high %s is %d" % (max_height, accumulated_sum) return (accumulated_sum + nodeValue) def height(self): if self.root!=None: return self._height(self.root,0) else: return 0 def _height(self,cur_node,cur_height): if cur_node==None: return cur_height left_height=self._height(cur_node.left_child,cur_height+1) right_height=self._height(cur_node.right_child,cur_height+1) return max(left_height,right_height) def find(self,value): if self.root!=None: return self._find(value,self.root) else: return None def _find(self,value,cur_node): if value==cur_node.value: return cur_node elif value<cur_node.value and cur_node.left_child!=None: return self._find(value,cur_node.left_child) elif value>cur_node.value and cur_node.right_child!=None: return self._find(value,cur_node.right_child) def delete_value(self,value): return self.delete_node(self.find(value)) def delete_node(self,node): # returns the node with min value in tree rooted at input node def min_value_node(n): current=n while current.left_child!=None: current=current.left_child return current # returns the number of children for the specified node def num_children(n): num_children=0 if n.left_child!=None: num_children+=1 if n.right_child!=None: num_children+=1 return num_children # get the parent of the node to be deleted node_parent=node.parent # get the number of children of the node to be deleted node_children=num_children(node) # break operation into different cases based on the # structure of the tree & node to be deleted # CASE 1 (node has no children) if node_children==0: # remove reference to the node from the parent if node_parent.left_child==node: node_parent.left_child=None else: node_parent.right_child=None # CASE 2 (node has a single child) if node_children==1: # get the single child node if node.left_child!=None: child=node.left_child else: child=node.right_child # replace the node to be deleted with its child if node_parent.left_child==node: node_parent.left_child=child else: node_parent.right_child=child # correct the parent pointer in node child.set_parent(node_parent) # CASE 3 (node has two children) if node_children==2: # get the inorder successor of the deleted node successor=min_value_node(node.right_child) # copy the inorder successor's value to the node formerly # holding the value we wished to delete node.value=successor.value # delete the inorder successor now that it's value was # copied into the other node self.delete_node(successor) def search(self,value): if self.root!=None: return self._search(value,self.root) else: return False def _search(self,value,cur_node): if value==cur_node.value: return True elif value<cur_node.value and cur_node.left_child!=None: return self._search(value,cur_node.left_child) elif value>cur_node.value and cur_node.right_child!=None: return self._search(value,cur_node.right_child) return False tree = binary_search_tree() for i in range(0, 10): tree.insert(i) tree.print_tree(2)when I click run I get message:
File "C:\Users\ntb1\Desktop\Codding\gggg.py", line 171, in <module> tree.print_tree(2) File "C:\Users\ntb1\Desktop\Codding\gggg.py", line 42, in print_tree self._print_tree(self.root, max_height) TypeError: _print_tree() takes at least 4 arguments (3 given)This is a code that generates binary tree based on height you give it and on line 42 it counts values of nodes based on height you ask on line 171.
it asks me to add one more argument to _print_tree inner function. I concluded I need to use parameter value to get value on the node.
I DO NOT KNOW how to fix this. Honestly, I don't know how inner funcions work, but that is beside the point.
ALL I NEED is to fix this code. It requires one, two words to add to be working again but I don't know which ones.
I am getting reeeeealy frustrated here so let me plz be straight: I am not looking for funny hints like "yeah, it is a python code" or "there is problem on line 42" OK!?!?! :D If you don't have answer to my question, don't write anything at all, plz. It is great way to make sure I won't come to your house and burn it down from sheer anger I am experiencing here :D :D :D