Skip to content

Commit

Permalink
Added all tree traversals for Cartesian Trees
Browse files Browse the repository at this point in the history
  • Loading branch information
Kishan-Ved committed Jul 10, 2024
1 parent 7a0ce6f commit e3076ef
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
7 changes: 7 additions & 0 deletions pydatastructs/trees/_backend/cpp/BinaryTreeTraversal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "RedBlackTree.hpp"
#include "SplayTree.hpp"
#include "AVLTree.hpp"
#include "CartesianTree.hpp"

typedef struct {
PyObject_HEAD
Expand Down Expand Up @@ -48,13 +49,19 @@ static PyObject* BinaryTreeTraversal___new__(PyTypeObject* type, PyObject *args,
if (PyType_Ready(&AVLTreeType) < 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 (PyType_Ready(&CartesianTreeType) < 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 *)&SplayTreeType)) {
self->tree = reinterpret_cast<SplayTree*>(tree)->sbbt->bst->binary_tree;
}
else if (PyObject_IsInstance(tree, (PyObject *)&AVLTreeType)) {
self->tree = reinterpret_cast<AVLTree*>(tree)->sbbt->bst->binary_tree;
}
else if (PyObject_IsInstance(tree, (PyObject *)&CartesianTreeType)) {
self->tree = reinterpret_cast<CartesianTree*>(tree)->sbbt->bst->binary_tree;
}
else if (PyObject_IsInstance(tree, (PyObject *)&RedBlackTreeType)) {
self->tree = reinterpret_cast<RedBlackTree*>(tree)->sbbt->bst->binary_tree;
}
Expand Down
24 changes: 12 additions & 12 deletions pydatastructs/trees/tests/test_binary_trees.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,21 +430,21 @@ def _test_CartesianTree(backend):
"(7, 7, 22, 7, 8), (None, 6, 42, 6, None), "
"(None, 8, 49, 8, None), (None, 2, 99, 2, None)]")

# 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] == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# assert [node.key for node in pre_order] == [3, 1, 0, 2, 5, 4, 9, 7, 6, 8]
trav = BinaryTreeTraversal(tree, 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] == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
assert [node.key for node in pre_order] == [3, 1, 0, 2, 5, 4, 9, 7, 6, 8]

# tree.insert(1.5, 4, 1.5)
tree.insert(1.5, 4, 1.5)

# 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] == [0, 1, 1.5, 2, 3, 4, 5, 6, 7, 8, 9]
# assert [node.key for node in pre_order] == [3, 1.5, 1, 0, 2, 5, 4, 9, 7, 6, 8]
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] == [0, 1, 1.5, 2, 3, 4, 5, 6, 7, 8, 9]
assert [node.key for node in pre_order] == [3, 1.5, 1, 0, 2, 5, 4, 9, 7, 6, 8]

# k = tree.search(1.5)
# assert tree.tree[tree.tree[k].parent].key == 3
k = tree.search(1.5)
assert tree.tree[tree.tree[k].parent].key == 3
# tree.delete(1.5)
# tree.tree[tree.tree[tree.root_idx].left].key == 1
# tree.delete(8)
Expand Down

0 comments on commit e3076ef

Please sign in to comment.