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); 

    }
}
8
  • 2
    Do you know what the static modifier means and how it affects class attributes? Commented Mar 26, 2014 at 22:46
  • also you are not connecting root to any node in else part Commented Mar 26, 2014 at 22:47
  • AvlNode's fields should not be static. Commented Mar 26, 2014 at 22:47
  • Not really. I just know the compiler was mad at me before because things weren't made static. Commented Mar 26, 2014 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. Commented Mar 26, 2014 at 22:50

1 Answer 1

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.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.