Ok, here is my code. I am trying to practice with Binary Trees. Why can't my size() method access the root node of my binary tree?
Also my root.left, and root.right assignments do not work.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import java.lang.*;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
System.out.println(bTree.size());
}
}
//Building Binary Trees
class bTree {
static class Node { //remember to initilize a root
String value;
Node left, right;
Node root = new Node("ROOT");
Node lefty = new Node("LEFT0");
Node righty = new Node("RIGHT0");
root.left = lefty;
root.right = righty;
Node(String value, Node left, Node right) {
this.value = value;
this.left = left;
this.right = right;
}
Node(String value) //THIS IS A SIBLING CONSTRUCTOR
{
this(value, null, null);
}
}
public static int size() //Public | sibling constructor
{
System.out.println("Debug0");
System.out.println(root.value);
return size(root);
}
//This method will find the size of a node
private static int size(Node r) //using recursion
{
if (r == null) {
return 0;
} else {
return 1 + (size(r.left) + size(r.right));
}
}
}
Any help would be much appreciated, I have a final tomorrow on this information!
Sam