CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Java AVL bug

  1. #1
    Join Date
    Jan 2012
    Posts
    39

    Angry Java AVL bug

    Hi i'm writing this code for an AVL tree. The program is working fine but it is not displaying the left and right child. Can you help me in fixing the problem. Here is my code:
    package testavltree;

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

    public class AVLProcess {

    Search sr = new Search();
    AVLTree ar = new AVLTree();

    public AVLProcess() {
    }

    public AVLTree ReadInputFile(String input_file, String output_file) throws Exception {
    String[] piece;

    ArrayList<String> pos = new ArrayList<String>();
    File file = new File(output_file);
    FileWriter writer;

    int currentCounter = 0;

    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;


    Node firstValue = null;
    Node secondValue = null;


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

    if (currentCounter == 0) {
    firstValue = new Node(0);
    secondValue = new Node(Integer.parseInt(piece[1]));
    } else {
    firstValue = secondValue;
    secondValue = new Node(Integer.parseInt(piece[1]));
    }

    ar.AvlInsert(firstValue, secondValue);

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

    }


    line = buff.readLine();
    currentCounter++;

    }

    ar.AvlBalanceTree(ar.getRoot());

    ListIterator iter = pos.listIterator();

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

    f = new Node(key, null, null);
    Node newNode = sr.search(ar.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) {

    writer.write("Input file Not Found..");
    }
    catch (IOException e) {
    e.printStackTrace();
    }
    writer.close();

    } catch (IOException e) {
    e.printStackTrace();
    }

    return ar;
    }
    Insert method:
    public void AvlInsert(Node nd, Node newNode) {
    if (newNode.value < nd.value) {
    if (nd.leftNode == null) {
    nd.leftNode = newNode;
    } else {
    AvlInsert(nd.leftNode, newNode);
    }
    } else {
    if (nd.rightNode == null) {
    nd.rightNode = newNode;
    } else {
    AvlInsert(nd.rightNode, newNode);
    }
    }

    AvlBalanceTree(nd);
    }

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Java AVL bug

    Please edit your post and wrap your code in code tags for better displaying.
    Norm

  3. #3
    Join Date
    Jan 2012
    Posts
    39

    Re: Java AVL bug

    Solved it thanks anyways

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured