In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to write a good java recursive function". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to write java recursive functions".
1 the three elements of recursion
To write recursion is to write the realization of three elements, which are function, boundary and recursive formula respectively. At the beginning, as long as you remember to write it this way, after writing a few algorithms, you will gradually understand why you want to do so.
1.1 Recursive first element-function
To make clear what your function is for, what the input parameter of the function should be, and what the return value is, these three questions start with what the function is for. You can define a function f () that assumes that each step of the recursion has been implemented, and then to clarify what the implementation has done, what the input parameter at least wants, and the return value and parameter return can be understood as the same thing. All in order to return a call to the upper layer or a global data, think clearly about the three elements of the function, then your function is defined.
1.2 Recursive boundaries, jumping out of recursion
Similarly, to do this first, and then to think about why, this step is to judge the input parameter of the function, the null of the input parameter, and the input parameter is the initial value, such as the first one or two digits of the Fibonacci series, which may not necessarily be complete at the beginning, it doesn't matter, the following step will continue to be improved, so the example I give here is the first one or two of Fibonacci, rather than directly talking about the conclusion. This step is in the implementation of the function, so the way to think about it is to assume that the critical value or the initial value, or the special value, you have to judge, when you write it for the first time, such as Fibonacci, you can write it this way.
If (n = 1) return 1 * * if (n = = 2) return 1
What you think is not necessarily right, or it is so elegant, it doesn't matter, just think about the boundary. Here is what the meaning of the boundary is. There are two points, one is the boundary of the abnormal value, and the other is the recursive end judgment. What to do than n < 0 in this question, and n = = 1 and n = = 2 respectively correspond to the above, of course, these two points may not be considered completely, assuming that you only consider as in the previous code, or when you write the boundary, you find that you write too much, or it is redundant, so it does not affect the result of the program, then write the recursive formula. Let's go back to the boundary issue.
1.3 Recursive formula
The significance is to gradually reduce the size of the algorithm, or to define a way to make the input value as close to the critical value as possible, that is, to find a relationship between f (n) and f (nmurx) sequence, where f (n) represents the scale of the problem to be solved, and the function value of f (nMux) is smaller than n, which is a key step in recursive functions. If there is no recursive formula, there is no recursion. For example, in the Fibonacci sequence, the recursion formula is f (n) = f (nMel 1) + f (n Mel 2). We observe this formula and find that its nth order is related to nMel 1 and nMel 2, so let's look at it. If we enter any integers, then the value of nMube 1 and nmi 2 may be negative, and we can see that the boundary 0 and negative numbers are not taken into account. So, review the recursion of the previous 1.2 at this time. Let's supplement the boundary and get:
If (n A return last;} 2.3Recursive traversal tree
Recursive traversal tree is also the easiest, assuming that you have not seen the traversal code before, then start from zero to consider this problem, first define the function, confirm that the input parameter is similar to the single linked list inversion, only need a TreeNode node, and then consider the boundary is null, and not for null, do you first think of this?
If (node = = null) return; if (node.left = = null & & node.right = = null) {System.out.pritln (node.val); return;}
Now it seems a little redundant, but assuming you don't know, then the next recursion, take the preorder as an example.
/ / first, the node itself System.out.println (node.val); / / then the node left preOrder (node.left); / / then the node right preOrder (node.right)
This is it, and then review the previous boundary problem. There are only two lines of code above. You can see that when the node is null, you can directly return without considering the child node. The boundary of the word node has already been considered in the boundary of the parent node. Of course, writing this boundary does not affect the running of the program at all, so the final pre-middle and post-order traversal is as follows:
/ / binary tree preorder traversal public static void preOrder (TreeNode node) {if (node = = null) return; System.out.println (node.val); preOrder (node.left); preOrder (node.right);} / / binary tree preorder traversal public static void inOrder (TreeNode node) {if (node = = null) return; preOrder (node.left); System.out.println (node.val); preOrder (node.right) } / / binary tree post-order traversal public static void postOrder (TreeNode node) {if (node = = null) return; preOrder (node.left); preOrder (node.right); System.out.println (node.val);} 2.4 construct a binary tree from a sequence
Next, let's fill in a recursive algorithm problem and enter a binary tree sequence to restore the construction of the binary tree. By the way, we will test the code of the previous traversal tree. After we are also familiar with the recursive routine, we will write the code directly.
/ / 1. To define the function confirmation, only one parameter is needed, that is, the remaining sequence public static TreeNode createBinaryTree (LinkedList inputList) {/ / defines an empty tree node. Here, for uniformity, it can be used in both the boundary and the recursive body, so it is written on the first line TreeNode node = null; / / 2. Boundary if (inputList = = null | | inputList.isEmpty ()) return node; / / 3. The main recursive body, delete and take the first element from the linked list, construct the left and right nodes, and finally return the current node Integer data = inputList.removeFirst (); / / data, mainly to judge outliers. Previously, it has been judged that the linked list is empty if (data! = null) {node = new TreeNode (data); node.left = createBinaryTree (inputList) Node.right = createBinaryTree (inputList);} return node;} public static void main (String [] args) {/ / preorder ergodic sequence LinkedList inputList = new LinkedList (new Integer (new Integer [] {3 beached9); TreeNode node = createBinaryTree (inputList) / / preorder traverses System.out.println ("preorder traversal:"); preOrder (node);} at this point, I believe you have a deeper understanding of "how to write java recursive functions". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.