Skip to content

Commit 654c49f

Browse files
committed
improved the code quality
1 parent 77b9533 commit 654c49f

File tree

3 files changed

+7
-27
lines changed

3 files changed

+7
-27
lines changed

pydatastructs/trees/_backend/cpp/BinarySearchTree.hpp

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ static PyObject* BinarySearchTree_search(BinarySearchTree* self, PyObject* args,
115115
if (comp == 1) {
116116
walk = reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(walk)])->left;
117117
}
118-
else{
118+
else {
119119
walk = reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(walk)])->right;
120120
}
121121
}
@@ -124,7 +124,7 @@ static PyObject* BinarySearchTree_search(BinarySearchTree* self, PyObject* args,
124124
if (ret_parent==Py_None || PyLong_AsLong(ret_parent)==0) {
125125
return walk;
126126
}
127-
else{
127+
else {
128128
return Py_BuildValue("OO",walk,parent);
129129
}
130130
Py_RETURN_NONE; // dummy return statement, never executed
@@ -188,7 +188,7 @@ static PyObject* BinarySearchTree_insert(BinarySearchTree* self, PyObject* args)
188188
prev_node = reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(walk)])->right;
189189
walk = reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(walk)])->right;
190190
}
191-
else{
191+
else {
192192
if (reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(walk)])->left == Py_None) {
193193
new_node->parent = prev_node;
194194
ArrayForTrees_append(bt->tree, Py_BuildValue( "[O]", reinterpret_cast<PyObject*>(new_node)) );
@@ -229,12 +229,12 @@ static PyObject* BinarySearchTree_delete(BinarySearchTree* self, PyObject *args,
229229
reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(bt->root_idx)])->data = Py_None;
230230
reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(bt->root_idx)])->key = Py_None;
231231
}
232-
else{
232+
else {
233233
if (reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(parent)])->left == walk) {
234234
Py_INCREF(Py_None);
235235
reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(parent)])->left = Py_None;
236236
}
237-
else{
237+
else {
238238
Py_INCREF(Py_None);
239239
reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(parent)])->right = Py_None;
240240
}
@@ -472,7 +472,7 @@ static PyObject* BinarySearchTree__lca_2(BinarySearchTree* self, PyObject* args)
472472
PyObject* curr_root = bt->root_idx;
473473
PyObject* u = BinarySearchTree_search(self, Py_BuildValue("(O)",j), PyDict_New());
474474
PyObject* v = BinarySearchTree_search(self, Py_BuildValue("(O)",k), PyDict_New());
475-
// std::cout<<PyLong_AsLong(u)<<" "<<PyLong_AsLong(v)<<std::endl;
475+
476476
if (u==Py_None || v==Py_None) {
477477
PyErr_SetString(PyExc_ValueError, "One of the nodes doesn't exist.");
478478
return NULL;
@@ -503,35 +503,26 @@ static PyObject* BinarySearchTree__lca_2(BinarySearchTree* self, PyObject* args)
503503
return NULL;
504504
}
505505
long long v_left = PyLong_AsLongLong(cres2);
506-
// std::cout<<"h1"<<std::endl;
507506

508507
while (!(u_left ^ v_left)) {
509508
if (u_left && v_left) {
510509
curr_root = reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(curr_root)])->left;
511-
// std::cout<<"curr_root chagned to: "<<PyLong_AsLong(curr_root)<<std::endl;
512510
}
513511
else {
514512
curr_root = reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(curr_root)])->right;
515513
}
516-
// std::cout<<"h2"<<std::endl;
517514

518515
if (curr_root == u || curr_root == v) {
519516
if (curr_root == Py_None) {
520517
Py_RETURN_NONE;
521518
}
522519
return reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(curr_root)])->key;
523520
}
524-
// std::cout<<"curr_root "<<PyLong_AsLong(curr_root)<<std::endl;
525-
526-
if (curr_root == Py_None || u == Py_None) {
527-
// std::cout<<"here"<<std::endl;
528-
}
529521

530522
if (!PyCallable_Check(bt->comparator)) {
531523
PyErr_SetString(PyExc_ValueError, "comparator should be callable");
532524
return NULL;
533525
}
534-
// std::cout<<"comp: "<<PyLong_AsLong(reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(u)])->key)<<" "<<PyLong_AsLong(reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(curr_root)])->key)<<std::endl;
535526
PyObject* arguments1 = Py_BuildValue("OO", reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(u)])->key, reinterpret_cast<TreeNode*>(bt->tree->_one_dimensional_array->_data[PyLong_AsLong(curr_root)])->key);
536527
PyObject* cres1 = PyObject_CallObject(bt->comparator, arguments1);
537528
Py_DECREF(arguments1);
@@ -540,7 +531,6 @@ static PyObject* BinarySearchTree__lca_2(BinarySearchTree* self, PyObject* args)
540531
return NULL;
541532
}
542533
u_left = PyLong_AsLongLong(cres1);
543-
// std::cout<<u_left<<std::endl;
544534

545535
if (!PyCallable_Check(bt->comparator)) {
546536
PyErr_SetString(PyExc_ValueError, "comparator should be callable");
@@ -554,9 +544,7 @@ static PyObject* BinarySearchTree__lca_2(BinarySearchTree* self, PyObject* args)
554544
return NULL;
555545
}
556546
v_left = PyLong_AsLongLong(cres2);
557-
// std::cout<<u_left<<" "<<v_left<<std::endl;
558547
}
559-
// std::cout<<"h4"<<std::endl;
560548

561549
if (curr_root == Py_None) {
562550
Py_RETURN_NONE;

pydatastructs/trees/_backend/cpp/BinaryTree.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ static PyObject* BinaryTree___str__(BinaryTree *self) {
114114
Py_INCREF(out);
115115
PyList_SET_ITEM(list, i, out);
116116
}
117-
else{
117+
else {
118118
PyObject* empty_string = PyUnicode_FromString("");
119119
PyList_SET_ITEM(list, i, empty_string);
120120
}

pydatastructs/trees/binary_trees.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,6 @@ def delete(self, key, **kwargs):
386386
return None
387387
if self.tree[walk].left is None and \
388388
self.tree[walk].right is None:
389-
# print("here 1")
390389
if parent is None:
391390
self.tree[self.root_idx].data = None
392391
self.tree[self.root_idx].key = None
@@ -406,7 +405,6 @@ def delete(self, key, **kwargs):
406405

407406
elif self.tree[walk].left is not None and \
408407
self.tree[walk].right is not None:
409-
# print("here 2")
410408
twalk = self.tree[walk].right
411409
par = walk
412410
flag = False
@@ -433,7 +431,6 @@ def delete(self, key, **kwargs):
433431
self._update_size(a)
434432

435433
else:
436-
# print("here 3")
437434
if self.tree[walk].left is not None:
438435
child = self.tree[walk].left
439436
else:
@@ -609,7 +606,6 @@ def _lca_1(self, j, k):
609606
def _lca_2(self, j, k):
610607
curr_root = self.root_idx
611608
u, v = self.search(j), self.search(k)
612-
print(u)
613609
if (u is None) or (v is None):
614610
raise ValueError("One of the nodes with key %s "
615611
"or %s doesn't exits"%(j, k))
@@ -621,21 +617,17 @@ def _lca_2(self, j, k):
621617
while not (u_left ^ v_left):
622618
if u_left and v_left:
623619
curr_root = self.tree[curr_root].left
624-
# print("curr_root changed to: ", curr_root)
625620
else:
626621
curr_root = self.tree[curr_root].right
627622

628623
if curr_root == u or curr_root == v:
629624
if curr_root is None:
630625
return None
631626
return self.tree[curr_root].key
632-
# print(curr_root)
633-
# print(self.tree[u].key, self.tree[curr_root].key)
634627
u_left = self.comparator(self.tree[u].key, \
635628
self.tree[curr_root].key)
636629
v_left = self.comparator(self.tree[v].key, \
637630
self.tree[curr_root].key)
638-
# print(u_left, v_left)
639631

640632
if curr_root is None:
641633
return curr_root

0 commit comments

Comments
 (0)