二叉树递归中的InOrder打印函数

huangapple 未分类评论52阅读模式
英文:

Binary Tree Recursion PrintInOrder function

问题

给定的代码片段如下:

public void printInorder() {
    printInorder(root);
}

private void printInorder(Node<E> n) {
    // ...
}
private static class Node<E> {
    private E data;
    private Node<E> left;
    private Node<E> right;

    private Node(E data) {
        this.data = data;
        left = right = null;
    }
}

我的任务是完成private中的Inorder方法,使其能够使用递归工作。
我目前已经完成的部分是:

private void printInorder(Node<E> n) {
    if (n != null) {
        printInorder(n.left);
        System.out.println(n.data);
        printInorder(n.right);
    }
}

这是正确的答案,我只是不太明白为什么它是正确的。

我理解如果你调用printInorder(root)来调用公共方法,它会进入私有方法,首先打印左侧的数据,然后通过"System.out.println"打印所有的数据。然后它会进入右侧,但是它是如何打印右侧数据的呢?

提前感谢你的帮助!

英文:

Given:

public void printInorder() {
	printInorder(root);
}

private void printInorder(Node&lt;E&gt; n) {
	...
}
private static class Node&lt;E&gt; {
	private E data;
	private Node&lt;E&gt; left;
	private Node&lt;E&gt; right;

	private Node(E data) {
		 data = data;
		 left = right = null;
	}

My task is to finish the private Inorder method for it to work with recursion.
What I did so far is:

private void printInorder(Node&lt;E&gt; n) {
if (n != null) {
    printInorder(n.left);
    System.out.println(n.data);
    printInorder(n.right);

Which is the right answer, I just dont understand exactly why it is right.

I understand that if you enter printInorder(root) to the public method. It will go the private method and first print the left hand side, then "System.out.println" all the data. Then it will go the rightside, but how does it print the data on the right side?

Thanks in advance for the help!

答案1

得分: 0

解释下面的递归模式。希望有助于理解右节点部分。

printInorder(1)	
	printInorder(2) // 1 的左节点
		printInorder(4) // 2 的左节点
			printInorder(null) // 4 的左节点。不会打印任何内容。
		System.out.println(4);
		printInorder(null); // 4 的右节点
	System.out.println(2);
	printInorder(5); // 2 的右节点
		printInorder(null); // 5 的左节点。这里不会打印。
	System.out.println(5);
	printInorder(null); // 5 的右节点。这里不会打印。
System.out.println(1);
	printInorder(3); // 1 的右节点
		printInorder(6) // 3 的左节点
			printInorder(null) // 6 的左节点。这里不会打印。
		System.out.println(6);
		printInorder(null); // 6 的右节点。这里不会打印。
	System.out.println(3);
		printInorder(7); // 3 的右节点
			printInorder(null); // 7 的左节点。这里不会打印。
		System.out.println(7);
		printInorder(null); // 7 的右节点。这里不会打印。
英文:

Explained the recursion pattern below. Hopefully helps understanding the right node part.

printInorder(1)	
	printInorder(2) //left node of 1
		printInorder(4) //left node of 2
			printInorder(null) // left node of 4. does not print anything. 
		System.out.println(4);
		printInorder(null); // right node of 4 
	System.out.println(2);
	printInorder(5); //referring right node of 2
		printInorder(null); // left node of 5 . No print here
	System.out.println(5);
	printInorder(null); // right node of 5. No print here
System.out.println(1);
	printInorder(3); // right node of 1
		printInorder(6) // left node of 6
			printInorder(null) // left node of 3. does no print here
		System.out.println(6);
		printInorder(null); // right node of 6 . No print here
	System.out.println(3);
		printInorder(7); //referring right node of 3
			printInorder(null); // left node of 7 . No print here
		System.out.println(7);
		printInorder(null); // right node of 7. No print here	

huangapple
  • 本文由 发表于 2020年4月8日 16:27:40
  • 转载请务必保留本文链接:https://java.coder-hub.com/61096424.html
匿名

发表评论

匿名网友

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

确定