Skip to content

Commit

Permalink
delete() for Splay Tree
Browse files Browse the repository at this point in the history
  • Loading branch information
Kishan-Ved committed Jun 20, 2024
1 parent 23ce3e0 commit 4ed8098
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 17 deletions.
18 changes: 18 additions & 0 deletions pydatastructs/trees/_backend/cpp/SplayTree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,27 @@ static PyObject* SplayTree_insert(SplayTree *self, PyObject* args) {
Py_RETURN_NONE;
}

static PyObject* SplayTree_delete(SplayTree *self, PyObject* args) {
PyObject* x = PyObject_GetItem(args, PyZero);
BinaryTree* bt = self->sbbt->bst->binary_tree;

PyObject* kwd_parent = PyDict_New();
PyDict_SetItemString(kwd_parent, "parent", PyLong_FromLong(1));
PyObject* tup = SelfBalancingBinaryTree_search(self->sbbt, Py_BuildValue("(O)", x), kwd_parent);
PyObject* e = PyTuple_GetItem(tup, 0);
PyObject* p = PyTuple_GetItem(tup, 1);
if (e == Py_None){
Py_RETURN_NONE;
}
SplayTree_splay(self, Py_BuildValue("(OO)", e, p));
PyObject* status = SelfBalancingBinaryTree_delete(self->sbbt, Py_BuildValue("(O)", x), PyDict_New());
return status;
}


static struct PyMethodDef SplayTree_PyMethodDef[] = {
{"insert", (PyCFunction) SplayTree_insert, METH_VARARGS, NULL},
{"delete", (PyCFunction) SplayTree_delete, METH_VARARGS, NULL},
{NULL}
};

Expand Down
34 changes: 17 additions & 17 deletions pydatastructs/trees/tests/test_binary_trees.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,28 +503,28 @@ def test_SplayTree():
assert [node.key for node in in_order] == [20, 30, 40, 50, 55, 100, 200]
assert [node.key for node in pre_order] == [55, 30, 20, 40, 50, 200, 100]

# t.delete(40)
t.delete(40)

# 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] == [20, 30, 50, 55, 100, 200]
# assert [node.key for node in pre_order] == [50, 30, 20, 55, 200, 100]
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] == [20, 30, 50, 55, 100, 200]
assert [node.key for node in pre_order] == [50, 30, 20, 55, 200, 100]

# t.delete(150)
t.delete(150)

# 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] == [20, 30, 50, 55, 100, 200]
# assert [node.key for node in pre_order] == [50, 30, 20, 55, 200, 100]
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] == [20, 30, 50, 55, 100, 200]
assert [node.key for node in pre_order] == [50, 30, 20, 55, 200, 100]

# t1 = SplayTree(1000, 1000)
# t1.insert(2000, 2000)
t1 = SplayTree(1000, 1000)
t1.insert(2000, 2000)

# trav = BinaryTreeTraversal(t1)
# 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] == [1000, 2000]
# assert [node.key for node in pre_order] == [2000, 1000]
trav = BinaryTreeTraversal(t1)
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] == [1000, 2000]
assert [node.key for node in pre_order] == [2000, 1000]

# t.join(t1)

Expand Down

0 comments on commit 4ed8098

Please sign in to comment.