How to implement a generic in-order traversal in java binary tree?

瘦欲@ 提交于 2021-02-17 05:14:25

问题


How can I create an in-order traversal for my Binary Tree class. I have looked and tried other examples out there but cant get seem to get anything to work for me.

Below is what I have so far for my in-order traversal method:

public void inOrder(TreeNode<T> root) {
    if(root !=  null) {
        inOrder(root.left());
        //Visit the node by Printing the node data  
        System.out.printf("%d ",root.value());
        inOrder(root.right());
    }
}

public static void main(String[] args) {
    BinaryTree<Integer> tree = new BinaryTree<Integer>();
    tree.insert(1);
    tree.insert(2);
    tree.insert(3);
    tree.insert(4);
    tree.insert(5);

    inOrder(tree.root);
}

But I seem to be getting error saying The method inOrder(TreeNode<T>) in the type BinaryTree<T> is not applicable for the arguments (BTree<T>).

Below are my classes: Binary Tree class

public class BinaryTree<T extends Comparable<? super T>> implements BTree<T> {

    private TreeNode<T> root;

    /**
     * Insert method. Insert a value in the binary tree.
     */
    @Override
    public void insert(T value) {
        if(root == null) { //if tree is empty insert value at root
            root = new TreeNode<T>(value);
        }else if(value.compareTo(value()) < 0) { //insert value to left as its smaller than the root value
            root.left().insert(value);
        }else{ //insert value to right as its greater than the root value
            root.right().insert(value);
        }

    }

    /**
     * Returns the value of a node in the tree.
     */
    @Override
    public T value() {
        return root.value();
    }

    /**
     * Returns the left node in the tree.
     */
    @Override
    public BTree<T> left() {
        return root.left();
    }

    /**
     * Returns the right node in the tree.
     */
    @Override
    public BTree<T> right() {
        return root.right();    
    }
}

TreeNode class

public class TreeNode<T extends Comparable<? super T>> {
    T value;
    BTree<T> left, right;

    public TreeNode(T value) {
        this.value = value;
        left = new BinaryTree<T>();
        right = new BinaryTree<T>();
    }

    public T value() {
        return value;
    }

    public BTree<T> left() {
        return left;
    }

    public BTree<T> right() {
        return right;
    }
}

Binary Tree Interface

public interface BTree<T extends Comparable<? super T>> {
    public void insert(T value);

    public T value();

    public BTree<T> left();

    public BTree<T> right();

}

回答1:


Your problem is that in the recursive call to the inOrder() function you pass in the BTree objects instead of the expected TreeNode object:

public void inOrder(TreeNode<T> root) {
    if(root !=  null) {
        inOrder(root.left()); //Passing in a BTree<T> object instead of TreeNode<T> 
        System.out.printf("%d ",root.value());
        inOrder(root.right()); //Passing in a BTree<T> object instead of TreeNode<T>
    }
}

You should decide to either use the left/right values stored in your TreeNode object as type TreeNode too or to cast the result of accessing the left/right values to TreeNode objects in your inOrder() function.



来源:https://stackoverflow.com/questions/49439565/how-to-implement-a-generic-in-order-traversal-in-java-binary-tree

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