Kendel Ventonda
Kendel Ventonda

Reputation: 411

Summing elements of binary tree in Java

I'm trying to sum the elements of a binary tree with a recursive and an iterative method. While the recursive one works finde, the iterative gives me an exception.

import java.util.Queue;

public class BinTree {

    public BinNode root;

    public boolean insertNode(BinNode bn) {
        BinNode child=null, parent=null;

        // Knoten suchen, nach welchem eingefuegt wird
        child = root;
        while( child != null) {
            parent = child;
            if (bn.element == child.element)     return false;
            else if (bn.element < child.element) child = child.left;
            else                                 child = child.right;
        }

        // Baum leer?
        if (parent==null)                     root = bn;
        // Einfuegen nach parent, links
        else if (bn.element < parent.element) parent.left = bn;
        // Einfuegen nach parent, rechts
        else                                  parent.right = bn;

        return true;
    }

    public BinNode findNode(int value) {
        BinNode  n = root;

        while (n != null) {
            if (value == n.element)     { return n;    }
            else if (value < n.element) { n = n.left;  }
            else                        { n = n.right; }
        }
        return null;
    }

    public String toString() {
        return root.toString();
    }

        //Max des Gesamtbaumes
        public int max(){
            if(root==null){
                return 0;
            }
            else {
                return root.max();
            }
        }

        //(Iterativ)
        public int max2(){
            //The line that throws out the exception
            Queue q = new LinkedList();
            int sum = 0;
            if(root!=null){
                q.add(root);
            }
            while(!q.isEmpty()){
                BinNode node = (BinNode) q.remove();

                if(node.left == null && node.right == null){
                    sum = sum + node.element;
                }
                else{
                    if(node.left != null){
                        q.add(node.left);
                    }
                }
                if(node.right != null){
                    q.add(node.right);
                }
            }
            return sum;
        }

}

The Queue q = new LinkedList(); in the max2-Method is giving me the Exception: Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - incompatible types: javaapplication34.LinkedList cannot be converted to java.util.Queue

Could anyone help here? Give me a kickstart or a little explanation? I'm very unsure what's the issue in detail.

I didn't add every class here, since most of them are common. But if needed I'll add them.

Upvotes: 0

Views: 546

Answers (2)

Andreas Dolk
Andreas Dolk

Reputation: 114767

We don't know the implementation of you special LinkedList class (only that it does not implement the java.lang.Queue interface), but it may work already if you just say:

LinkedList q = new LinkedList();

(I assume that it is an assignment and that you have to use this special LinkedList for the task)

Upvotes: 0

Joe C
Joe C

Reputation: 15684

It appears that you have defined a class called LinkedList in the same package, and it doesn't implement Queue.

If you want to use a java.util.LinkedList, you should either import or use the entire qualified name.

Upvotes: 3

Related Questions