# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3.7.4 ('base')
#     language: python
#     name: python3
# ---

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


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


btree = BinarySearchTree()
print(btree.root)


# insert node
class BinarySearchTree():
    def __init__(self):
        self.root = None

    def insert(self, value):
        node = Node(value)
        if self.root == None:
            self.root = node
            return True

        curNode = self.root
        nextNode = None
        while True:
            if value < curNode.value:
                # go left
                nextNode = curNode.left
                if nextNode == None:
                    curNode.left = node
                    return True
            elif value > curNode.value:
                # go right
                nextNode = curNode.right
                if nextNode == None:
                    curNode.right = node
                    return True
            else:
                return False

            # move forward to next node if have not return
            curNode = nextNode        
  


# +
btree = BinarySearchTree()
btree.insert(10)
btree.insert(1)
btree.insert(3)
btree.insert(12)

btree.root.value
# -

btree.root.left.value

btree.root.right.value





StarcoderdataJupyterScriptsDedupFiltered