-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbetterbst.py
77 lines (58 loc) · 2.56 KB
/
betterbst.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from __future__ import annotations
from typing import List, Tuple, TypeVar
from data_structures.bst import BinarySearchTree
K = TypeVar('K')
I = TypeVar('I')
class BetterBST(BinarySearchTree[K, I]):
def __init__(self, elements: List[Tuple[K, I]]) -> None:
"""
Initialiser for the BetterBST class.
We assume that the all the elements that will be inserted
into the tree are contained within the elements list.
As such you can assume the length of elements to be non-zero.
The elements list will contain tuples of key, item pairs.
First sort the elements list and then build a balanced tree from the sorted elements
using the corresponding methods below.
Args:
elements(List[tuple[K, I]]): The elements to be inserted into the tree.
Complexity:
Best Case Complexity: TODO
Worst Case Complexity: TODO
"""
super().__init__()
new_elements: List[Tuple[K, I]] = self.__sort_elements(elements)
self.__build_balanced_tree(new_elements)
def __sort_elements(self, elements: List[Tuple[K, I]]) -> List[Tuple[K, I]]:
"""
Recall one of the drawbacks to using a binary search tree is that it can become unbalanced.
If we know the elements ahead of time, we can sort them and then build a balanced tree.
This will help us maintain the O(log n) complexity for searching, inserting, and deleting elements.
Args:
elements (List[Tuple[K, I]]): The elements we wish to sort.
Returns:
list(Tuple[K, I]]) - elements after being sorted.
Complexity:
Best Case Complexity: TODO
Worst Case Complexity: TODO
"""
raise NotImplementedError
def __build_balanced_tree(self, elements: List[Tuple[K, I]]) -> None:
"""
This method will build a balanced binary search tree from the sorted elements.
Args:
elements (List[Tuple[K, I]]): The elements we wish to use to build our balanced tree.
Returns:
None
Complexity:
(This is the actual complexity of your code,
remember to define all variables used.)
Best Case Complexity: TODO
Worst Case Complexity: TODO
Justification:
TODO
Complexity requirements for full marks:
Best Case Complexity: O(n * log(n))
Worst Case Complexity: O(n * log(n))
where n is the number of elements in the list.
"""
raise NotImplementedError