Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
15 changes: 14 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -888,9 +888,22 @@
"uuid": "6b7f7b1f-c388-4f4c-b924-84535cc5e1a0",
"slug": "hexadecimal",
"deprecated": true
},
{
"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"
]
}
],
"foregone": [

]
}
52 changes: 52 additions & 0 deletions exercises/binary-search-tree/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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
8 changes: 8 additions & 0 deletions exercises/binary-search-tree/binary_search_tree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class TreeNode(object):
def __init__(self, value):
pass


class BinarySearchTree(object):
def __init__(self):
pass
35 changes: 35 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,35 @@
import unittest

from binary_search_tree import BinarySearchTree


class BinarySearchTreeTests(unittest.TestCase):
bst = BinarySearchTree()

def test_add_integer_number1(self):
self.assertTrue(self.bst.add(7))

def test_add_integer_number2(self):
self.assertTrue(self.bst.add(1))

def test_add_integer_number3(self):
self.assertTrue(self.bst.add(6))

def test_add_integer_number4(self):
self.assertTrue(self.bst.add(5))

def test_add_float_number(self):
self.assertTrue(self.bst.add(7.5))

def test_search_valid_number1(self):
self.assertEqual(self.bst.search(7).value, 7)

def test_search_valid_number2(self):
self.assertEqual(self.bst.search(7.5).value, 7.5)

def test_search_valid_number3(self):
self.assertEqual(self.bst.search(1).value, 1)


if __name__ == '__main__':
unittest.main()
69 changes: 69 additions & 0 deletions exercises/binary-search-tree/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
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)
inserted = True
return inserted
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
return inserted
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
return inserted

def search(self, searched_number):
cur_node = self.root
found = False
while not found:
if(cur_node is None):
raise ValueError("Element not found")
elif(searched_number < cur_node.value):
cur_node = cur_node.left_node
elif(searched_number > cur_node.value):
cur_node = cur_node.right_node
elif(searched_number == cur_node.value):
return cur_node

def print_inorder(self, node):
if(node is not None):
self.print_inorder(node.left_node)
print(node.value)
self.print_inorder(node.right_node)

def print_preorder(self, node):
if(node is not None):
print(node.value)
self.print_preorder(node.left_node)
self.print_preorder(node.right_node)

def print_postorder(self, node):
if(node is not None):
self.print_preorder(node.left_node)
self.print_preorder(node.right_node)
print(node.value)