algorithm - Java pass by value and recursion -


I have a simple code that prints the path of a specific node in a tree. Using my Java string, my implementation is in the form of the following

  // string using the static zero getPathS (node ​​node, string path, int key) {if (node ​​== Null) {return; } And getPathS (node.left, path + + + node data, key); getPathS (node ​​right, path + + + Nodedata, key);}   

Suppose that is in the form of a tree below,

If

  1 343 // will be printed from path to root    

Prefix & lt; integer & gt; path, int key) {if (node ​​== null) {// 1. path = new arreelist & lt; integer & gt; (); path = new arreelist & lt; integer & Gt; (); // 2. or tried path. Clear () - it should clear the path / return path; return tap;} and if (node.data == key) {path.add (node) path of return;} path.add (node.data); return non-null (getPath (node ​​left, path, key), getPath (node. right, path, key))} private listing non -NL (list path 1, list path 2) {if (path1! = Null) return path1; If (path 2! = Null) return path 2; Return tap; } // class node {node left, node right, int data; }; // Code to call GetPath node node = new node (1); Node Left = new node (2); Node.left.left = New node (4); Node Right = new node (34); Node Right. Right = new node (3); System.out.println (getPath (node, new ArrayList (), 3)); In the second implementation, I tried two approaches, when we got the null node, in 1 approach if I put the new ArrayList on the path , Then it prints all the elements. I

  [1, 2, 4, 34, 3]   

If I path.clear () , then this is the only element that can be searched for the last element.

How can we ensure that the ArrayList string works in a recursive form? The problem here is that you failed to call both nonNull ()

. There is an improvement here which takes into account the possibility that we remove the data of the current node, if we fail to find the keys in our children.
  Public Stable List & lt; Integer & gt; GetPath (node ​​node, list & lt; integer & gt; path, int key) {if (node ​​== zero) {return tap; } And if (node.data == key) {path.add (node.data); return path; } Path.add (node.data); // If the path is unchanged then nothing is found in left children (if the gate path (node ​​left, path, key)! = Null gate = gate (node ​​right, path, key)! = Null) // Found in branch or other return path; } // found in no branch, remove our data path. (Overview (path.size) - 1); Return tap; }   

Of course, it seems that we are tampering different lists, but there is only one: the reason for the first time was given in the argument that this is why data is removed from it must be given. You should be clear about your arguments.


A cleaner solution, which emphasizes the fact that there is only one list. Specify all keys from <@> * / ** * {@code node}, if specified key {@code key} from {@link node}, then if the key of the specified node is not found in the key, then List * Guaranteed to be unchanged If the key is found between children, then the specified list * will include new elements (in addition to the old ones). * At the ultimate node * node * to the absolute path * to start * key to close the * ultimate key * to the current path * Return to the right if the key was found between the children of the specified node, Incorrect otherwise * / public stable boolean getPath (node ​​node, list & lt; integer & gt; path, int key) {if (node ​​== faucet) {// reached the leaf, and the key return was not found false; } // Add path path. Add (node.data); // Or is lazy here, we treat everything in the given order // If the gatepath fails on the left children, the path is unchanged and used for the right children (node.data == key || getpath (node.left, path, key) || Gate path (node. Right, path, key)) / key is found in the current node, its left child, or its correct children, come back true; } // found in no branch, remove our data path. (Overview (path.size) - 1); return false; }

Note that I did not use the path.remove (node.data) , because one node with that data may be more pre-installed Will be removed instead of the last.

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -