Note that there are some explanatory texts on larger screens.

plurals
  1. POLogic Error with an AVL tree
    primarykey
    data
    text
    <p>I am tryintto implement an AVL tree using JAVA. I have followed this code as an example: </p> <p><a href="http://knowledgejunk.net/2010/11/09/avltree-java-source-code/" rel="nofollow">http://knowledgejunk.net/2010/11/09/avltree-java-source-code/</a></p> <p>But mine is a bit different, I want to read integers from a file and put them into an AVL tree. The AVL tree is to be constructed number by number and balanced every number. The problem is that '0' is being put in and when I traverse the tree (inorder traversal), I am not getting the numbers in order. To ensure what the program is adding, I have made it to print every number it is inputting, thus:</p> <p>FIRST = 9 Inputting: 8 Inputting: 55 Inputting: 44 Inputting: 34 Inputting: 43 Inputting: 76 Inputting: 67 Inputting: 56 Inputting: 65 Inputting: 45 Inputting: 99 Inputting: 88 Inputting: 44 Inputting: 12 Inputting: 21 Inputting: 54 Inputting: 58 Inputting: 77 Inputting: 46 Inputting: 66 Inputting: 23 Inputting: 33 Inputting: 40 Inputting: 17 Inputting: 18 Inputting: 4 Inputting: 5 Inputting: 2 Inputting: 59 Inputting: 90 Inputting: 80 Inputting: 50 Inputting: 60 Inputting: 20 Inputting: 26</p> <p>I have also coded the program to give me the Min / Max Values and the Inorder traversal, here is my result:</p> <p>Min is 0. Max is 99 Min is 0. Max is 99 Min is 0. Max is 99 Min is 0. Max is 99 Min is 0. Max is 99 Min is 0. Max is 99 Min is 0. Max is 99 Min is 0. Max is 99 Min is 0. Max is 99 Min is 0. Max is 99 Min is 0. Max is 99 Min is 0. Max is 99 Min is 0. Max is 99 Min is 0. Max is 99 Min is 0. Max is 99 Min is 0. Max is 99 Min is 0. Max is 99 Min is 0. Max is 99 Min is 0. Max is 99 Min is 0. Max is 99 Min is 0. Max is 99 Min is 0. Max is 99 Min is 0. Max is 99 Min is 0. Max is 99 Min is 0. Max is 99 Min is 0. Max is 99 Min is 0. Max is 99 Min is 0. Max is 99 Min is 0. Max is 99 Min is 0. Max is 99 Min is 0. Max is 99 Min is 0. Max is 99 Min is 0. Max is 99 Min is 0. Max is 99</p> <pre><code>0, 0, 0, 43, 44, 0, 12, 21, 44, 45, 54, 0, 2, 4, 5, 17, 18, 20, 23, 26, 33, 40, 46, 50, 58, 59, 60, 65, 66, 67, 76, 77, 80, 88, 90, 99, </code></pre> <p>What's strange is that they are being sorted in a different way, and I don't know from where 0 is coming from!</p> <p>Here is my traversal function code, and insert:</p> <pre><code>public Node traverse(Node n){ //INORDER Node result = new Node(); if(n != null){ if(n.getLeft() != null){ result = traverse(n.getLeft()); } result = n; System.out.print(result.getValue() + ", "); if(n.getRight() != null){ result = traverse(n.getRight()); } } return result; } public Node insert(int value, Node x){ if(x == null){ x = new Node(value); } else if(value &lt; x.getValue()){ x.addLeft(insert(value,x.getLeft())); if(getHeight(x.getLeft())-getHeight(x.getRight())==2){ if(value &lt; x.getLeft().getValue()){ x = sRotwLeft(x); }else{ x = dRotwLeft(x); } } } else if(value &gt; x.getValue()){ x.addRight(insert(value,x.getRight())); if(getHeight(x.getRight())- getHeight(x.getLeft()) ==2){ if(value &gt; x.getRight().getValue()){ x = sRotwRight(x); }else{ x = dRotwRight(x); } } } </code></pre> <p>sRotwRight stands for Single Rotate with right, for example.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload