Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.5k
Implement exercise binary-search-tree#773
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
cae11fed8256db5b6516906b303fb39e8d8a5350dc5604b081013ec85883681ab2fa3e1425ffccb4e3bffed194a9990ec271928613fe384e62c593349f670110627f2f4d558f5663d1456f971bfa1de56805557a3b916a51332fdccd94b1f18c399fa01cb5ac054b29d43a2baFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| # Binary Search Tree | ||
| Insert and search for numbers in a binary tree. | ||
| When we need to represent sorted data, an array does not make a good | ||
| data structure. | ||
| Say we have the array `[1, 3, 4, 5]`, and we add 2 to it so it becomes | ||
| `[1, 3, 4, 5, 2]` now we must sort the entire array again! We can | ||
| improve on this by realizing that we only need to make space for the new | ||
| item `[1, nil, 3, 4, 5]`, and then adding the item in the space we | ||
| added. But this still requires us to shift many elements down by one. | ||
| Binary Search Trees, however, can operate on sorted data much more | ||
| efficiently. | ||
| A binary search tree consists of a series of connected nodes. Each node | ||
| contains a piece of data (e.g. the number 3), a variable named `left`, | ||
| and a variable named `right`. The `left` and `right` variables point at | ||
| `nil`, or other nodes. Since these other nodes in turn have other nodes | ||
| beneath them, we say that the left and right variables are pointing at | ||
| subtrees. All data in the left subtree is less than or equal to the | ||
| current node's data, and all data in the right subtree is greater than | ||
| the current node's data. | ||
| For example, if we had a node containing the data 4, and we added the | ||
| data 2, our tree would look like this: | ||
| 4 | ||
| / | ||
| 2 | ||
| If we then added 6, it would look like this: | ||
| 4 | ||
| / \ | ||
| 2 6 | ||
| If we then added 3, it would look like this | ||
| 4 | ||
| / \ | ||
| 2 6 | ||
| \ | ||
| 3 | ||
| And if we then added 1, 5, and 7, it would look like this | ||
| 4 | ||
| / \ | ||
| / \ | ||
| 2 6 | ||
| / \ / \ | ||
| 1 3 5 7 | ||
Contributor 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. There should also be some other stuff that goes here. Check another exercise and copy it over, or regenerate this README with the ContributorAuthor 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. Done | ||
| ## Submitting Exercises | ||
| Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/<exerciseName>` directory. | ||
| For example, if you're submitting `bob.py` for the Bob exercise, the submit command would be something like `exercism submit <path_to_exercism_dir>/python/bob/bob.py`. | ||
| For more detailed information about running tests, code style and linting, | ||
| please see the [help page](http://exercism.io/languages/python). | ||
| ## Source | ||
| Wikipedia [https://en.wikipedia.org/wiki/Binary_search_tree](https://en.wikipedia.org/wiki/Binary_search_tree) | ||
| ## Submitting Incomplete Solutions | ||
| It's possible to submit an incomplete solution so you can see how others have completed the exercise. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| class TreeNode(object): | ||
| def __init__(self, value): | ||
| self.value = value | ||
| class BinarySearchTree(object): | ||
| def __init__(self): | ||
| pass | ||
Contributor 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. Can you also include skeletons for the ContributorAuthor 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. Done | ||
| def add(self, value): | ||
| pass | ||
| def search(self, value): | ||
| pass | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import unittest | ||
| from binary_search_tree import BinarySearchTree | ||
| class BinarySearchTreeTests(unittest.TestCase): | ||
| def test_add_integer_numbers(self): | ||
| bst = BinarySearchTree() | ||
| bst.add(1) | ||
| bst.add(8) | ||
| bst.add(3) | ||
| bst.add(5) | ||
| bst.add(2) | ||
| self.assertEqual(list(bst.list()), [1, 2, 3, 5, 8]) | ||
| def test_add_float_numbers(self): | ||
| bst = BinarySearchTree() | ||
| bst.add(7.5) | ||
| bst.add(5.3) | ||
| bst.add(5.5) | ||
| bst.add(6.0) | ||
| bst.add(7.7) | ||
| self.assertEqual(list(bst.list()), [5.3, 5.5, 6.0, 7.5, 7.7]) | ||
| def test_add_mixed_numbers(self): | ||
| bst = BinarySearchTree() | ||
| bst.add(1) | ||
| bst.add(8) | ||
| bst.add(7.5) | ||
| bst.add(5.3) | ||
| self.assertEqual(list(bst.list()), [1, 5.3, 7.5, 8]) | ||
| def test_add_duplicated_numbers(self): | ||
| bst = BinarySearchTree() | ||
| bst.add(1) | ||
| bst.add(1) | ||
| bst.add(7.5) | ||
| bst.add(5.3) | ||
| self.assertEqual(list(bst.list()), [1, 1, 5.3, 7.5]) | ||
| def test_search_existent_numbers(self): | ||
| bst = BinarySearchTree() | ||
| bst.add(1) | ||
| bst.add(7.5) | ||
| self.assertEqual(bst.search(1).value, 1) | ||
| self.assertEqual(bst.search(7.5).value, 7.5) | ||
| def test_search_nonexistent_numbers(self): | ||
| bst = BinarySearchTree() | ||
| bst.add(1) | ||
| bst.add(7.5) | ||
| self.assertIs(bst.search(6), None) | ||
| self.assertIs(bst.search(8.8), None) | ||
| if __name__ == '__main__': | ||
| unittest.main() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| from collections import deque | ||
| class TreeNode(object): | ||
| def __init__(self, value): | ||
| self.value = value | ||
| self.left_node = None | ||
| self.right_node = None | ||
| def __str__(self): | ||
| return str(self.value) | ||
| class BinarySearchTree(object): | ||
| def __init__(self): | ||
| self.root = None | ||
| def add(self, value): | ||
| if(self.root is None): | ||
| self.root = TreeNode(value) | ||
| else: | ||
| inserted = False | ||
| cur_node = self.root | ||
| while not inserted: | ||
| if(value <= cur_node.value): | ||
| if(cur_node.left_node): | ||
| cur_node = cur_node.left_node | ||
| else: | ||
| cur_node.left_node = TreeNode(value) | ||
| inserted = True | ||
| elif(value > cur_node.value): | ||
| if(cur_node.right_node): | ||
| cur_node = cur_node.right_node | ||
| else: | ||
| cur_node.right_node = TreeNode(value) | ||
| inserted = True | ||
| def search(self, value): | ||
| cur_node = self.root | ||
| found = False | ||
| while not found: | ||
| if(cur_node is None): | ||
| return None | ||
| elif(value < cur_node.value): | ||
| cur_node = cur_node.left_node | ||
| elif(value > cur_node.value): | ||
| cur_node = cur_node.right_node | ||
| elif(value == cur_node.value): | ||
| return cur_node | ||
| def list(self): | ||
| elements = deque() | ||
| self.trav_inorder(self.root, elements) | ||
| return elements | ||
| def trav_inorder(self, node, elements): | ||
| if(node is not None): | ||
| self.trav_inorder(node.left_node, elements) | ||
| elements.append(node.value) | ||
| self.trav_inorder(node.right_node, elements) |
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.
This needs the title of the exercise as a heading:
# Binary Search TreeThere 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.
Done