简体   繁体   English

归并排序中的递归,快速排序和树的遍历

[英]Recursion in Merge Sort , Quick Sort and traversal of trees

While learing different algorithms (like merge sort , quick sort or Tree traversals) I have observed that there are two recursive calls immediately followed by each other.在学习不同的算法(如合并排序、快速排序或树遍历)时,我观察到有两个递归调用紧随其后。

I am unable to understand completely.Pls explain in simple terms why do we use two recursive calls?完全看不懂,请简单解释一下为什么要使用两个递归调用? Is this any kind of pattern?这是任何一种模式吗?

Also are there any algorithms where more than two immediate recursive calls are made?还有任何算法可以进行两次以上的立即递归调用吗?

Merge Sort归并排序

m_sort(numbers, temp, left, mid); m_sort(numbers, temp, left, mid);

m_sort(numbers, temp, mid+1, right); m_sort(numbers, temp, mid+1, right);

Tree Traversals树遍历

preorder(node.left)预购(节点。左)

preorder(node.right)预购(节点。右)

There are two recursive calls because the same function needs to be performed in two different places. 有两个递归调用,因为相同的功能需要在两个不同的位置执行。 In the case of tree traversals starting from the root you want to recursively go down the left and then down the right. 在从根开始遍历树的情况下,您要递归地从左向下再向右向下。 The way that function calls work, F calls preorder(node.left) and knows nothing about preorder(node.right) . 函数调用工作的方式, F调用preorder(node.left)而对preorder(node.right)一无所知。 When it goes into the node.left it is now at B . 当它进入node.left它现在位于B The same recursive call will be made there all the way until the bottom, at A . 将会一直进行相同的递归调用,直到底部A为止。 When preorder(node.left) returns from A then the code in B calls preorder(node.right) and the recursion will continue. 当预购(node.left)从返回A 在代码中B调用preorder(node.right)和递归将继续。

This isn't so much a pattern as the nature of doing recursive work on many binary structures, where a divide-and-conquer strategy is adapted to split the work into smaller parts, and then recursion is performed on each part seperately until the trivial case is met (such as a node without children as in A , when it returns) 这并不是一种模式,而是在许多二进制结构上进行递归工作的本质,其中采用分而治之策略将工作拆分成较小的部分,然后分别对每个部分执行递归直到琐碎满足大小写(例如,返回时,如A没有子节点的节点)

维基百科的预遍历

Source: " Sorted binary tree preorder " by Sorted_binary_tree.svg : Miles derivative work: Pluke ( talk ) - Sorted_binary_tree.svg . 来源: Sorted_binary_tree.svg的Sorted Binary tree preorder ”: Miles衍生作品: Pluke交谈-Sorted_binary_tree.svg Licensed under Public Domain via Wikimedia Commons . 通过Wikimedia Commons在公共领域许可。

The reason you want to call it twice is that because it splits the problem into half.你想调用它两次的原因是因为它把问题分成了两半。

For the sorting case, you want to sort the lower half and the upper half.对于排序情况,您希望对下半部分和上半部分进行排序。 And in the tree case, you want to traverse the left track and the right track.在树的情况下,您要遍历左轨道和右轨道。 It just happened the number is 2 because you split the domain into half in each recursion.恰好这个数字是 2,因为您在每次递归中将域分成两半。 But you can split the problem into how many parts you want, and some problem might even has variable number of parts in each recursion.但是你可以将问题分成你想要的部分,有些问题甚至可能在每次递归中都有可变数量的部分。

An easy way to imagine this, when you stand in a crossroad, you think about how many directions you can go from there, and if you want to visit all of the directions, then you need to call all of them.一个简单的想象方法,当你站在十字路口时,你会考虑从那里可以去多少个方向,如果你想访问所有的方向,那么你需要调用所有的方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM