简体   繁体   English

Java链表maxElement递归

[英]Java Linked List maxElement recursive

I have been working on an assignment for awhile now one of the methods I have to define is a recursive solution to finding the max element in the list. 我已经从事一段时间的赋值工作,现在我必须定义的方法之一是在列表中查找max元素的递归解决方案。 I feel like my solution is close, but I keep getting the last element inserted back, instead of the max element. 我觉得我的解决方案已经接近了,但是我一直在找回最后插入的元素,而不是max元素。 Can someone point out what I need to do to solve this? 有人可以指出我需要做些什么来解决这个问题吗? * I have been instructed not to use pre-built classes or methods. *已指示我不要使用预构建的类或方法。 * *

        int maxElement () {

    if (head == null) {
        return -1;
    }
    else {
        Node current = head;
        return maxElement (current);
    }
}

private int maxElement (Node current) {     
    Node max;
    // If our next node does not exist, our current node has to be max
    if (current.getNext() == null) {
        return current.getKey();
    }
    //  
    else {  

        if (current.getNext().getKey() > current.getKey()) {
            // Assign the larger key as our new max
            max = current.getNext();
            return maxElement (max);
        }
        else {

        }
    }
    return maxElement (max.getNext());
}

You have to keep track of the current max in your recursion: 您必须在递归中跟踪当前的最大值:

public Node max() {
    return maxAux(root, null);
}

private Node maxAux(Node current, Node currentMax) {
    if(current == null) {
        return currentMax;
    }
    if(currentMax == null || current.getKey() > currentMax.getKey()) {
        return maxAux(current.getNext(), current);   // current is the new max
    } else {
        return maxAux(current.getNext(), currentMax);
    }
}

Here is a fairly elegant solution: 这是一个相当优雅的解决方案:

public Node max(Node node1, Node node2) {
    if (node1 == null || node2 == null)
        return node1;
    else
        return max(node1.getKey() > node2.getKey() ? node1 : node2, node2.getNext());
}

It is called using max(head, head.getNext()) . 使用max(head, head.getNext())调用它。

If you specifically need to avoid passing the current maximum into the method then: 如果您特别需要避免将当前最大值传递给方法,则:

private static Node currentMax;

private Node maxElement(Node node) {
    if (currentMax == null || node == null)
        return currentMax;
    else if (node.getKey() > currentMax.getKey())
        currentMax = node;
    return maxElement(node.getNext());
}

This is called with currentMax = head; 这被称为currentMax = head; then maxElement(head) . 然后是maxElement(head)

private Node head, max;

int maxElement () {

    if (head == null) {
        return -1;
    }
    else {
        Node current = head;
        return maxElement (current);
    }
}

    private int maxElement (Node current) {     

        // If our next node does not exist, our current node has to be max
        if (current.getNext() == null) {
            current = this.max;
            return this.max.getKey();
        }
        //  
        else {  

        if (current.getNext().getKey() > current.getKey()) {
            // Assign the larger key as our new max
            this.max = current.getNext();
            return maxElement (current.getNext());
        }
        else {

        }
    }
    return maxElement (max.getNext());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM