从数组创建 BST

发布于 2024-10-25 20:29:46 字数 805 浏览 0 评论 0原文

我需要通过以下(奇怪的)方式创建二叉搜索树:

给我一个数组(A[n])。 A[1] 成为树的根。

  • 然后,我将 A[1]+A[2] 插入到根的左子树(subtree1,下面使用),并将 A[1]-A[2] 插入到根的右子树(subtree2) )

  • 我将 A[1]+A[2]+A[3] 插入到 subtree1 (subtree3) 的左子树,将 A[1]+A[2]-A[3] 插入到 subtree1 的右子树 ( subtree4)。

  • 然后,我将 A[1]-A[2]+A[3] 插入到 subtree2 (subtree5) 的左子树,将 A[1]-A[2]-A[3] 插入到 subtree2 (subtree5) 的右子树subtree2 (subtree6)。

  • 我对 subtree3、subtree4、subtree5、subtree6 重复,直到到达数组的末尾。

因此,基本上,数组的第一个元素成为树的根,然后向下移动:每个左子树的值是其父级加上数组的下一个元素的和,每个右子树的值是它的父元素和数组中下一个元素的父元素。

我知道我需要使用递归的概念,但以修改的方式。在这里输入我的问题并试图向除了我的大脑之外的其他人解释它实际上让我以一种给我一些想法尝试的方式形成它,但我可以看到我正在处理的问题是一个常见的问题,所以也许你可以给给我一些关于如何使用递归来构建树的指导。

环顾其他问题和讨论,我了解到有一项政策禁止提出完整的解决方案,因此我想明确表示我不是在寻求解决方案,而是在寻求指导。如果有人想看一下,我可以向您展示我已经完成的工作。

I need to create a binary search tree in the following (strange) way:

I am given an array (A[n]). A[1] becomes the root of the tree.

  • Then, I insert A[1]+A[2] to the left subtree (subtree1, used below) of the root and also insert A[1]-A[2] to the right subtree (subtree2) of the root.

  • I insert A[1]+A[2]+A[3] to the left subtree of subtree1 (subtree3) and A[1]+A[2]-A[3] to the right subtree of subtree1 (subtree4).

  • Then, I insert A[1]-A[2]+A[3] to the left subtree of subtree2 (subtree5) and A[1]-A[2]-A[3] to the right subtree of subtree2 (subtree6).

  • I repeat for subtree3, subtree4, subtree5, subtree6 until I reach the end of the array.

So, basically, the first element of the array becomes the root of the tree and then I move down: Every left subtree has for value the sum of its parent plus the next element of the array and every right subtree has for value the difference of its parent and of the next element in the array.

I understand I need to use the concept of recursion but in a modified way. Typing my problem here and trying to explain it to someone else apart from my brain actually made me form it in a way that gave me some ideas to try but I can see the problem I am dealing with being a usual problem so maybe you could give me some pointers on how to use recursion to build the tree.

Looking around at other questions and the discussions I understand there is a policy against asking whole solutions so I wanted to make it clear that I am not asking for the solution but for guidance to it. If someone would like to have a look I can show you what I've already done.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

捶死心动 2024-11-01 20:29:46

进行递归的方法是始终假设您手头已经有一个工作函数。那么让我们看看[使用 Java 语法]...

Tree buildTree(int currentSum, int[] array, int index, boolean sign);

假设可行。那么你需要在索引 i 处构建一棵树吗?

// current value to look at at this level
int curValue = array[index];
// depending on sign, it may be negative
if (!sign) { 
  curValue *= -1;
}
// add it to the running total
int nodeValue = currentSum + curValue;
Node nd = new Node(nodeValue);

nd.left = buildTree(nodeValue, array, index + 1, true);
nd.right = buildTree(nodeValue, array, index + 1, false);

基本上就是这样。您需要处理边缘情况:index = array.length、创建第一个节点等

The way to do recursion is to always assume you already have a working function in hand. So let's see [using Java syntax]...

Tree buildTree(int currentSum, int[] array, int index, boolean sign);

Suppose that works. Then would do u need to do to build a tree at index i?

// current value to look at at this level
int curValue = array[index];
// depending on sign, it may be negative
if (!sign) { 
  curValue *= -1;
}
// add it to the running total
int nodeValue = currentSum + curValue;
Node nd = new Node(nodeValue);

nd.left = buildTree(nodeValue, array, index + 1, true);
nd.right = buildTree(nodeValue, array, index + 1, false);

That's basically it. You need to take care of the edge cases: of index = array.length, creation of the very first node, and the like

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文