Skip to content

Commit

Permalink
All RedBlackTree traversals done
Browse files Browse the repository at this point in the history
  • Loading branch information
Kishan-Ved committed Jun 7, 2024
1 parent ac88b3d commit bc02f34
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
10 changes: 9 additions & 1 deletion pydatastructs/trees/_backend/cpp/BinaryTreeTraversal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "BinaryTree.hpp"
#include "BinarySearchTree.hpp"
#include "SelfBalancingBinaryTree.hpp"
#include "RedBlackTree.hpp"

typedef struct {
PyObject_HEAD
Expand All @@ -36,7 +37,14 @@ 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 *)&SelfBalancingBinaryTreeType)) {
if (PyType_Ready(&RedBlackTreeType) < 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 *)&RedBlackTreeType)) {
self->tree = reinterpret_cast<RedBlackTree*>(tree)->sbbt->bst->binary_tree;
}
else if (PyObject_IsInstance(tree, (PyObject *)&SelfBalancingBinaryTreeType)) {
self->tree = reinterpret_cast<SelfBalancingBinaryTree*>(tree)->bst->binary_tree;
}
else if (PyObject_IsInstance(tree, (PyObject *)&BinarySearchTreeType)) {
Expand Down
10 changes: 5 additions & 5 deletions pydatastructs/trees/tests/test_binary_trees.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,11 +559,11 @@ def test_RedBlackTree():
tree.insert(6, 6)
assert str(tree) == "[(11, 10, 10, 3), (10, 18, 18, None), (None, 7, 7, None), (None, 15, 15, None), (0, 16, 16, 6), (None, 30, 30, None), (1, 25, 25, 7), (5, 40, 40, 8), (None, 60, 60, None), (None, 2, 2, None), (None, 17, 17, None), (9, 6, 6, 2)]"

# trav = BinaryTreeTraversal(tree)
# 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] == [2, 6, 7, 10, 15, 16, 17, 18, 25, 30, 40, 60]
# assert [node.key for node in pre_order] == [16, 10, 6, 2, 7, 15, 25, 18, 17, 40, 30, 60]
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] == [2, 6, 7, 10, 15, 16, 17, 18, 25, 30, 40, 60]
assert [node.key for node in pre_order] == [16, 10, 6, 2, 7, 15, 25, 18, 17, 40, 30, 60]

# assert tree.lower_bound(0) == 2
# assert tree.lower_bound(2) == 2
Expand Down

0 comments on commit bc02f34

Please sign in to comment.