Skip to content

Commit 3c961ae

Browse files
committed
added preorder traversal and tested
1 parent 1097ccc commit 3c961ae

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

pydatastructs/trees/_backend/cpp/BinaryTreeTraversal.hpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <structmember.h>
77
#include <cstdlib>
88
#include <iostream>
9+
#include <stack>
910
#include "../../../utils/_backend/cpp/utils.hpp"
1011
#include "../../../utils/_backend/cpp/TreeNode.hpp"
1112
#include "../../../linear_data_structures/_backend/cpp/arrays/ArrayForTrees.hpp"
@@ -24,13 +25,14 @@ static void BinaryTreeTraversal_dealloc(BinaryTreeTraversal *self) {
2425

2526
static PyObject* BinaryTreeTraversal___new__(PyTypeObject* type, PyObject *args, PyObject *kwds) {
2627
BinaryTreeTraversal *self;
28+
self = reinterpret_cast<BinaryTreeTraversal*>(type->tp_alloc(type, 0));
29+
2730
PyObject* tree = PyObject_GetItem(args, PyZero);
2831
if (PyType_Ready(&BinarySearchTreeType) < 0) { // This has to be present to finalize a type object. This should be called on all type objects to finish their initialization.
2932
return NULL;
3033
}
3134
if (PyObject_IsInstance(tree, (PyObject *)&BinarySearchTreeType)) {
3235
self->tree = reinterpret_cast<BinarySearchTree*>(tree)->binary_tree;
33-
std::cout<<"here"<<std::endl;
3436
}
3537
else {
3638
PyErr_SetString(PyExc_ValueError, "Not a supported type for BinaryTreeTraversal.");
@@ -39,7 +41,31 @@ static PyObject* BinaryTreeTraversal___new__(PyTypeObject* type, PyObject *args,
3941
return reinterpret_cast<PyObject*>(self);
4042
}
4143

44+
static PyObject* BinaryTreeTraversal__pre_order(BinaryTreeTraversal* self, PyObject *args){
45+
PyObject* visit = PyList_New(0);
46+
ArrayForTrees* tree = self->tree->tree;
47+
long size = self->tree->size;
48+
long node = PyLong_AsLong(PyObject_GetItem(args, PyZero));
49+
std::stack<long> s;
50+
s.push(node);
51+
52+
while (!s.empty()) {
53+
node = s.top();
54+
s.pop();
55+
TreeNode* curr_node = reinterpret_cast<TreeNode*>(tree->_one_dimensional_array->_data[node]);
56+
PyList_Append(visit, reinterpret_cast<PyObject*>(curr_node));
57+
if (curr_node->right != Py_None) {
58+
s.push(PyLong_AsLong(curr_node->right));
59+
}
60+
if (curr_node->left != Py_None) {
61+
s.push(PyLong_AsLong(curr_node->left));
62+
}
63+
}
64+
return visit;
65+
}
66+
4267
static struct PyMethodDef BinaryTreeTraversal_PyMethodDef[] = {
68+
{"_pre_order", (PyCFunction) BinaryTreeTraversal__pre_order, METH_VARARGS, NULL},
4369
{NULL}
4470
};
4571

pydatastructs/trees/_backend/cpp/trees.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <Python.h>
22
#include "BinaryTree.hpp"
33
#include "BinarySearchTree.hpp"
4+
#include "BinaryTreeTraversal.hpp"
45

56
static struct PyModuleDef trees_struct = {
67
PyModuleDef_HEAD_INIT,
@@ -26,5 +27,11 @@ PyMODINIT_FUNC PyInit__trees(void) {
2627
Py_INCREF(&BinarySearchTreeType);
2728
PyModule_AddObject(trees, "BinarySearchTree", reinterpret_cast<PyObject*>(&BinarySearchTreeType));
2829

30+
if (PyType_Ready(&BinaryTreeTraversalType) < 0) {
31+
return NULL;
32+
}
33+
Py_INCREF(&BinaryTreeTraversalType);
34+
PyModule_AddObject(trees, "BinaryTreeTraversal", reinterpret_cast<PyObject*>(&BinaryTreeTraversalType));
35+
2936
return trees;
3037
}

pydatastructs/trees/tests/test_binary_trees.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ def test_cpp_BST2():
6060
"(5, 6, 6, 6), (None, 4, 4, None), (None, 7, 7, None), (8, 14, 14, None), "
6161
"(None, 13, 13, None)]")
6262

63+
from pydatastructs.trees._backend.cpp import _trees
64+
t = _trees.BinaryTreeTraversal(b)
65+
p = t._pre_order(0)
66+
assert [node.key for node in p] == [8, 3, 1, 6, 4, 7, 10, 14, 13]
67+
# key_arr = [node.key for node in p]
68+
# print(key_arr)
69+
6370
##### _simple_path() test #####
6471
path = b._simple_path(1,0)
6572
assert path[0] == 0
@@ -178,6 +185,7 @@ def test_cpp_BST2():
178185
assert b3.lower_bound(-1) == 7
179186
assert b3.lower_bound(20) is None
180187

188+
test_cpp_BST2()
181189

182190
def test_cpp_BST_speed():
183191
BST = BinarySearchTree
@@ -195,6 +203,18 @@ def test_cpp_BST_speed():
195203
print("Time taken by Python backend: ",t2-t1,"s")
196204
print("Time taken by C++ backend: ",t4-t3,"s")
197205

206+
# def test_cpp_BinaryTreeTraversal():
207+
# BST = BinarySearchTree
208+
# from pydatastructs.trees._backend.cpp import _trees
209+
# b = BST(backend=Backend.CPP)
210+
# for i in range(10,-1,-1):
211+
# b.insert(i,i)
212+
# t = _trees.BinaryTreeTraversal(b)
213+
# p = t._pre_order(0)
214+
# key_arr = [node.key for node in p]
215+
# print(key_arr)
216+
217+
# test_cpp_BinaryTreeTraversal()
198218

199219
################### Python Tests below ###################
200220

0 commit comments

Comments
 (0)