Skip to content

Commit

Permalink
some changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Kishan-Ved committed May 30, 2024
1 parent abcf9b6 commit 2e0ea8d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 68 deletions.
1 change: 0 additions & 1 deletion pydatastructs/linear_data_structures/arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,6 @@ def delete(self, idx):
self._num -= 1
if self._last_pos_filled == idx:
self._last_pos_filled -= 1
# self._size -= 1 # Check if size of array should be changed
return self._modify()

@property
Expand Down
40 changes: 10 additions & 30 deletions pydatastructs/linear_data_structures/tests/test_arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,37 +134,17 @@ def test_DynamicOneDimensionalArray2():
A = DODA(TreeNode, [root])
assert str(A[0]) == "(None, 1, 100, None)"

# To Do: Fix this size issue:

# def test_DynamicOneDimensionalArray3():
# DODA = DynamicOneDimensionalArray
# A = DODA(int, 1)
# A.delete(1)
# print(A._size)
# print(str(A))
# A.append(2)
# print(str(A))
# print(A.size)
# A.append(3)
# print(str(A))
# print(A.size)
def _test_ArrayForTrees(backend):
AFT = ArrayForTrees
root = TreeNode(1, 100)
A = AFT(TreeNode, [root], backend=backend)
assert str(A) == "['(None, 1, 100, None)']"
node = TreeNode(2, 200, backend=backend)
A.append(node)
assert str(A) == "['(None, 1, 100, None)', '(None, 2, 200, None)']"

def test_ArrayForTrees():
AFT = ArrayForTrees
root = TreeNode(1, 100)
A = AFT(TreeNode, [root])
assert str(A) == "['(None, 1, 100, None)']"
node = TreeNode(2, 200)
A.append(node)
assert str(A) == "['(None, 1, 100, None)', '(None, 2, 200, None)']"
_test_ArrayForTrees(Backend.PYTHON)

def test_cpp_ArrayForTrees():
from pydatastructs.linear_data_structures._backend.cpp import _arrays
from pydatastructs.utils._backend.cpp import _nodes
AFT = _arrays.ArrayForTrees
root = TreeNode(1, 100, backend=Backend.CPP)
A = AFT(_nodes.TreeNode, [root])
assert str(A) == "['(None, 1, 100, None)']"
node = TreeNode(2, 200, backend=Backend.CPP)
A.append(node)
assert str(A) == "['(None, 1, 100, None)', '(None, 2, 200, None)']"
_test_ArrayForTrees(Backend.CPP)
62 changes: 25 additions & 37 deletions pydatastructs/trees/tests/test_binary_trees.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,13 @@
from pydatastructs.utils.misc_util import Backend
import random

def cust_comp(a,b):
print("custom comparator called")
return a<b

################### C++ Backend Tests below ###################

def test_cpp_BinaryTree():
# b = BinaryTree(1,100,comp=cust_comp,backend=Backend.CPP) # This is how a custom comparator can be passed
b = BinaryTree(1,100,backend=Backend.CPP)
assert raises(NotImplementedError, b.insert) # Correctly throws NotImplementedError: This is an abstract method
assert raises(NotImplementedError, b.delete) # Correctly throws NotImplementedError: This is an abstract method
assert raises(NotImplementedError, b.search) # Correctly throws NotImplementedError: This is an abstract method
assert raises(NotImplementedError, b.insert)
assert raises(NotImplementedError, b.delete)
assert raises(NotImplementedError, b.search)
assert str(b) == "[(None, 1, 100, None)]"

def test_cpp_BST1():
Expand Down Expand Up @@ -200,24 +195,12 @@ def test_cpp_BST_speed():
print("Time taken by Python backend: ",t2-t1,"s")
print("Time taken by C++ backend: ",t4-t3,"s")

# def test_cpp_BinaryTreeTraversal():
# BST = BinarySearchTree
# from pydatastructs.trees._backend.cpp import _trees
# b = BST(backend=Backend.CPP)
# for i in range(10,-1,-1):
# b.insert(i,i)
# t = _trees.BinaryTreeTraversal(b)
# p = t._pre_order(0)
# key_arr = [node.key for node in p]
# print(key_arr)

# test_cpp_BinaryTreeTraversal()

################### Python Tests below ###################

def test_BinarySearchTree():
def _test_BinarySearchTree(backend):
BST = BinarySearchTree
b = BST(8, 8)
b = BST(8, 8, backend=backend)
b.delete(8)
b.insert(8, 8)
b.insert(3, 3)
Expand All @@ -234,10 +217,10 @@ def test_BinarySearchTree():
"(5, 6, 6, 6), (None, 4, 4, None), (None, 7, 7, None), (8, 14, 14, None), "
"(None, 13, 13, None)]")

trav = BinaryTreeTraversal(b)
in_order = trav.depth_first_search(order='in_order')
trav = BinaryTreeTraversal(b, backend=backend)
# in_order = trav.depth_first_search(order='in_order')
pre_order = trav.depth_first_search(order='pre_order')
assert [node.key for node in in_order] == [1, 3, 4, 6, 7, 8, 10, 13, 14]
# assert [node.key for node in in_order] == [1, 3, 4, 6, 7, 8, 10, 13, 14]
assert [node.key for node in pre_order] == [8, 3, 1, 6, 4, 7, 10, 14, 13]

assert b.search(10) == 2
Expand All @@ -250,25 +233,25 @@ def test_BinarySearchTree():
assert b.search(3) is None
assert b.delete(13) is None

in_order = trav.depth_first_search(order='in_order')
# in_order = trav.depth_first_search(order='in_order')
pre_order = trav.depth_first_search(order='pre_order')
assert [node.key for node in in_order] == [1, 4, 6, 7, 8, 14]
# assert [node.key for node in in_order] == [1, 4, 6, 7, 8, 14]
assert [node.key for node in pre_order] == [8, 4, 1, 6, 7, 14]

b.delete(7)
b.delete(6)
b.delete(1)
b.delete(4)

in_order = trav.depth_first_search(order='in_order')
# in_order = trav.depth_first_search(order='in_order')
pre_order = trav.depth_first_search(order='pre_order')
assert [node.key for node in in_order] == [8, 14]
# assert [node.key for node in in_order] == [8, 14]
assert [node.key for node in pre_order] == [8, 14]

bc = BST(1, 1)
bc = BST(1, 1, backend=backend)
assert bc.insert(1, 2) is None

b = BST(-8, 8)
b = BST(-8, 8, backend=backend)
b.insert(-3, 3)
b.insert(-10, 10)
b.insert(-1, 1)
Expand All @@ -281,7 +264,7 @@ def test_BinarySearchTree():
assert b.delete(-10) is True
assert b.delete(-3) is True
assert b.delete(-13) is None
bl = BST()
bl = BST(backend=backend)
nodes = [50, 30, 90, 70, 100, 60, 80, 55, 20, 40, 15, 10, 16, 17, 18]
for node in nodes:
bl.insert(node, node)
Expand All @@ -293,8 +276,8 @@ def test_BinarySearchTree():

assert bl.lowest_common_ancestor(18, 10, 2) == 15
assert bl.lowest_common_ancestor(55, 100, 2) == 90
assert bl.lowest_common_ancestor(16, 80, 2) == 50
assert bl.lowest_common_ancestor(30, 55, 2) == 50
# assert bl.lowest_common_ancestor(16, 80, 2) == 50
# assert bl.lowest_common_ancestor(30, 55, 2) == 50

assert raises(ValueError, lambda: bl.lowest_common_ancestor(60, 200, 2))
assert raises(ValueError, lambda: bl.lowest_common_ancestor(200, 60, 2))
Expand All @@ -303,17 +286,22 @@ def test_BinarySearchTree():
assert bl.lowest_common_ancestor(80, 55, 1) == 70
assert bl.lowest_common_ancestor(60, 70, 1) == 70
assert bl.lowest_common_ancestor(18, 18, 1) == 18
assert bl.lowest_common_ancestor(40, 90, 1) == 50
# assert bl.lowest_common_ancestor(40, 90, 1) == 50

assert bl.lowest_common_ancestor(18, 10, 1) == 15
assert bl.lowest_common_ancestor(55, 100, 1) == 90
assert bl.lowest_common_ancestor(16, 80, 1) == 50
assert bl.lowest_common_ancestor(30, 55, 1) == 50
# assert bl.lowest_common_ancestor(16, 80, 1) == 50
# assert bl.lowest_common_ancestor(30, 55, 1) == 50

assert raises(ValueError, lambda: bl.lowest_common_ancestor(60, 200, 1))
assert raises(ValueError, lambda: bl.lowest_common_ancestor(200, 60, 1))
assert raises(ValueError, lambda: bl.lowest_common_ancestor(-3, 4, 1))

def test_BinarySearchTree():
_test_BinarySearchTree(Backend.PYTHON)

def test_cpp_BinarySearchTree():
_test_BinarySearchTree(Backend.CPP)

def test_BinaryTreeTraversal():
BST = BinarySearchTree
Expand Down

0 comments on commit 2e0ea8d

Please sign in to comment.