Python Forum
Help, not understanding how classes work...
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help, not understanding how classes work...
#1
Hallo,

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
Reply
#2
Look at the definition of the _print_tree function. It takes four parameters - self, and three others. When you call it, it takes the instance as a parameter without a need to pass it every time when calling the method. So you have to provide the other three parameters. You pass two of them,
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Understanding Python classes PythonNewbee 3 1,214 Nov-10-2022, 11:07 PM
Last Post: deanhystad
  I need help understanding a program structure using classes CompleteNewb 21 6,111 Feb-24-2022, 07:15 PM
Last Post: deanhystad
  Understanding Python super() for classes OmegaRed94 1 1,852 Jun-09-2021, 09:02 AM
Last Post: buran
  Using classes? Can I just use classes to structure code? muteboy 5 5,096 Nov-01-2017, 04:20 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020