Hello every one.
Basically i am trying to construct a binary tree. but there's a problem. When i run the program, there is an error. somewhere in the treeInsert method(). I am having difficulty because the root is not node, it is an integer value. then the left and right subtree is a binary tree. before, i used nodes to construct binary trees.. i can't root.left. if sumone could just look and see what is the problem.
package assignments;
public class BinaryTree {
int root;
BinaryTree leftSubTree;
BinaryTree rightSubTree;
static BinaryTree myTree;
BinaryTree(){
root = 0;
leftSubTree = null;
rightSubTree = null;
}
BinaryTree(Integer n){
root = n;
}
BinaryTree(Integer n, BinaryTree left, BinaryTree right){
root = n;
leftSubTree = left;
rightSubTree = right;
}
public int getRoot(){
return(root);
}
public BinaryTree getLeftChild(){
return(leftSubTree);
}
public BinaryTree getRightChild(){
return (rightSubTree);
}
public int getHeight(){
int left = -1;
int right = -1;
if( leftSubTree != null ){
left = leftSubTree.getHeight();
}
if( rightSubTree != null ){
right = rightSubTree.getHeight();
}
if( left > right ){
return left + 1;
}return right + 1;
}
public boolean isEmpty(){
boolean ai = false;
if(root == 0){
return true;
}return ai;
}
public int countNodes(){
int count = 1;
if (this.getLeftChild() != null){
count += this.getLeftChild().countNodes();
}
if (this.getRightChild() != null){
count += this.getRightChild().countNodes();
}return count;
}
public int countLeaves(BinaryTree t){
int count = 0;
if(root == 0){
countLeaves(leftSubTree);
if(leftSubTree.root != 0 && rightSubTree.root != 0){
count++;
}
countLeaves(rightSubTree);
}return count;
}
public void destroy(){
}
public void preorderTraversal(){
if (root != 0) {
System.out.println(root);
new BinaryTree(leftSubTree.root).preorderTraversal();
new BinaryTree(rightSubTree.root).preorderTraversal();
}
}
public void inorderTraversal(){
if (root != 0) {
new BinaryTree(leftSubTree.root).inorderTraversal();
System.out.println(root);
new BinaryTree(rightSubTree.root).inorderTraversal();
}
}
public void postorderTraversal(){
if (root != 0) {
new BinaryTree(leftSubTree.root).postorderTraversal();
new BinaryTree(rightSubTree.root).postorderTraversal();
System.out.println(root);
}
}
public void levelorderTraversal(){
}
public void treeInsert(BinaryTree t, int newItem) {
// Add the item to the binary sort tree to which the parameter
// "root" refers. Note that root is passed by reference since
// its value can change in the case where the tree is empty.
if ( t.root == 0 ) {
// The tree is empty. Set root to point to a new node containing
// the new item. This becomes the only node in the tree.
t = new BinaryTree(newItem);
// NOTE: The left and right subtrees of root
// are automatically set to NULL by the constructor.
// This is important!
return;
} else if ( newItem < t.root) {
treeInsert( leftSubTree, newItem );
}else {
treeInsert(rightSubTree, newItem );
}
}
public static void main(String args[]){
myTree = new BinaryTree();
myTree.insert();
System.out.println("Height of tree is: "+ myTree.getHeight());
System.out.println("Number of nodes: "+ myTree.countNodes());
System.out.println("Number of leaves: "+ myTree.countLeaves(myTree));
System.out.println("Preorder: ");
myTree.preorderTraversal();
System.out.println("Inorder: ");
myTree.inorderTraversal();
System.out.println("PostOrder: ");
myTree.postorderTraversal();
System.out.println("LevelOrder: ");
myTree.levelorderTraversal();
}
public void insert(){
int arrNum[] = {50,17,76,9,23,54,14,19,72,12,67};
for(int i=0; i<=10; i++){
treeInsert(arrNum[i]);
}
}
}