如何二叉树转换为二进制搜索树就地,即,我们不能使用任何额外的空间转换为、二叉树、空间

2023-09-11 03:01:52 作者:哄你睡觉

如何将二叉树转换成二叉搜索树就地,也就是说,我们不能使用任何额外的空间。

How to convert a binary tree to binary search tree in-place, i.e., we cannot use any extra space.

推荐答案

您不会过多去,但如果需求是什么,我觉得是,你有一个二叉树已经创建并坐在内存,但没有排序。(你想让它进行排序,反正路)

You don't give much to go on, but if the requirement is what I think it is, you have a binary tree already created and sitting in memory, but not sorted (the way you want it to be sorted, anyway).

我假设这棵树结的样子

struct tree_node {
    struct tree_node * left;
    struct tree_node * right;
    data_t data;
};

我也假设你可以READ C

I'm also assuming that you can read C

虽然我们可以只是坐在那里想知道为什么没有按照排序顺序,这并不对我们有什么好被创造了有史以来这棵树,所以我会忽略它,只是处理排序了。

While we could just sit around wondering why this tree was ever created without having been created in sorted order that doesn't do us any good, so I'll ignore it and just deal with sorting it.

这是不使用任何额外的空间要求是奇数。暂时会有额外的空间,如果只在栈。我会认为这意味着调用malloc或类似的东西,也该得到的树有使用没有更多的内存比原来的无序树。

The requirement that no extra space be used is odd. Temporarily there will be extra space, if only on the stack. I'm going to assume that it means that calling malloc or something like that and also that the resulting tree has to use no more memory than the original unsorted tree.

第一,最简单的办法是做无序树移除从树中的每个节点,做一个排序插入一个新的树的preorder穿越。这是O(N + N *的log(n)),这是O(n *的log(n))。

The first and easiest solution is to do a preorder traversal of the unsorted tree removing each node from that tree and doing a sorted insertion into a new tree. This is O(n+n*log(n)), which is O(n*log(n)).

如果这不是他们想要什么和你将不得不使用的旋转和东西.....这是可怕的!

If this isn't what they want and you're going to have to use rotations and stuff..... that's horrible!

我认为你可以做一个堆排序的一个奇怪的版本做到这一点,但我遇到了问题。 没想到的是另一件事,这将是可怕的慢,会做冒泡排序的奇数版在树上。

I thought that you could do this by doing an odd version of a heap sort, but I ran into problems. Another thing that did come to mind, which would be horribly slow, would to do an odd version of bubble sort on the tree.

有关此的每个节点进行比较,并可能交换,每次它的直接子(因此也与其父),直至您遍历树并没有发现任何 所需的交换。做一个摇床排序(冒泡排序的那张左到右,左右)版本,这样的工作最好,最初的传球后,你会不会需要遍历下来子树没看出来以便相对于它的父

For this each node is compared and possibly swapped with each of it's direct children (and therefore also with its parent) repeatedly until you traverse the tree and don't find any needed swaps. Doing a shaker sort (bubble sort that goes left to right and the right to left) version of this would work best, and after the initial pass you would not need to traverse down subtrees that did not look out of order with respect to it's parent.

我敢肯定,无论是这个algorthm被认为由别人在我面前,并有我只是不知道一个很酷的名字,或者说,它是在某种方式,我没有看到根本性的缺陷。

I'm sure that either this algorthm was thought up by someone else before me and has a cool name that I just don't know, or that it is fundamentally flawed in some way that I'm not seeing.

未来与运行​​时计算的第二个建议是pretty的复杂。起初,我认为,这将仅仅是为O(n ^ 2),像泡沫和振动筛排序,但我不能满足自己的子树遍历避免可能无法赢得足够使为O一点点更好的(N ^ 2)。从本质上讲泡沫和振动筛排序得到这个优化过,但只在那里总共有序性的早期出现的结束,你可以砍掉限制。有了这棵树版本你oppurtunities,以避免可能的大块的一系列中间也是如此。嗯,就像我说的,它可能是致命的缺陷。

Coming up with the run-time calculations for the second suggestion is a pretty complicated. At first I thought that it would simply be O(n^2), like bubble and shaker sorts, but I can't satisfy myself that the subtree traversal avoidance might not win enough to make it a little bit better than O(n^2). Essentially bubble and shaker sorts get this optimization too, but only at the ends where total sortedness occurs early and you can chop down the limits. With this tree version you get oppurtunities to possibly avoid chunks in the middle of the set as well. Well, like I said, it's probably fatally flawed.

 
精彩推荐
图片推荐