Skip to content

Commit

Permalink
Fixed SBBT traversal
Browse files Browse the repository at this point in the history
  • Loading branch information
Kishan-Ved committed Jun 5, 2024
1 parent 463c9aa commit f69b9d4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <cstdlib>
#include <iostream>
#include <structmember.h>
#include "../../../../linear_data_structures/_backend/cpp/arrays/DynamicOneDimensionalArray.hpp"

Expand Down
8 changes: 4 additions & 4 deletions pydatastructs/trees/_backend/cpp/BinaryTreeTraversal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ static PyObject* BinaryTreeTraversal___new__(PyTypeObject* type, PyObject *args,
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.
return NULL;
}
if (PyObject_IsInstance(tree, (PyObject *)&BinarySearchTreeType)) {
self->tree = reinterpret_cast<BinarySearchTree*>(tree)->binary_tree;
}
else if (PyObject_IsInstance(tree, (PyObject *)&SelfBalancingBinaryTreeType)) {
if (PyObject_IsInstance(tree, (PyObject *)&SelfBalancingBinaryTreeType)) {
self->tree = reinterpret_cast<SelfBalancingBinaryTree*>(tree)->bst->binary_tree;
}
else if (PyObject_IsInstance(tree, (PyObject *)&BinarySearchTreeType)) {
self->tree = reinterpret_cast<BinarySearchTree*>(tree)->binary_tree;
}
else {
PyErr_SetString(PyExc_ValueError, "Not a supported type for BinaryTreeTraversal.");
return NULL;
Expand Down
12 changes: 6 additions & 6 deletions pydatastructs/trees/tests/test_binary_trees.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,13 +456,13 @@ def test_SelfBalancingBinaryTree():
assert tree.tree[2].right != 3
assert tree.tree[tree.tree[5].parent].right == 5

# trav = BinaryTreeTraversal(tree, backend=Backend.CPP)
# 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] == [4.4, 4.5, 4.55, 4.6, 4.65, 5, 5.5]
# assert [node.key for node in pre_order] == [5, 4.5, 4.4, 4.55, 4.6, 4.65, 5.5]
trav = BinaryTreeTraversal(tree, backend=Backend.CPP)
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] == [4.4, 4.5, 4.55, 4.6, 4.65, 5, 5.5]
assert [node.key for node in pre_order] == [5, 4.5, 4.4, 4.55, 4.6, 4.65, 5.5]

# assert tree.tree[tree.tree[3].parent].right == 3
assert tree.tree[tree.tree[3].parent].right == 3
# tree._left_rotate(5, 3)
# assert str(tree) == original_tree
# tree.insert(4.54, 4.54)
Expand Down

0 comments on commit f69b9d4

Please sign in to comment.