I am shifting from procedural C programming to OOP Python programming and I faced some problems while implementing binary search trees.
I cannot make my Tree_Node null in case of deleting it. In C I could do this because I would handle the case in the function as follows:
void insert(Node tree, int key)
{
if (tree == null)
{
// deal with it
}
}
But in python using OOP I cannot make my Tree_Node null as it is a class and it contains all my methods.
class Tree:
# Tree methods
t = Tree()
t = None # Cannot do this as then how would I call its methods
Q1) How do you solve this problem in general? (not just for binary search trees) What is the standard OOP practice for this?
Second problem is that I cannot modify self in Python. In C I could do as follows:
void foo(Node tree, int key)
{
tree = tree->left
}
But in python's class I cannot do as follows:
class Tree:
def foo(self, key):
self = self.left # Cannot do this
Q2) So how do I solve such problems? Any standard OOP practice?
Q3) A meta question. I thought OOP makes programming easier. But in this case, I find it much tough to implement a rather simple data structure. It adds restrictions such as not able to modify self, not able to make it null etc. Am I doing something wrong?
Treeclass should contain both methods and data. So you would have some properties (leftandrightI assume from your subsequent snippets) that would represent the value of the nodes, allowing you to sayt.left = None.