0

how do i actually pass root node to the search method.the trick i used is not working.is there any way to give the search method the root of binary tree. i just want the default value of curr_node equal to the root for my search method. refer to #1 for definition.

class node():
    def __init__(self, data=None):
        self.data = data
        self.leftc = None
        self.rightc = None


class bst():
    def __init__(self):
        self.root = None

    def insert(self, value):
        if(self.root == None):
            self.root = node(value)
        else:
            self._insert(value, self.root)

    def _insert(self, value, curr_node):
        if(value < curr_node.data):
            if(curr_node.leftc == None):
                curr_node.leftc = node(value)
            else:
                self._insert(value, curr_node.leftc)
        elif(value > curr_node.data):
            if (curr_node.rightc == None):
                curr_node.rightc = node(value)
            else:
                self._insert(value, curr_node.rightc)
        else:
            print("you are inserting repeated value")

    def search(self, svalue, curr_node=self.root):  #1
        if(curr_node == None):
            print("item not found")
        elif(curr_node.data == value):
            print("item found")
        elif(svalue > curr_node):
            return search(svalue, curr_node.rightc)
        elif(svalue < curr_node):
            return search(svalue, curr_node.leftc)


bst = bst()
bst.insert(100)
bst.insert(99)
bst.insert(91)
bst.insert(210)
bst.insert(344)
bst.insert(360)
bst.search(344)
  • While *creating* the class It is trying to evaluate `self` which doesn't exits yet. – wwii Jan 08 '20 at 03:13
  • `bst = bst()` - that's probably a bad idea, you are *shadowing* the class name by assigning an instance to that name. – wwii Jan 08 '20 at 03:15

0 Answers0