0

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)
0

2 Answers 2

1

In Javascript if you do not pass parameters to a function it defaults them to being undefined, however, this is not tolerated by Python. If you run your code you will notice you get the following error.

Traceback (most recent call last):
  File "main.py", line 41, in <module>
    root.insert(8)
  File "main.py", line 17, in insert
    currNode.left = ChildNode(val)
  File "main.py", line 34, in __init__
    super().__init__()
TypeError: __init__() missing 2 required positional arguments: 'val' and 'key'

Which is very helpful because it tells us that the super.__init__() call is missing two arguments, which are defined in the super class.

If you intend to replicate the JS behaviour then you would do well to add a default None to the arguments in the constructor of the superclass.

class BinarySearchTree:
    def __init__(self, val: int = None, key: int = None):
        self.val = val
        self.left = None
        self.right = None
        self.key = key
Sign up to request clarification or add additional context in comments.

Comments

1

The child class's __init__ method needs to pass the appropriate arguments in the super().__init__() call.

# having trouble inheriting here
class ChildNode(BinarySearchTree):
    def __init__(self, val: int):
        super().__init__(val, '')

        del self.key

You don't need to do self.val = val in the child class, since that's done by the parent class.

That said, this doesn't seem like an appropriate class hierarchy in either language. You shouldn't delete attributes in the child class, because any inherited methods that depend on it will fail.

7 Comments

Well I wanted the BinarySearchTree instance to have the key property but I didn't want the childNode to inherit that property. I didn't know if there was a way to selectively inherit properties from the parent so I just deleted that key prop for every ChildNode instance
Subclasses are for IS-A relationships. This seems like a HAS-A relationship -- a binary search tree contains child nodes. A child node is not a kind of search tree.
Remember that subclasses must satisfy the Liskov Substitution Principle.
So that means I should include the key property in all my ChildNode instances?
@RonnyFitzgerald then you don't want inheritance
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.