Java inheritance and recursion

倖福魔咒の 提交于 2019-12-23 15:39:03

问题


I have a superclass and a subclass as follows:

class Tree{
 ..
 public void add(..){
 //makes a call to protected function add(..) 
 }//for client to use.
 protected TreeNode add(..){}//recursive function which calls itslef
}

class Stree extends Tree{
 //overrides the recursive add function from class Tree
 protected TreeNode add(..){
   ..
   super.add();//calls the non-recursive add function in superclass.
 }
}

The problem here is that when I call super.add() from the new add function in the subclass, it goes to Tree.add(). Inside Tree.add(). there is a call to add(), which calls the recursive add function in the subclass instead of super, i.e. Stree.add(), instead of Tree.add() which results in an infinite loop. Do you guys see where the problem is?

This is a homework assignment hence I cannot change the name of the recursive function. I am explicitly required to add functionality to the recursive add function, without rewriting any existing code, which basically means I will have to make a call to the original add() function.

edit: Code for the Tree.add()//recursive. Note that I cannot modify this code to gain the functionality I seek.

protected StreeNode add(StreeNode node, String value) {
        if (node == null) {
            node = new StreeNode(value);
            numElements++;
        } else if (node.data.compareTo(value) == 0) {
            // do nothing, String was already in Set
        } else if (node.data.compareTo(value) > 0) {
            node.left = add(node.left, value);      // x = change(x)
        } else {
            node.right = add(node.right, value);    // x = change(x)
        }

        return node;
    }

edit: Now that I see that this is the expected behaviour, how do I go about achieving the following:

  1. Add the value using the original recursive add()
  2. Implement extra functionality

回答1:


Without seeing the parameters I assume void add(...) is the method to add something into the tree, whereas the protected recursive method looks for the node to add to and then performs the add.

I further assume the public non-recursive method passes the tree's root to the recursive method as a start parameter while the recursive method either passes the left or right child until you hit a leaf. Calling the non-recursive method might thus in starting at the root again and again.

Thus I'd say that the recursive and inherited method should not call the non-recursive version but should call itself again.




回答2:


Ok. This is random, may not make much sense.

In your comments, it says your Tree.add is recursive, and that STree.add is recursive:

Create protected method Tree.addCommon(..), which does not recurse, just does the stuff that needs to be done.

Tree.add(..) calls addCommon(..), and then this.add(..) to recurse.

STree.add(..) does it's extra stuff, calls super.addCommon(..) for the common stuff, and then this.add(..) to recurse.

I know. should have written pseudo-code. Lazy.




回答3:


Maybe it's not the case if you can't modify the class, but I think the best solution is to create another method called addHelper(...) that will be responsible for the recursion, then you call this helper inside the add(...) method.



来源:https://stackoverflow.com/questions/10357029/java-inheritance-and-recursion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!