java - Inserting a node in BST -



java - Inserting a node in BST -

i trying insert nodes in custom bst.the first time insertdata method called , new node correctly inserted root.the problem occuring in sec , subsequent calls.

below code :

1.the node class =

package ishan.trees.tree; class node implements comparable<node> { private int data; public int getdata() { homecoming data; } public void setdata(int data) { this.data = data; } public node getleftchild() { homecoming leftchild; } public void setleftchild(node leftchild) { this.leftchild = leftchild; } public node getrightchild() { homecoming rightchild; } public void setrightchild(node rightchild) { this.rightchild = rightchild; } private node leftchild; private node rightchild; public node(int data,node leftchild,node rightchild) { this.data=data; this.leftchild=leftchild; this.rightchild=rightchild; } @override public int compareto(node o) { if(o.getdata() > this.data) homecoming -1; if(o.getdata() < this.data) homecoming 1; homecoming 0; } }

the tree class =

package ishan.trees.tree; public class tree { private node root=null; public node getroot() { homecoming root; } public void insertdata(int data) { node node=new node(data,null,null); insert(node,this.root); } private node insert(comparable<node> node,node root1) { if(root1==null) {//insert first element ie root this.root=new node(((node)node).getdata(),null,null); } else if(node.compareto(root1) <0) { root1.setleftchild(insert(node,root1.getleftchild())); } else if(node.compareto(root1) >0) { root1.setleftchild(insert(node,root1.getrightchild())); } homecoming root1; } }

3.main class =

package ishan.trees.usage;

import ishan.trees.tree.tree; public class usuage { public static void main(string a[]) { tree tree=new tree(); tree.insertdata(10); //---------1 tree.insertdata(15); //---------2 tree.insertdata(9); //---------3 tree.insertdata(4); //---------4 } }

when debug sec phone call this:

insertdata(15){ insert(15,10) }

which makes phone call insert method ---->

insert(15,null)

i null every time , results in current node replacing root node.

i cant figure out why during phone call , root1 reference null , not pointing root?

more info :

its during phone call insertdata() insert() . during sec phone call insertdata(15) , create phone call insert(15,this.root) -->insert(node,root1) . root1 reference turns out null.but when inspect this.root referring right root node..

thanks!

alright here dry run code,

inserting 10.

when insert first element, api insert creates new root per code , sets value 10,

now sec insertion makes interesting, watch happenes

stacktrace

insertdata(15); insert(node,root) // here root actuall root, initialized when u inserted first // goes lastly else if within insert api root1.setrightchild(insert(node,root1.getrightchild())); // see now, apis calls insert again, coz new node value greater root value // how next stack trace like, root right kid null insert(node,null); // observer sec argument null 1 time again

now per insert code end creating root again(root1 argument null, first status executed), discarding defined root. causing issue overriding root 1 time again , again.

java binary-search-tree

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -