Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

JavaWorld Daily Brew

Java AVL tree



Hi i am making an AVL tree and i am having some problems in the search method, because a null exception error is given when the program is ran. Here is my code. Can you pls implement for me the search method so the program will run. Thanks in advance

package testavltree;

import java.io.IOException;
import javax.swing.JOptionPane;

/**
*
* @author Rapapc
*/
public class TestAVLTree {

public static void main(String[] args) throws IOException, Exception {
System.out.println("your inorder traversal is:");
AVLarray process = new AVLarray();
Node nd1 = process.ReadInputFile("E:\\Java files txt\\input.txt", "E:\\Java files txt\\output2.txt");
String input = JOptionPane.showInputDialog(null, "Please select a number to search");
Search sr = new Search();

while (input != null) {
try {
Node newNode = sr.search(nd1.getRoot(), Integer.parseInt(input));
JOptionPane.showMessageDialog(null, "The node you searched for " + input + " is " + newNode.toString());
System.exit(0);
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Please enter a valid number to search next time");
System.exit(0);
}

}

}
}

package testavltree;

public class Node {

private int value;
private Node leftNode;
private Node rightNode;

public Node(int value) {
this.value = value;
}

public Node() {
}

public Node(int value, Node leftNode, Node rightNode) {
this.value = value;
this.leftNode = leftNode;
this.rightNode = rightNode;
}

public void setValue(int value) {
this.value = value;
}

public void setLeft(Node leftNode) {
this.leftNode = leftNode;
}

public void setRight(Node rightNode) {
this.rightNode = rightNode;
}

public int getValue() {
return value;
}

public Node getLeftNode() {
return leftNode;
}

public Node getRightNode() {
return rightNode;
}
private Node root;

public Node getRoot() {
return root;
}

public Node insert(int n) {

root = insert(n, root);
root = balance(root);
return root;
}

public Node insert(int x, Node t) {
if (t == null) {
t = new Node(x);
} else if (x < t.getValue()) {
t.setLeft(insert(x, t.getLeftNode()));
} else if (x > t.getValue()) {
t.setRight(insert(x, t.getRightNode()));
}
return t;
}

public int getLeftHeight() {
if (this.leftNode == null) {
return 0;
} else {
return this.getLeftNode().getHeight() + 1;
}

}

public int getRightHeight() {
if (this.rightNode == null) {
return 0;
} else {
return this.getRightNode().getHeight() + 1;
}
}
public int getHeight ()
{
if (this.leftNode == null && this.rightNode== null)
{
return 0;
}
else
{
int leftH = 0;
int rightH = 0;
if (this.leftNode != null)
{
leftH++;
leftH += this.getLeftNode().getHeight();
}
if (this.rightNode != null)
{
rightH++;
rightH += this.getRightNode().getHeight();
}
return Math.max(leftH,rightH);
}
}

public Node balance(Node n) {
if (Math.abs(n.getLeftHeight() - n.getRightHeight()) == 2) {
if (n.rightNode == null) {
if (n.leftNode.leftNode != null) {
return n.LLRotation();
}
if (n.leftNode.rightNode != null) {
return n.LRRotation();
}
}
if (n.leftNode == null) {
if (n.rightNode.rightNode != null) {
return this.RRRotation();
}
if (n.rightNode.leftNode != null) {
return this.RLRotation();
}
}
} else {
if (this.getLeftNode() != null) {
return this.getLeftNode().balance(n);
}
if (this.getRightNode() != null) {
return this.getRightNode().balance(n);
}
}
return this;
}

public Node LLRotation() {
Node temp = this.leftNode;
this.leftNode = null;
temp.rightNode = this;
return temp;
}

public Node RRRotation() {
Node temp = this.rightNode;
this.rightNode = temp.leftNode;
try {
temp.leftNode = (Node) this.clone();
} catch (CloneNotSupportedException ex) {
}
this.value = temp.value;
this.rightNode = temp.rightNode;
this.leftNode = temp.leftNode;
return temp;
}

public Node LRRotation() {
this.leftNode = this.leftNode.RRRotation();
return LLRotation();
}

public Node RLRotation() {
this.rightNode = this.rightNode.RRRotation();
return RRRotation();
}

public static String getAsString(Node n) {
if (n != null) {
return n.toString();
} else {
return "N";
}
}

@Override
public String toString() {
return "(" + getAsString(leftNode) + " " + value + " " + getAsString(rightNode) + ")";

}
}

public class Search {

public Node search(Node root, int key) {

Node n;
Node node = root;
if (node == null) {

n = null;
return n; // missing from tree

} else if (key < node.getValue()) {

return search(node.getLeftNode(), key);

} else if (key > node.getValue()) {

return search(node.getRightNode(), key);

} else {

return new Node(node.getValue(), node.getLeftNode(), node.getRightNode()); // found it
}
}
}
package testavltree;

import java.io.*;
import java.util.ArrayList;
import java.util.ListIterator;

public class AVLarray {

Node nd = new Node();
Search sr = new Search();

// Read the input file.
public Node ReadInputFile(String input_file, String output_file) throws Exception {
String[] piece;

ArrayList pos = new ArrayList ();

File file = new File(output_file);
FileWriter writer;
try {
writer = new FileWriter(file);

FileReader reader;
try {
reader = new FileReader(input_file);
BufferedReader buff = new BufferedReader(reader);
String line = buff.readLine();

Node cleft;
Node cRight;

while (line != null) {

//If the line starts “insert”, get the value of node and insert to tree.
if (line.contains("insert")) {
piece = line.split(" ");
pos.add(piece[1]);
Node c = nd.insert(Integer.parseInt(piece[1]));

} else if (line.contains("show inorder")) {
inOrder(nd.getRoot(), writer);

}

line = buff.readLine();

}//while

ListIterator iter = pos.listIterator();

while (iter.hasNext()) {
Node f = null;
String s = "";
int index;
int key = Integer.parseInt(iter.next().toString());

f = new Node(key, null, null);
Node newNode = sr.search(nd.getRoot(), key);

cleft = newNode.getLeftNode();
cRight = newNode.getRightNode();

if (cleft != null) {
if (pos.contains(Integer.toString(cleft.getValue()))) {
index = pos.indexOf(Integer.toString(cleft.getValue()));
s += Integer.toString(index) + "," + Integer.toString(newNode.getValue());
}
} else {
s = "-," + Integer.toString(newNode.getValue());
}

if (cRight != null) {
if (pos.contains(Integer.toString(cRight.getValue()))) {
index = pos.indexOf(Integer.toString(cRight.getValue()));
s += "," + Integer.toString(index);
}
} else {
s += ",- ";
}

System.out.println(s);

}

ListIterator iters = pos.listIterator();

while (iters.hasNext()) {

System.out.println(Integer.toString(iters.nextIndex()) + iters.next());
}

reader.close();

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
writer.write("Input file Not Found..");
} // open reader
catch (IOException e) {
// TODO Auto-generated catch block
}
writer.close();

} catch (IOException e) {
// TODO Auto-generated catch block
}

return nd;
}
//If the sorting is inorder, last node is the most right node.

public Node findLastForInOrder(Node node) {
if (node.getRightNode() == null) {
return node;
} else {
return findLastForInOrder(node.getRightNode());
}
}

public void inOrder(Node root, FileWriter writer) {
if (root != null) {
inOrder(root.getLeftNode(), writer);
try {
if (findLastForInOrder(nd.getRoot()).getValue() != root.getValue()) {
System.out.print(root.getValue() + ",");
} else {
System.out.print(root.getValue() + "");
}
} catch (Exception e) {
// TODO Auto-generated catch block
}
inOrder(root.getRightNode(), writer);
}
}
}