-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Changes from all commits
8e6c2ed
6e46058
6b3aed6
e2f7b44
a5070bc
dd46a3d
dcae81e
26351f5
3752370
2bda09a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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() |
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) | ||
|
||
def insert(self, new_val): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You already wrote this function below. |
||
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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
|
||
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 |
There was a problem hiding this comment.
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...