Implementing Java Tree Methods
<p>I have to implement recursive methods for a binary tree and wanted to see if the methods I wrote are correctly implemented, as I cannot test them. These are not actual methods. I just need to write the pseudocode of the algorithm.</p>

<p>a) Count number of nodes in a Tree</p>

<pre><code>countNodes(TreeNode root){
    if(root == null)
        return 0;
    else{
        TreeNode left = root.getLeftChild();
        TreeNode right = root.getRightChild();
        return (countNodes(left)+countNodes(right)) + 1;
    }
}
</code></pre>

<p>b) Compute the height of a tree</p>

<pre><code>height(TreeNode root){
    if(root == null)
        return 0;
    else{
        return Math.max(height(root.getLeftChild()), height(root.getRightChild()) +1;
    }
}
</code></pre>

<p>c) Find the maximum element</p>

<pre><code>maxElem(TreeNode root){
    if(root == null)
        return 0;
    else{
        int temp = 0;
        temp = Math.max(maxElem(root.getLeftChild()), maxElem(root.getRightChild()));
        return Math.max(root.getValue, temp);
    }
}
</code></pre>

<p>d) Find the sum of the elements</p>

<pre><code>sum(TreeNode root){
    if(root == null)
        return 0;
    else{
        return (sum(root.getLeftChild()) + sum(root.getRightChild())) + root.getValue();
    }
}
</code></pre>

<p>e) Find the average of the Elements</p>

<pre><code>average(TreeNode root){
    int sum = sum(root);
    int elems = countNodes(root);
    return sum/elems;
}
</code></pre>

<p>f) Find a Specific Item</p>

<pre><code>search(int i, TreeNode root){
    if(root == null)
        return false;
    else if(root.getValue == i)
        return true;
    else{ 
        search(i, root.getLeftChild);
        search(i, root.getRightChild);
    }
}
</code></pre>

<p>g) Determine whether an item is an ancestor of another</p>

<pre><code>isAncestor(TreeNode p, NodeNode c){
    if(p==null)
        return false;
    else
        return (c in p.getLeftChild() || c in p.getRightChild())
}
</code></pre>

<p>h) Determine the highest level that is full</p>

<pre><code>maxFull(TreeNode root)
    if(root == null)
        return 0;
    else{
        return(numNodes in level h == 2^h - 1)
    }
}
</code></pre>
