tayloh / algorithms

Minimal examples of data structures and algorithms in Python (forked for DD2480 assignment 3)
MIT License
0 stars 0 forks source link

Refactor tree/red_black_tree/red_black_tree.py. #7

Open aoengin opened 1 year ago

aoengin commented 1 year ago

Document the plan for reducing the CCN by refactoring the code.

aoengin commented 1 year ago

In the main while loop there are two different cases:

Therefore it's possible to divide these two parts into two different methods to decrease the CCN of the code. Since the code is divided into two pieces, helper methods do not have high CCN and at the same time, the complexity of the delete_fixup method substantially decreases. It is not possible to remove any if clauses since all checks are necessary for different conditions. However, it is possible to make some if conditions more understandable by dividing them into two pieces. For example:

if (node_brother.left is None or node_brother.left.color == 0) and (
                                node_brother.right is None or node_brother.right.color == 0)

This if statement can be divided into two different if clauses:

if (node_brother.left is None or node_brother.left.color == 0):
         if(node_brother.right is None or node_brother.right.color == 0)

Even though it does not decrease the complexity it makes code readable and understandable.