Skip to content
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

Implement exercise binary-search-tree #773

Merged
merged 31 commits into from
Nov 13, 2017
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
cae11fe
Implement exercise binart-search-tree
rodrigocam Oct 8, 2017
d8256db
Add TreeNode class and binary search tree add method
rodrigocam Oct 8, 2017
5b65169
Add preorder, postorder and inorder traversal
rodrigocam Oct 8, 2017
06b303f
Add search method
rodrigocam Oct 8, 2017
b39e8d8
Add some tests
rodrigocam Oct 8, 2017
a5350dc
Add more testes, examle file and a readme
rodrigocam Oct 8, 2017
5604b08
Fix flake8 diretives
rodrigocam Oct 8, 2017
1013ec8
Add a new entry to config.json
rodrigocam Oct 8, 2017
5883681
Change new key place
rodrigocam Oct 8, 2017
ab2fa3e
Change search paramater name and add early if condition to exit if th…
rodrigocam Oct 10, 2017
1425ffc
Change exercise position in config.json
rodrigocam Oct 27, 2017
cb4e3bf
Merge branch 'master' into master
cmccandless Oct 27, 2017
fed194a
Merge branch 'master' into master
cmccandless Oct 27, 2017
9990ec2
Merge branch 'master' into master
cmccandless Oct 27, 2017
7192861
fix config.json syntax error
cmccandless Oct 27, 2017
3fe384e
Add blank line
rodrigocam Nov 3, 2017
62c5933
Add title at readme
rodrigocam Nov 3, 2017
49f6701
Add necessary info at readme
rodrigocam Nov 3, 2017
10627f2
Add skeletons for add and search methods
rodrigocam Nov 3, 2017
f4d558f
Change assert used by tests
rodrigocam Nov 3, 2017
5663d14
Add list method
rodrigocam Nov 3, 2017
56f971b
Change tests to include the new list method
rodrigocam Nov 3, 2017
fa1de56
Merge branch 'master' into master
cmccandless Nov 3, 2017
805557a
Fix flake8 guidelines
rodrigocam Nov 9, 2017
3b916a5
Change search and add method
rodrigocam Nov 9, 2017
1332fdc
Merge branch 'master' of https://github.com/rodrigocam/python
rodrigocam Nov 9, 2017
cd94b1f
Change variable name in search method and delete .cache directory
rodrigocam Nov 13, 2017
18c399f
Remove spaces on blank line
rodrigocam Nov 13, 2017
a01cb5a
Change parameter of search method
rodrigocam Nov 13, 2017
c054b29
Remove print methods
rodrigocam Nov 13, 2017
d43a2ba
Merge branch 'master' into master
Nov 13, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1128,6 +1128,20 @@
"trees"
]
},
{
"uuid": "6f196341-0ffc-9780-a7ca-1f817508247161cbcd9",
"slug": "binary-search-tree",
"core": false,
"unlocked_by": null,
"difficulty": 4,
"topics":[
"recursion",
"classes",
"trees",
"searching",
"object_oriented_programming"
]
},
{
"uuid": "e7351e8e-d3ff-4621-b818-cd55cf05bffd",
"slug": "accumulate",
Expand Down
71 changes: 71 additions & 0 deletions exercises/binary-search-tree/README.md
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.
Copy link
Contributor

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 Tree

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done


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
Copy link
Contributor

Choose a reason for hiding this comment

The 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 configlet.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.
14 changes: 14 additions & 0 deletions exercises/binary-search-tree/binary_search_tree.py
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
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you also include skeletons for the add and search methods here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done


def add(self, value):
pass

def search(self, value):
pass
58 changes: 58 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,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()
61 changes: 61 additions & 0 deletions exercises/binary-search-tree/example.py
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

Choose a reason for hiding this comment

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

Also add a check for if(self.root is None): return false which does an early exit if the tree hasn't been constructed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, checked!

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)