0

I am trying to build an AVL tree, but seem to be having a problem with nodes. I try to make a new node and it changes all the values of other nodes to whatever I was giving the new node.

//AVL.java
import java.util.*;
import java.io.*;

public class AVL{
    static AvlNode root;

   public static void tree(int[] list){
      for(int i=0; i<list.length; i++){
         insertPrep(list[i]);
      }
   }


   public static void insertPrep(int data){
        //if not null insert data into existing root node otherwise make new node using data
        if (root==null){root = new AvlNode(data);}
        else {
             System.out.println("inPr else");
             System.out.println(root.key +  " & " + data);
             AvlNode newNode = new AvlNode(data);
             System.out.println(root.key +  " & " + newNode.key);

      }
    }

   //where tree is made and stored
    static class AvlNode{
        static int key, height; //data for input numbers and height for height of nodes to keep balance
        static AvlNode left, right; //left for left side of tree and right for right side of tree    

        AvlNode(int data){
             key = data;
       }

   }
}

And here is what I am using the above for: //Tree.java import java.io.; import java.util.;

public class Tree{

   public static void main(String[] args){

      int n = 10; //numbers to be in array
      int a[] = new int[n]; //first array

      for (int i=0; i<n; i++){
         a[i] = i+1; //insert #'s 1-n; smallest to largest

      }

      AVL.tree(a); 

    }
}
Cat
  • 69
  • 1
  • 8
  • 2
    Do you know what the `static` modifier means and how it affects class attributes? – BackSlash Mar 26 '14 at 22:46
  • also you are not connecting root to any node in else part – jmj Mar 26 '14 at 22:47
  • `AvlNode`'s fields should not be static. – Louis Wasserman Mar 26 '14 at 22:47
  • Not really. I just know the compiler was mad at me before because things weren't made static. – Cat Mar 26 '14 at 22:48
  • 2
    Right. Gotta watch those emotional compilers :-). Static means that something doesn't belong to a particular instance of a class. To use non-static methods or fields, you need to create an object. Doing that would have been a better idea than making everything static. – Dawood ibn Kareem Mar 26 '14 at 22:50
  • possible duplicate of [In laymans terms, what does 'static' mean in Java?](http://stackoverflow.com/questions/2649213/in-laymans-terms-what-does-static-mean-in-java) – Bernhard Barker Mar 26 '14 at 22:54
  • Don't change things to make your code compile without knowing why you're changing it. Googling the error should give you a decent idea of the problem, if not, we'd happily help by explaining why you're getting the error and the steps required to fix it. – Bernhard Barker Mar 26 '14 at 22:57
  • "I try to make a new node and it changes all the values of other nodes to whatever I was giving the new node." It looks like you *do* know what static means ;) – aliteralmind Mar 26 '14 at 22:57

1 Answers1

1

Sorry for being rather brief with my earlier comment. Your problem is these two lines inside AvlNode.

static int key, height; 
static AvlNode left, right;

That implies that every AvlNode has the same value, for each of these four fields. That would mean that there's only one key, and one height for every node of the tree; and only one left descendant and only one right descendant of every node of the tree. You really need to remove those static modifiers, so that every AvlNode instance can have its own copies of these four fields.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110