0

I don't know how to traverse a given binary tree recursively, just by using the following methodes of the tree. For example every TreeNode has an int Value and a getter TreeNode.getValue() and now i want to find the node with the maximum value in the tree. Note that a tree has only a pointer to its currentNode and u can move to the parent, right, left or root node.

My Question:

Tree tree;

public int findMaxValue() {
    //how to implement this as recursive function
}

The tree class:

/**
 * The abstract class for a tree.
 * @author JK, KM
 */
public abstract class Tree {

    /**
     * Moves to the left child of the current node
     * 
     * @return true if left child exists and the move was successful; otherwise
     *         false
     */
    public abstract boolean moveToLeftNode();

    /**
     * Moves to the right child of the current node
     * 
     * @return true if right child exists and the move was successful; otherwise
     *         false
     */
    public abstract boolean moveToRightNode();

    /**
     * Moves to the parent of the current node
     * 
     * @return true if parent exists and the move was successful; otherwise
     *         false
     */
    public abstract boolean moveToParentNode();

    /**
     * @return true if left child exists; otherwise false
     */
    public abstract boolean hasLeftNode();

    /**
     * @return true if right child exists; otherwise false
     */
    public abstract boolean hasRightNode();

    /**
     * @return true if parent exists; otherwise false
     */
    public abstract boolean hasParentNode();

    /**
     * Sets the left child of the current node
     * 
     * @return true if successful; otherwise false (no root set)
     * 
     */
    public abstract boolean setLeftNode(TreeNode node);

    /**
     * Sets the right child of the current node
     * 
     * @return true if successful; otherwise false (no root set)
     * 
     */
    public abstract boolean setRightNode(TreeNode node);

    /**
     * Sets the current node. If the tree is empty, sets the root.
     * 
     */
    public abstract void setCurrentNode(TreeNode node);

    /**
     * @return the current node or null if the tree is empty
     */
    public abstract TreeNode getCurrentNode();

    /**
     * moves to the root node of this tree
     * 
     * @return true if there's a root; otherwise false
     */
    public abstract boolean moveToRoot();

    /**
     * clears the whole tree, which includes deleting all nodes and the root
     * node
     */
    public abstract void clearTree();
2
  • i would recommend to have a look at some data structure book. How about Tannenbaum ? Commented Jun 4, 2011 at 8:30
  • homework more or less - i solved this problem by creating a sequential list of the tree and iterate over each node. unfortunately we have to use a recursive method. Commented Jun 4, 2011 at 8:40

1 Answer 1

2

You can traverse a binary tree recursively like this:

public int findMaxValue() {
    return max(this.node.getValue(),
               this.getLeftChild().findMaxValue(),
               this.getRightChild().findMaxValue());
}

Implementing this given your Tree interface and checking for leaf nodes is left as an exercise.

Sign up to request clarification or add additional context in comments.

4 Comments

Ok, but how can i solve the problem, if a left and a right node exist? I can either move the tree to the left node or to the right node.
@Chris You have a method called hasLeftNode() and a method called hasRightNode() defined in the interface, use those to check if you can move to the left or right.
The datamodel doesn't offer any getters for the children. If i want to get the left node, i must use something like this: tree.moveToLeftNode; TreeNode node = tree.getCurrentNode; tree.moveToParentNode
@Chris exactly, why don't you try to implement it like that?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.