Skip to content

dibs: implement binary-search-tree #388

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
6 changes: 6 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,12 @@
"difficulty": 1,
"topics": [
]
},
{
"slug": "binary-search-tree",
"difficulty": 1,
"topics": [
]
}
],
"deprecated": [
Expand Down
25 changes: 25 additions & 0 deletions exercises/binary-search-tree/binary_search_tree_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import unittest

from binary_search_tree import BST


class BSTTests(unittest.TestCase):
def setUp(self):
self.tree = BST(4)

def test_insert(self):
self.tree.insert(2)
self.tree.insert(1)
self.assertTrue(self.tree.search(2))
self.assertTrue(self.tree.search(1))

def test_search(self):
self.tree.insert(2)
self.tree.insert(1)
self.assertTrue(self.tree.search(2))
self.assertTrue(self.tree.search(1))
self.assertFalse(self.tree.search(9))
self.assertFalse(self.tree.search(20))

if __name__ == '__main__':
unittest.main()
51 changes: 51 additions & 0 deletions exercises/binary-search-tree/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class Node(object):
def __init__(self, value):
self.value = value
self.left = None
self.right = None


class BST(object):
def __init__(self, root):
self.root = Node(root)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requiring an initial value for the BST seems like a very odd design choice...


def insert(self, new_val):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You already wrote this function below. self.pre_insert(self.root, new_val)

if new_val > self.root.value:
if self.root.right is None:
self.root.right = Node(new_val)
else:
self.pre_insert(self.root.right, new_val)
elif new_val < self.root.value:
if self.root.left is None:
self.root.left = Node(new_val)
else:
self.pre_insert(self.root.left, new_val)

def pre_insert(self, node, val):
if node.right is None and val > node.value:
node.right = Node(val)
elif node.left is None and val < node.value:
node.left = Node(val)
elif val > node.value:
self.pre_insert(node.right, val)
else:
self.pre_insert(node.left, val)

def search(self, find_val):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This search function is probably a little easier to read if you do something like this:

cur = self.root
while cur:
    if find_val == cur.value:
        return True
    cur = cur.left if find_val < cur.value else cur.right
return False

current = self.root
if find_val == current.value:
return True
else:
while current.left is not None or current.right is not None:
if find_val == current.value:
return True
elif find_val > current.value and current.right is not None:
current = current.right
elif find_val < current.value and current.left is not None:
current = current.left
else:
break
if current.left is None and current.right is None:
if find_val == current.value:
return True
return False