Skip to content

Commit 5784390

Browse files
committed
new exercises and solutions
1 parent d796185 commit 5784390

7 files changed

Lines changed: 656 additions & 2 deletions

File tree

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
## Development - Advanced, exercise 41
2+
3+
### Text
4+
A binary search tree is a binary tree data structure where each node may have at most two children and the value of each node is greater than (or equal to) all the values in the respective node's left subtree and less than (or equal to) the ones in its right subtree. It can be built, recursively following an approach which recalls the binary search strategy, starting from a list of ordered items (e.g. a list of integers), where each item become a node of a tree.
5+
6+
<img src="img/bst.png" alt="Example of a binary search tree" style="max-height:100px;" />
7+
8+
As a reminder, the binary search strategy first checks if the middle item of a list is equal to item to search for and, in case this is not true, it continues to analyse the part of the list that either preceeds or follows the middle item if it is greater than or less than the item to search.
9+
10+
Write a recursive algorithm in Python – `def create_bst(ordered_list, parent)` – which takes in input an ordered list of integers ordered_list and a parent node `parent`, and returns the root node of the binary search tree created from the input list. In the first call, the function is run passing `None` as input of the second parameter `parent`, e.g.
11+
12+
```
13+
create_bst([0, 1, 1, 2, 3, 5, 8, 13], None)
14+
```
15+
16+
and returns the binary search tree (i.e. its root node) shown in the example above.
17+
18+
19+
### Solution
20+
```python
21+
from anytree import Node, RenderTree
22+
23+
# Test case for the function
24+
def test_create_bst(ordered_list, parent, expected):
25+
result = create_bst(ordered_list, parent)
26+
27+
if str(RenderTree(result)) == str(RenderTree(expected)):
28+
return True
29+
else:
30+
return False
31+
32+
33+
# Code of the function
34+
def create_bst(ordered_list, parent):
35+
cur_len = len(ordered_list)
36+
37+
if cur_len == 1:
38+
return Node(ordered_list[0], parent)
39+
elif cur_len > 1:
40+
mid = cur_len // 2
41+
42+
r = Node(ordered_list[mid], parent)
43+
44+
create_bst(ordered_list[:mid], r)
45+
create_bst(ordered_list[mid+1:], r)
46+
47+
return r
48+
49+
50+
# Tests
51+
print(test_create_bst([9], None, Node(9)))
52+
53+
r1 = Node(5)
54+
Node(1, r1)
55+
Node(9, r1)
56+
print(test_create_bst([1, 5, 9], None, r1))
57+
58+
r2 = Node(5)
59+
r2_1 = Node(3, r2)
60+
Node(1, r2_1)
61+
r2_2 = Node(9, r2)
62+
Node(7, r2_2)
63+
print(test_create_bst([1, 3, 5, 7, 9], None, r2))
64+
65+
r3 = Node(3)
66+
r3_1 = Node(1, r3)
67+
r3_1_1 = Node(1, r3_1)
68+
Node(0, r3_1_1)
69+
Node(2, r3_1)
70+
r3_2 = Node(8, r3)
71+
Node(5, r3_2)
72+
Node(13, r3_2)
73+
print(test_create_bst([0, 1, 1, 2, 3, 5, 8, 13], None, r3))
74+
```
75+
76+
### Additional material
77+
The runnable [Python file](exercise_41.py) is available online.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright (c) 2022, Silvio Peroni <essepuntato@gmail.com>
3+
#
4+
# Permission to use, copy, modify, and/or distribute this software for any purpose
5+
# with or without fee is hereby granted, provided that the above copyright notice
6+
# and this permission notice appear in all copies.
7+
#
8+
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
9+
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
10+
# FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
11+
# OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
12+
# DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
13+
# ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
14+
# SOFTWARE.
15+
16+
from anytree import Node, RenderTree
17+
18+
# Test case for the function
19+
def test_create_bst(ordered_list, parent, expected):
20+
result = create_bst(ordered_list, parent)
21+
22+
if str(RenderTree(result)) == str(RenderTree(expected)):
23+
return True
24+
else:
25+
return False
26+
27+
28+
# Code of the function
29+
def create_bst(ordered_list, parent):
30+
cur_len = len(ordered_list)
31+
32+
if cur_len == 1:
33+
return Node(ordered_list[0], parent)
34+
elif cur_len > 1:
35+
mid = cur_len // 2
36+
37+
r = Node(ordered_list[mid], parent)
38+
39+
create_bst(ordered_list[:mid], r)
40+
create_bst(ordered_list[mid+1:], r)
41+
42+
return r
43+
44+
45+
# Tests
46+
print(test_create_bst([9], None, Node(9)))
47+
48+
r1 = Node(5)
49+
Node(1, r1)
50+
Node(9, r1)
51+
print(test_create_bst([1, 5, 9], None, r1))
52+
53+
r2 = Node(5)
54+
r2_1 = Node(3, r2)
55+
Node(1, r2_1)
56+
r2_2 = Node(9, r2)
57+
Node(7, r2_2)
58+
print(test_create_bst([1, 3, 5, 7, 9], None, r2))
59+
60+
r3 = Node(3)
61+
r3_1 = Node(1, r3)
62+
r3_1_1 = Node(1, r3_1)
63+
Node(0, r3_1_1)
64+
Node(2, r3_1)
65+
r3_2 = Node(8, r3)
66+
Node(5, r3_2)
67+
Node(13, r3_2)
68+
print(test_create_bst([0, 1, 1, 2, 3, 5, 8, 13], None, r3))

0 commit comments

Comments
 (0)