emory-courses / dsa-java

Data Structures and Algorithms in Java
https://emory.gitbook.io/dsa-java/
42 stars 55 forks source link

Section 4.1 #121

Closed ddalama closed 3 years ago

ddalama commented 3 years ago

Binary Search Trees: Section 4.1

private void replaceChild(N oldNode, N newNode) {
    if (isRoot(oldNode))
        setRoot(newNode);
    else
        oldNode.getParent().replaceChild(oldNode, newNode);
}

In the above code (from AbstractBinarySearchTree class), L5 calls the superclass's method replaceChild(). Is this correct? If this is correct, how does the Java compiler know it has to call the superclass's replaceChild() method and not recursively call this class's replaceChild() method?

marvinquiet commented 3 years ago

It calls the oldNode's parent replaceChild function, which is in AbstractBinaryNode class.