如何在二叉树中实现一个方法,在访问变量之后返回下一个被访问的节点?

huangapple go评论56阅读模式
英文:

How does one implement a method in Binary Tree in returning the next node visited after variable?

问题

[![在这里输入图片描述][1]][1]

  [1]: https://i.stack.imgur.com/yvjWq.png

前序遍历:/ * + a b - c d + e f
中序遍历:a + b * c - d / e + f
后序遍历:a b + c d - * e f + /

假设我们有这棵树。我已经有打印这些遍历顺序的代码;前序、中序和后序。现在剩下的问题是,用户被要求输入在哪个字符旁边的*前序遍历*?假设用户输入了字母a,那么输出应该是

前序遍历紧邻哪个字符?:a
Preordernext(a) = b

以此类推到中序和后序,现在的问题是我似乎无法找到特定节点的下一个节点。

System.out.print("前序遍历中紧邻哪个字符?:");
char q = scn.next().charAt(0);
System.out.print("PreorderNext(" + q + ")| ");
binaryTree.searchPostOrder(q);

到目前为止,这是我想出来的,但是除了空白之外,我没有得到任何结果...

public void searchPostOrder(char kwanix) {
searchPostorder(root, kwanix);
}
private char searchPostorder(Node node, char kwanix) {
if (node.getData() == kwanix) {
if (node.getLeft() != null) {
if (node.getLeft().equals(kwanix)) {
kwanix = node.getData();
}
}
if (node.getRight() != null) {
if (node.getRight().equals(kwanix)) {
kwanix = node.getData();
}
}
}
return kwanix;
}

运行程序后的结果如下:

后序遍历| d c b a
前序遍历| a c d b
中序遍历| c d a b

前序遍历中紧邻哪个字符?:a
PreorderNext(a)|


<details>
<summary>英文:</summary>

[![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/yvjWq.png

Preorder: / * + a b – c d + e f
Inorder: a + b * c – d / e + f
Postorder: a b + c d - * e f + /

Say we have this tree. I already got the code for printing out the orders; Preorder, Inorder and Postorder. Now whats left is the problem, the user is asked to input *Preorder next to what character?*, lets say the user entered the letter a, so the output should be

Preorder next to what character?: a
Preordernext(a) = b

and so on to Inorder and Postorder, now the problem is i cant seem to locate the next node from a specific node.

System.out.print("Preorder next of what character: ");
char q = scn.next().charAt(0);
System.out.print("PreorderNext("+q+")| ");
binaryTree.searchPostOrder(q);

So far this is what i came up with and I received no result aside from blank...

public void searchPostOrder(char kwanix) {
searchPostorder(root, kwanix);
}
private char searchPostorder(Node node, char kwanix) {
if (node.getData() == kwanix) {
if (node.getLeft() != null) {
if (node.getLeft().equals(kwanix)) {
kwanix = node.getData();
}
}
if (node.getRight() != null) {
if (node.getRight().equals(kwanix)) {
kwanix = node.getData();
}
}
}
return kwanix;
}

This is the result after running the program

Post Order| d c b a
Pre order| a c d b
In order| c d a b

Preorder next of what character: a
PreorderNext(a)|


</details>


# 答案1
**得分**: 3

一种(简单的)解决方案:

你发现你的树的“遍历”导致了不同的结果。

    前序遍历:/ * + a b - c d + e f
    中序遍历:a + b * c - d / e + f
    后序遍历:a b + c d - * e f + /

注意,这个输出包含了**所有**你的节点,只是顺序不同。看一下:在前序遍历行的序列中... `b` 紧接在 `a` 之后!

换句话说:一个简单的解决方案是:

- 你选择用户想要的遍历顺序策略
- 你按照该顺序遍历你的树
- 当你找到 `a` 时,继续到下一个节点(一个叶子节点),并将其作为结果返回

<details>
<summary>英文:</summary>

One (simple) solution:

You figured that your tree &quot;walk&quot; leads to different results.

    Preorder: / * + a b – c d + e f
    Inorder: a + b * c – d / e + f
    Postorder: a b + c d - * e f + /

Note that this output contains **all** your nodes, just in different order. And look: in the sequence for the pre order row ... `b` comes right after `a`!

In other words: one simple solution is: 

- you pick the order strategy the user wants
- you walk your tree in that order
- when you find `a`, you continue to the next node (that is a leaf), and that is what you return as result

</details>



# 答案2
**得分**: 2

我的做法将是:

1. 我将遍历所有节点,并仅存储包含字符而不是运算符的节点的值。
例如:

    先序遍历:/ * + a b – c d + e f

    我的数组将包含的字符为:`{a,b,c,d,e,f}`。

2. 然后,我将从索引0循环到数组长度-1,如果遇到userSymbol,则打印[index+1],否则打印空。

<details>
<summary>英文:</summary>

My approach will be :

 1. I will move through all nodes and store value of only those nodes containing characters and not the operators.
Example 

    Preorder: / * + a b – c d + e f

    myArray =&gt; a b c d e f 

My array will contain only characters : `{a , b ,  c,  d,  e , f}`.

 2. Then I will start a loop through index 0 to length of array.length-1 if encounter userSymbol then

Print [index+1] 

else

Print empty.



</details>



huangapple
  • 本文由 发表于 2020年10月15日 17:37:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/64368762.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定