I'm having trouble translating the following JavaScript code into python. My problem is inheriting the parent properties and methods in the ChildNode class by using super.__init__(). In js, you just call super() and get all the props. It doesn't seem to be the same in python. The super function is demanding to have val and key inside the parenthesis but I'm not sure how that would work because Im getting val from the ChildNode.__init__ function
JS CODE that does what I want
class BinarySearchTree {
constructor(val, key) {
this.val = val;
this.left = null;
this.right = null;
this.key = key;
}
insert(val) {
const currNode = this;
if (val === currNode.val) return null;
if (val < currNode.val) {
if (!currNode.left) {
currNode.left = new ChildNode(val);
return this;
} else {
currNode.left.insert(val);
}
}
if (val > currNode.val) {
if (!currNode.right) {
currNode.right = new ChildNode(val);
return this;
} else {
currNode.right.insert(val);
}
}
}
}
class ChildNode extends BinarySearchTree {
constructor(val) {
super();
this.val = val;
delete this.key;
}
}
var root = new BinarySearchTree(20, 14);
root.insert(8);
root.insert(22);
root.insert(4);
console.log(root);
Python attempt
class BinarySearchTree:
def __init__(self, val: int, key: int):
self.val = val
self.left = None
self.right = None
self.key = key
def insert(self, val):
currNode = self
if val == currNode.val:
return None
if val < currNode.val:
if not currNode.left:
currNode.left = ChildNode(val)
return self
else:
currNode.left.insert(val)
if val > currNode.val:
if not currNode.right:
currNode.right = ChildNode(val)
return self
else:
currNode.right.insert(val)
# having trouble inheriting here
class ChildNode(BinarySearchTree):
def __init__(self, val):
super().__init__()
self.val = val
del self.key
root = BinarySearchTree(20, 14)
root.insert(8)
root.insert(22)
root.insert(4)
print(root)