0

For the code below I wrote for Binary Search Tree, I get the error delete_node(curr,value) NameError: global name 'delete_node' is not defined. I am wondering why this will be the case for static method which is called from the instance method delete(self,value) . Why does it not work as a static method? I am calling it from a instance method 'delete' so does it does not require access to instance members explicitly.

class Node:
    def __init__(self,value=None):
        self.value = value
        self.left = None
        self.right = None

class BST:
    def __init__(self,value=None):
        self.root = Node(value)
    @staticmethod
    def delete_node(node,value):
       if node is None:
           return node
       if value > node.value:
           node.right = delete_node(node.right,value)
       elif value < node.value:
           node.left = delete_node(node.left,value)
       else:
           if node.left is None and node.right is None:
               node = None
               return None
           elif node.left is None:
               temp = node.right
               node = None
               return temp
           elif node.right is None:
               temp = node.left
               node = None
               return temp
           else:
            #min_value = get_min(node.right)
               node.value = get_min(node.right)
               node.right = delete_node(node.right,node.value)

       def delete(self,value):
           if self.root == None:
           return
           curr = self.root
           delete_node(curr,value)
def get_min(node):
    curr = node
    while curr.left:
        curr = curr.left
    return curr.value

if __name__ == '__main__':
    tree = BST(5)
    tree.delete(5)
vkaul11
  • 4,098
  • 12
  • 47
  • 79

2 Answers2

0

The design of your class seems confusing.

In python, when we define a staticmethod that means the class name is just something like namespace, you can not access any member of the class in the staticmethod.

If you want static methods in C#, try classmethod.

I recommend the popular question for classmethod vs staticmethod.

In the specific question, change your code like this:

class BST:
    ...
    @staticmethod
    def delete_node(node,value):
    ...
               node.right = BST.delete_node(node.right,node.value)

    def delete(self,value):
        ...
        BST.delete_node(curr,value)
PaleNeutron
  • 2,543
  • 4
  • 25
  • 43
  • What about helper functions that you want to call from inside instance methods ? – vkaul11 May 27 '19 at 02:18
  • But why does it not work as a staticfunction? I am calling it from a instance method delete. – vkaul11 May 27 '19 at 02:21
  • @vkaul11, there are two different ways, replace all `delete_node` to `BST.delete_node`, or move the current `delete_node` function out of the class. – PaleNeutron May 27 '19 at 02:22
0

As others have eluded to, in order to call methods on a class object you have to specify the thing you're calling it on.

So where you use delete_node, you can use BST.delete_node since it's static. The design is a bit odd given a BST would normally be done as an object encapsulated completely, but this is your immediate problem.

If it's an instance method, then you would call self.delete_node

Vaughan Hilts
  • 2,839
  • 1
  • 20
  • 39
  • I wanted to use it as a helper function delete_node. How exactly do I accomplish that? Just make it an external method? – vkaul11 May 27 '19 at 02:12
  • Yes, you can make it an external method / a free function if you wish to accomplish this. – Vaughan Hilts May 27 '19 at 02:18
  • But why does it not work as a staticfunction? I am calling it from a instance method delete. – vkaul11 May 27 '19 at 02:21
  • A static function belongs to a class. You can think of it as a helper function. For example, if you had the class "helpers" you could add things like "round" to it. It's a way of namespacing it. It lets you group things together instead of having 1000s of floating around functions – Vaughan Hilts May 27 '19 at 02:23
  • Yes but why does delete_node work as a helper function here ? I just put it inside the class as it is only useful within the class. – vkaul11 May 27 '19 at 02:35