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

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

    def insert(self,current,value):
        if current == None:
            current = Node(value)
        elif value < current.value:
            self.insert(current.left,value)
        elif value > current.value:
            self.insert(current.right,value)

tree = binary_search_tree()
for i in (12,5,18,3,4,6):
    tree.insert(tree.root,i)

The insert method for my binary search tree is not working can someone help me out.

(Post is mostly code, please add more details.)

aL_eX
  • 1,453
  • 2
  • 15
  • 30
Xuan
  • 101
  • 9
  • Don't you need to create your tree before inserting values on it? – davidbuzatto Dec 07 '17 at 22:46
  • @davidbuzatto I did create a a tree, sorry i did not include it in the code block above – Xuan Dec 07 '17 at 22:56
  • You might want to consider how `self.root` can ever change when you never assign it a value other than `None`. And if `self.root` is always `None`, you'll never be able to add items. – Jim Mischel Dec 07 '17 at 23:40
  • @Dukeling I figured that might be the problem, any idea how should i approach this differently? – Xuan Dec 08 '17 at 08:10
  • `current = Node(value)` will change only the local variable and leave the thing you're passing in unchanged. Related: [How do I pass a variable by reference?](https://stackoverflow.com/q/986006) Or you can just return the thing you want to change it to and put the assignment outside of the function (e.g. `current.left = self.insert(current.left,value)`). – Bernhard Barker Dec 08 '17 at 08:11

0 Answers0