快速排序和树遍历

快速排序和树遍历

本文介绍了在递归归并排序,快速排序和树遍历的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然learing不同的算法(如归并排序,快速排序或树遍历)我观察到,有立即跟对方两个递归调用。

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.

我无法理解completely.Pls深入浅出的解释了为什么我们用两个递归调用?这是任何一种模式?

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?

合并排序

m_sort(数字,温度,左,中);

m_sort(numbers, temp, left, mid);

m_sort(数字,温度,中+ 1,右);

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

树遍历

preorder(node.left)

preorder(node.left)

preorder(node.right)

preorder(node.right)

推荐答案

有两个递归调用,因为相同的功能需要在两个不同的地方进行。在树的遍历从根开始的情况下,你想递归地往下走左边,然后降权。该函数调用工作, F 呼叫的方式 preorder(node.left)和一无所知 preorder(node.right)。当它进入 node.left 现在是 B 。同样的递归调用将作出有一路,直到底部,在 A 。当preorder(node.left)从 A 返回,然后的code。在 B 电话 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). When it goes into the node.left it is now at B. The same recursive call will be made there all the way until the bottom, at A. When preorder(node.left) returns from A then the code in B calls preorder(node.right) and the recursion will continue.

这是与其说是一个图案作为操作的递归工作在许多二进制结构中,当一个分而治之的策略适于工作分成更小的部分,然后递归每个部分单独进行的性质直到琐碎的情况下得到满足(如一个节点,而孩子在 A ,当它返回时)

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)

来源:衍生作品: Pluke (的) - 的。在公共领域的许可通过维基共享资源

Source: "Sorted binary tree preorder" by Sorted_binary_tree.svg: Milesderivative work: Pluke (talk) - Sorted_binary_tree.svg. Licensed under Public Domain via Wikimedia Commons.

这篇关于在递归归并排序,快速排序和树遍历的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 08:00