递归上限java

时间:2013-03-01 19:03:57

标签: java recursion

这是一个家庭作业问题,所以基本的递归算法就是我真正想要的。我正在研究AATree的地板/天花板方法。我当前的树有这样的结构:

        40
  20        60
10  30    50    80
              70  90
                    100

(等级顺序)。我的楼层方法有一个迭代解决方案:

 /**
   * Returns the greatest element in the set <= the_target.
   * Strings are compared by ASCII values.
   * 
   * @param the_target Element to compare set values with.
   * @return Greatest element <= the_target, null if no such element exists.
   */
  public E floor(final E the_target) {
    AANode<E> current_node = my_root;
    E result = null;
    int root_value = compare(current_node.my_element, the_target);
    while (true) {
      if (root_value == 0) {
        if (current_node.my_left.equals(my_null_node) 
            && current_node.my_right.equals(my_null_node)) {
          break;
        }
        result = current_node.my_element;
        break;
      }
      if (contains(the_target)) {
        result = the_target;
        break;
      } else if (root_value > 0) { 
        if (current_node.my_left.my_element == null) {
          break;
        }
        current_node = current_node.my_left;
        root_value = compare(current_node.my_element, the_target);
      } else {
        if (current_node.my_right.my_element == null) {
          result = current_node.my_element;
          break;
        }
        result = current_node.my_element; 
        current_node = current_node.my_right;  
        root_value = compare(current_node.my_element, the_target);
      }
    }
    return result;
  }

但我想让我的ceiling()方法递归。它必须具有此方法签名:

  /**
   * Returns the smallest element in the set >= the_target.
   * 
   * @param the_target Element to compare set values with.
   * @return Smallest element >= the_target, null if no such element exists.
   */
  public E ceiling(final E the_target);

我打算用一个返回E的辅助递归方法来实现它。我试图让我的逻辑正确,并且会喜欢一些算法建议。

感谢大家的帮助!我懂了。

 /**
   * Helper recursive method for ceiling().
   * 
   * @param the_root The current node.
   * @param the_smallest The previous smallest element.
   * @param the_target The target for ceiling.
   * @return The ceiling element of the tree.
   */
  private E findCeiling(final AANode<E> the_root, final AANode<E> the_smallest,
                        final E the_target) {
    AANode<E> small = the_smallest;
    if (compare(the_target, small.my_element) > 0) {
      small = the_root;
    }
    // base case
    if (the_root.my_left.my_element == null 
        && the_root.my_right.my_element == null) {
      return small.my_element;
    } else {
      if (compare(the_target, the_root.my_element) > 0) {
        if (compare(the_smallest.my_element, the_root.my_element) > 0) {
          small = the_root;
        }
        return findCeiling(the_root.my_right, small, the_target);
      } else {
        if (compare(the_smallest.my_element, the_root.my_element) > 0) {
          small = the_root;
        }
        return findCeiling(the_root.my_left, small, the_target);
      }
    }
  }

2 个答案:

答案 0 :(得分:1)

您希望遍历树并跟踪到目前为止看到的小于目标的集合中的最小值。让我们调用这个值SGT

您的帮助程序例程需要接受当前正在检查的节点以及当前SGT值。

这是一个让你入门的提示......

 public E ceiling(final E target) { 
     return ceilingHelper(root, root.my_element, target);
 }

 private E ceilingHelper(final AANode<E> current_node, 
                         final E SGT, 
                         final E target) { 
    //now do you get the idea ?  your recursion will need a base case
    //and a recursive step.  you can traverse the tree using a depth first
    //search
 }

答案 1 :(得分:0)

我不认为提供一个确切的解决方案是合适的,但你应该看看http://en.wikipedia.org/wiki/Tree_traversal。递归地执行此操作需要所有通用算法。

通常,递归解决方案涉及将问题定义为子问题。作为提示,两个相邻子树的细胞如何相关?还要考虑递归结束的基本内容。叶节点的细胞是什么?

树问题的递归解决方案可以非常简洁和简单。