|
6 | 6 | #include <structmember.h>
|
7 | 7 | #include <iostream>
|
8 | 8 | #include <cstdlib>
|
| 9 | +#include <stack> |
9 | 10 | #include "../../../utils/_backend/cpp/utils.hpp"
|
10 | 11 | #include "../../../utils/_backend/cpp/TreeNode.hpp"
|
11 | 12 | #include "../../../linear_data_structures/_backend/cpp/arrays/ArrayForTrees.hpp"
|
@@ -392,12 +393,48 @@ static PyObject* BinarySearchTree_upper_bound(BinarySearchTree* self, PyObject *
|
392 | 393 | return BinarySearchTree__bound_helper(self, Py_BuildValue("(OOO)", self->binary_tree->root_idx, key, Py_True));
|
393 | 394 | }
|
394 | 395 |
|
| 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 | + |
395 | 431 | static struct PyMethodDef BinarySearchTree_PyMethodDef[] = {
|
396 | 432 | {"insert", (PyCFunction) BinarySearchTree_insert, METH_VARARGS | METH_KEYWORDS, NULL},
|
397 | 433 | {"delete", (PyCFunction) BinarySearchTree_delete, METH_VARARGS | METH_KEYWORDS, NULL},
|
398 | 434 | {"search", (PyCFunction) BinarySearchTree_search, METH_VARARGS | METH_KEYWORDS, NULL},
|
399 | 435 | {"lower_bound", (PyCFunction) BinarySearchTree_lower_bound, METH_VARARGS | METH_KEYWORDS, NULL},
|
400 | 436 | {"upper_bound", (PyCFunction) BinarySearchTree_upper_bound, METH_VARARGS | METH_KEYWORDS, NULL},
|
| 437 | + {"_simple_path", (PyCFunction) BinarySearchTree__simple_path, METH_VARARGS, NULL}, |
401 | 438 | {NULL}
|
402 | 439 | };
|
403 | 440 |
|
|
0 commit comments