Note that there are some explanatory texts on larger screens.

plurals
  1. POimplementing binary search tree insert
    primarykey
    data
    text
    <p>I'm trying to write code for a binary search tree, the first method I'm working on is the add (insert) method. The root seems to insert properly, but I'm getting null pointer exception when adding the second node. I'll indicate the exact problem spot in my code with comments. </p> <p>If you can see how to fix the bugs, or let me know if my overall logic is flawed it would be incredibly helpful.-- I will mention that this is for school, so I'm not looking to make a really impressive model...most of my layout choices simply reflect the way we've been working in class. Also, method names were selected by the teacher and should stay the same. Feel free to edit the formatting, had a little trouble.</p> <h1>BINARY TREE CLASS</h1> <pre><code>public class BinarySearchTree { private static Node root; public BinarySearchTree() { root = null; } public static void Add (Node newNode) { Node k = root; if (root == null)//-----------------IF TREE IS EMPTY ----------------- { root = newNode; } else // -------TREE IS NOT EMPTY -------- { if (newNode.value &gt; k.value) //-------NEW NODE IS LARGER THAN ROOT--------- { boolean searching = true; while(searching) // SEARCH UNTIL K HAS A LARGER VALUE { //***CODE FAILS HERE**** if(k.value &gt; newNode.value || k == null) { searching = false; } else {k = k.rightChild; } } if ( k == null) { k = newNode;} else if (k.leftChild == null){ k.leftChild = newNode;} else { Node temp = k.leftChild; k.leftChild = newNode; newNode = k.leftChild; if(temp.value &gt; newNode.value ) { newNode.rightChild = temp; } else { newNode.leftChild = temp; } } } if (newNode.value &lt; k.value) //-----IF NEW NODE IS SMALLER THAN ROOT--- { boolean searching = true; while(searching) // ----SEARCH UNTIL K HAS SMALLER VALUE {// **** CODE WILL PROBABLY FAIL HERE TOO *** if(k.value &lt; newNode.value || k == null) {searching = false;} else {k = k.leftChild;} } if ( k == null) { k = newNode;} else if (k.rightChild == null){ k.rightChild = newNode;} else { Node temp = k.rightChild; k.rightChild = newNode; newNode = k.rightChild; if(temp.value &gt; newNode.value ) { newNode.rightChild = temp; } else { newNode.leftChild = temp; } } } }} // sorry having formatting issues } </code></pre> <h1>NODE CLASS</h1> <pre><code>public class Node { int value; Node leftChild; Node rightChild; public Node (int VALUE) { value = VALUE; } } </code></pre> <h1>TEST APPLICATION</h1> <pre><code>public class TestIT { public static void main(String[] args) { BinarySearchTree tree1 = new BinarySearchTree(); Node five = new Node(5); Node six = new Node(6); tree1.Add(five); tree1.Add(six); System.out.println("five value: " + five.value); System.out.println("five right: " + five.rightChild.value); } } </code></pre>
    singulars
    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.
 

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