Crossing a binary tree recursively

I am hopelessly lost when it comes to recursive functions. I need to create a recursive function to move a binary tree and insert a new node between specific values. Do I need to reinstall my trace function and change it in any other function in which I use it? Anyone please rate the move function?

I think my passing code is ok.

Node traverse (Node currentNode){ if (!currentNode.left.equals(null)){ traverse (currentNode.left); return currentNode.left; } if (!currentNode.right.equals(null)){ traverse (currentNode.right); return currentNode.right; } return currentNode; } 
+7
source share
3 answers

When it comes to binary trees, there are several different types of traversals that can be done recursively. They are written in the order they are referenced, then visited (L = left child, V = visit node, R = right child).

  • Bypass OK (LVR)
  • Reverse Order Bypass (RVL)
  • Order Bypass (VLR)
  • Bypass in order (LRV)

Your code seems to follow a post-order workaround, but you get a few things mixed up. First, node is what you want to go through; data is what you want to visit. Secondly, you have no reason to return the node itself, as it is implemented. Your code does not allow the condition to say: "I am looking for this specific data, do you have it Mr. Node @ 0xdeadbeef? ', Which will be found with some definition of an additional search parameter.

The BST Academic Bypass prints only nodes. If you want to add a search function, this is another parameter, as well as an additional check of the correct node.

Here is a snippet:

 // Academic public void traverse (Node root){ // Each child of a tree is a root of its subtree. if (root.left != null){ traverse (root.left); } System.out.println(root.data); if (root.right != null){ traverse (root.right); } } // Search with a valid node returned, assuming int public Node traverse (Node root, int data){ // What data are you looking for again? if(root.data == data) { return root; } if (root.left != null){ return traverse (root.left, data); } if (root.right != null){ return traverse (root.right, data); } return null; } 
+13
source

It looks like you are looking at the pre-order methodology, but I'm a little skeptical about what exactly you want to accomplish, without actually comparing your current node with some base value, determining that u has reached the destination of uranium. I would suggest laying a simple tree and visualizing steps. Then try to put this in code.

+1
source

A recursive function returns the value of itself with a modified parameter or a termination (exit) condition. e.g. Factorial:

 int factorial( int param ) { if ( param > 1 ) { return param * factorial( param -1 ); } else { return 1; } } 

In your code, you call "traverse", but then do nothing with the result ... When your recursive function ends, your final return will be the first left child if it exists, otherwise the first right child if it exists, otherwise the root node.

Please give more detailed information on why you need to cross the tree (also not sure what you mean by “copy of the function and its modification in every other function”, the whole idea of ​​the function is to call a lot)

0
source

All Articles