Skip to content

Commit bda0dd1

Browse files
committed
BST _simple_path() done and tested
1 parent 0036bd1 commit bda0dd1

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

pydatastructs/trees/_backend/cpp/BinarySearchTree.hpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <structmember.h>
77
#include <iostream>
88
#include <cstdlib>
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"
@@ -392,12 +393,48 @@ static PyObject* BinarySearchTree_upper_bound(BinarySearchTree* self, PyObject *
392393
return BinarySearchTree__bound_helper(self, Py_BuildValue("(OOO)", self->binary_tree->root_idx, key, Py_True));
393394
}
394395

396+
static PyObject* BinarySearchTree__simple_path(BinarySearchTree* self, PyObject *args) {
397+
PyObject* key = PyObject_GetItem(args, PyZero);
398+
PyObject* root = PyObject_GetItem(args, PyOne);
399+
std::stack<long> stack;
400+
stack.push(PyLong_AsLong(root));
401+
PyObject* path = PyList_New(0);
402+
long node_idx = -1;
403+
BinaryTree* bt = self->binary_tree;
404+
405+
while (!stack.empty()){
406+
long node = stack.top();
407+
stack.pop();
408+
if (reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[node])->key == key) {
409+
node_idx = node;
410+
break;
411+
}
412+
if (reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[node])->left != Py_None) {
413+
stack.push(PyLong_AsLong(reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[node])->left));
414+
}
415+
if (reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[node])->right != Py_None) {
416+
stack.push(PyLong_AsLong(reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[node])->right));
417+
}
418+
}
419+
if (node_idx == -1) {
420+
return path;
421+
}
422+
while (node_idx != 0) {
423+
PyList_Append(path, PyLong_FromLong(node_idx));
424+
node_idx = PyLong_AsLong(reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[node_idx])->parent);
425+
}
426+
PyList_Append(path, PyLong_FromLong(0));
427+
PyList_Reverse(path);
428+
return path;
429+
}
430+
395431
static struct PyMethodDef BinarySearchTree_PyMethodDef[] = {
396432
{"insert", (PyCFunction) BinarySearchTree_insert, METH_VARARGS | METH_KEYWORDS, NULL},
397433
{"delete", (PyCFunction) BinarySearchTree_delete, METH_VARARGS | METH_KEYWORDS, NULL},
398434
{"search", (PyCFunction) BinarySearchTree_search, METH_VARARGS | METH_KEYWORDS, NULL},
399435
{"lower_bound", (PyCFunction) BinarySearchTree_lower_bound, METH_VARARGS | METH_KEYWORDS, NULL},
400436
{"upper_bound", (PyCFunction) BinarySearchTree_upper_bound, METH_VARARGS | METH_KEYWORDS, NULL},
437+
{"_simple_path", (PyCFunction) BinarySearchTree__simple_path, METH_VARARGS, NULL},
401438
{NULL}
402439
};
403440

pydatastructs/trees/tests/test_binary_trees.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ def test_cpp_BST2():
6464
"(5, 6, 6, 6), (None, 4, 4, None), (None, 7, 7, None), (8, 14, 14, None), "
6565
"(None, 13, 13, None)]")
6666

67+
path = b._simple_path(1,0)
68+
assert path[0] == 0
69+
assert path[1] == 1
70+
assert path[2] == 3
71+
6772
assert b.search(10) == 2
6873
assert b.search(-1) is None
6974
assert b.delete(13) is True

0 commit comments

Comments
 (0)