Sunday, 15 January 2012

java - How to add BNode to binary search tree? -


From our assignment, one question is that we want to write a method in the Betty class. It will take an integer as a parameter, and then add it to the tree so I did this:

  add the public zeros (int v) {if (root == null) {root = New BNode (v); } And {add (v, root); }} Add Public Zero (Int v, Beanode branch) {if (v == branch.getVal ()) {return; } If (v & lt; branch.getVal ()) {if (branch.getLeft () == faucet) {branch.setLeft (new BNode (v)); } Else {add (v, branch.getLeft ()); }} Else {if (branch.getRight () == faucet) {branch.setRight (new BNode (v)); } Else {add (v, branch.getRight ()); }}}   

But I wonder how do you add when the parameter of the method is just a biodegradable? Is it just:

  public zero addition (betode B) {add (BNode.getVal ()); }   

Is there no more efficient way to do this? Any help would be appreciated.

Yes, replace your parameter in another way: do not do your methods in a int but expect a bnode . Then change it:

  Add public zeros (int v, bnoid branch)   

to

  add public zeros (   

and replace:

  Add public zeros (int v)   

to

  Create a separate method:  
  Public Zero Addition (int b) {add (new)   

Binode (B));}

In this way you are avoiding creating an additional object in memory.

No comments:

Post a Comment