java - Binary Search Tree having issues with recognising when making root node -


trying make root in bst such:

if (currentnode == null) {             currentnode = new binarynode(newint);             system.out.println(currentnode);             system.out.println(newint);             //system.out.println(newint.getvalue());             system.out.println("node null, made root");          }else{ 

the println there debugging. i'm having issues output:

binarynode@7004ba66 4 node null, made root binarynode@4669b7fe 6 node null, made root binarynode@46aea8cf 1 node null, made root binarynode@74ccd249 3 node null, made root binarynode@3301f287 2 node null, made root binarynode@44d9973a 8 node null, made root binarynode@29578426 7   node null, made root binarynode@30a4effe 5 node null, made root binarynode@1c8825a5 9 node null, made root 

which makes me think isn't recognising (currentnode == null) should. ideas why?

full pastebin: here

any appreciated :)

the problem when assign currentnode, root not assigned.

java passes variables value, meaning copy of value or reference gets passed method. in case, currentnode, formal parameter of insertnode method, passed copy of root field returned getroot method.

to fix this, should split insertnode method in two:

public void insert(int newint); 

and

private binarynode insert(int newint, binarynode node); 

the public method should used without getroot parameter (the users of class inserting in tree should never need pass root, otherwise able break tree passing node in middle numbers should go in different branch).

the private method should return old or new node. should using this:

public void insert(int newint) {     root = insert(newint, root); } 

the method should return new node if node passed in null. when node not null, method should return node passed in.

as far outputlist problem goes, should use stringbuffer instead of string construct output. unlike string immutable, stringbuilder mutable. lets change string inside (use append instead of +=).


Comments