Skip to content

Commit

Permalink
Added order statistics to BST (#31)
Browse files Browse the repository at this point in the history
* added ArrayForTrees

* added order statistics to BSTs

* removed tuple as input from arrays
  • Loading branch information
czgdp1807 authored Oct 30, 2019
1 parent fbc4a25 commit c0fdee9
Show file tree
Hide file tree
Showing 5 changed files with 322 additions and 91 deletions.
59 changes: 51 additions & 8 deletions pydatastructs/linear_data_structures/arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class OneDimensionalArray(Array):
A valid object type.
size: int
The number of elements in the array.
elements: list/tuple
elements: list
The elements in the array, all should
be of same type.
init: a python type
Expand Down Expand Up @@ -73,12 +73,18 @@ def __new__(cls, dtype=NoneType, *args, **kwargs):
obj = object.__new__(cls)
obj._dtype = dtype
if len(args) == 2:
if _check_type(args[0], (list, tuple)) and \
if _check_type(args[0], list) and \
_check_type(args[1], int):
size, data = args[1], [dtype(arg) for arg in args[0]]
elif _check_type(args[1], (list, tuple)) and \
for i in range(len(args[0])):
if dtype != type(args[0][i]):
args[0][i] = dtype(args[0][i])
size, data = args[1], [arg for arg in args[0]]
elif _check_type(args[1], list) and \
_check_type(args[0], int):
size, data = args[0], [dtype(arg) for arg in args[1]]
for i in range(len(args[1])):
if dtype != type(args[1][i]):
args[1][i] = dtype(args[1][i])
size, data = args[0], [arg for arg in args[1]]
else:
raise TypeError("Expected type of size is int and "
"expected type of data is list/tuple.")
Expand All @@ -93,8 +99,11 @@ def __new__(cls, dtype=NoneType, *args, **kwargs):
init = kwargs.get('init', None)
obj._data = [init for i in range(args[0])]
elif _check_type(args[0], (list, tuple)):
for i in range(len(args[0])):
if dtype != type(args[0][i]):
args[0][i] = dtype(args[0][i])
obj._size, obj._data = len(args[0]), \
[dtype(arg) for arg in args[0]]
[arg for arg in args[0]]
else:
raise TypeError("Expected type of size is int and "
"expected type of data is list/tuple.")
Expand All @@ -110,7 +119,9 @@ def __setitem__(self, idx, elem):
if elem is None:
self._data[idx] = None
else:
self._data[idx] = self._dtype(elem)
if type(elem) != self._dtype:
elem = self._dtype(elem)
self._data[idx] = elem

def fill(self, elem):
elem = self._dtype(elem)
Expand Down Expand Up @@ -237,8 +248,40 @@ def delete(self, idx):
self[idx] != None:
self[idx] = None
self._num -= 1
self._modify()
return self._modify()

@property
def size(self):
return self._size

class ArrayForTrees(DynamicOneDimensionalArray):
"""
Utility dynamic array for storing nodes of a tree.
See Also
========
pydatastructs.linear_data_structures.arrays.DynamicOneDimensionalArray
"""
def _modify(self):
if self._num/self._size < self._load_factor:
new_indices = dict()
arr_new = OneDimensionalArray(self._dtype, 2*self._num + 1)
j = 0
for i in range(self._last_pos_filled + 1):
if self[i] != None:
arr_new[j] = self[i]
new_indices[self[i].key] = j
j += 1
for i in range(j):
if arr_new[i].left != None:
arr_new[i].left = new_indices[self[arr_new[i].left].key]
if arr_new[i].right != None:
arr_new[i].right = new_indices[self[arr_new[i].right].key]
if arr_new[i].parent != None:
arr_new[i].parent = new_indices[self[arr_new[i].parent].key]
self._last_pos_filled = j - 1
self._data = arr_new._data
self._size = arr_new._size
return new_indices
return None
7 changes: 4 additions & 3 deletions pydatastructs/linear_data_structures/tests/test_arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@

def test_OneDimensionalArray():
ODA = OneDimensionalArray
A = ODA(int, 5, [1, 2, 3, 4, 5], init=6)
A = ODA(int, 5, [1.0, 2, 3, 4, 5], init=6)
A[1] = 2.0
assert A
assert ODA(int, (1, 2, 3, 4, 5), 5)
assert ODA(int, [1.0, 2, 3, 4, 5], 5)
assert ODA(int, 5)
assert ODA(int, [1, 2, 3])
assert ODA(int, [1.0, 2, 3])
raises(IndexError, lambda: A[7])
raises(IndexError, lambda: A[-1])
raises(ValueError, lambda: ODA())
Expand Down
Loading

0 comments on commit c0fdee9

Please sign in to comment.