java - Inserting a node in BST -


I am trying to insert nodes in my custom BST. For the first time called the insertData method, the new node is inserted correctly as the root. The problem occurs in second and subsequent calls.

Below is my code:

1. node class =

  package ishan.trees.tree; Class node comparative & lt; Node & gt; {Private entity data; Public int getData () {return data; } Public Zero Set Data (Int Data) {this.data = data; } Public node getLeftChild () {return child left; } Public Zero set lift shield (node ​​left child) {this.leftChild = leftChild; } Public node getRightChild () {returns rightChild; } Public Zero Setwrights (Node RightChild) {this.rightChild = rightChild; } Private node leftChild; Private node daimbild; Public node (int data, node left child, node child right) {this.data = data; This.leftChild = leftChild; This.rightChild = rightChild; } To compare override public int (node ​​o) {if (o.getData ()> gt; this.data) return -1; If (o.getdata ()   

tree class =

  package ishan.trees.tree; Public square tree {private node root = zero; Public node getRoot () {Return Route; } Public Zero insert data (int data) {node node = new node (data, empty, empty); (Node, this.root) included; } Enter the private node (relative to the lieutenant; node> node, node root 1) {if (root1 == null) {// Insert the first element, that is, root it. Root = new node (node) node) .getData (), faucet, zero); } And if (node.compareTo (root1) & lt; 0) {root1.setLeftChild (insert (node, root 1.getLeftChild ())); } Else if (node.compareTo (root1)> 0) {root1.setLeftChild (insert (node, root 1.getRightChild ())); } Return root1; }}   

3.man class =

package ishan.trees.usage; Import ishan.trees.tree.Tree; Public class usage {public static zero principal (string A []) {tree tree = new tree (); Tree.insertData (10); // --------- 1 tree.interta (15); // --------- 2 trees. Insert data (9); // --------- 3 trees. Instructions (4); // --------- 4}}

When I debug another call, something like this happens:

Insert container (15) {Insert (15,10)}

which calls the insert method ---->

Insert (15, empty)

I am getting it empty all the time and it is in place of the root node in the node.

I do not know why during the call, the root 1 reference is zero and is not pointing to my root?

More information:

Insert to insert () (during the call from) () to insert data during my second call (15), I I make a call (15, this.root) -> insert (node, root1). But this root 1 reference is faucet but when I inspect it, it is referring to the correct root node.

Thank you!

Insert 10.

When you insert the first element If so, then this AP I << p>

Insert according to your code creates a new route for you and gives it a value of 10,

Now the second entry makes this interesting

  Insert container (15); Insert (node, root) // Here is the root of your work, originally started Is done when u gets inserted / it goes to the other side if inserted inside API route 1 .SettyBold (insert (node, root 1.getRigh TChild ()); // Now look, this API actually reincludes, the value of the new node in the cose is the maximum root value /, how this next stack trace appears, such as the original correct child tap was inserted (Node, empty); // Supervisor the second argument is again empty   

will now finish to make the route again according to your inclusion code (root 1 argument is zero, first condition is executed), Cancel the already defined route. This is what causes your problem. You are overriding your route repeatedly.

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -