日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java实现递归层次遍历_Java实现二叉树的前序、中序、后序、层序遍历(递归方法)...

發布時間:2023/12/1 java 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java实现递归层次遍历_Java实现二叉树的前序、中序、后序、层序遍历(递归方法)... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在數據結構中,二叉樹是樹中我們見得最多的,二叉查找樹可以加速我們查找的效率,那么輸出一個二叉樹也變得尤為重要了。

二叉樹的遍歷方法分為四種,分別為前序遍歷、中序遍歷、后序、層序遍歷。下圖即為一個二叉樹。

前序遍歷:先遍歷根結點,然后遍歷左子樹,最后遍歷右子樹。

結果為:4 2 1 3 6 5 7 8 10

中序遍歷:先遍歷左子樹,然后遍歷根結點,最后遍歷右子樹。

結果為:1 2 3 4 5 6 7 8 10

后序遍歷:先遍歷左子樹,然后遍歷右子樹,最后遍歷根節點。

結果為:1 3 2 5 10 8 7 6 4

層序遍歷:逐層遍歷。

結果為:4 2 6 1 3 5 7 8 10

按照上面的規則,就可以很快地對一顆二叉樹進行遍歷,并且快速寫出結果,下面我附上我使用遞歸的方法對二叉樹實施四種遍歷的Java代碼。

public class Tree>

{

private static class BinaryNode

{

BinaryNode(AnyType theElement)

{

this(theElement, null, null);

}

BinaryNode(AnyType theElement, BinaryNode lt, BinaryNode rt)

{

element = theElement;

left = lt;

right = rt;

}

AnyType element;

BinaryNode left;

BinaryNode right;

}

private BinaryNode root;

public void insert(AnyType x)

{

root = insert(x, root);

}

private BinaryNode insert(AnyType x, BinaryNode t)

{

if(t == null)

{

return new BinaryNode<>(x, null, null);

}

int compareResult = x.compareTo(t.element);

if(compareResult < 0)

{

t.left = insert(x, t.left);

}

else if(compareResult > 0)

{

t.right = insert(x, t.right);

}

else

{

;

}

return t;

}

/**

* 前序遍歷

*/

public void preOrder(BinaryNode Node)

{

if (Node != null)

{

System.out.print(Node.element + " ");

preOrder(Node.left);

preOrder(Node.right);

}

}

/**

* 中序遍歷

*/

public void midOrder(BinaryNode Node)

{

if (Node != null)

{

midOrder(Node.left);

System.out.print(Node.element + " ");

midOrder(Node.right);

}

}

/**

* 后序遍歷

*/

public void posOrder(BinaryNode Node)

{

if (Node != null)

{

posOrder(Node.left);

posOrder(Node.right);

System.out.print(Node.element + " ");

}

}

/*

* 層序遍歷

* 遞歸

*/

public void levelOrder(BinaryNode Node) {

if (Node == null) {

return;

}

int depth = depth(Node);

for (int i = 1; i <= depth; i++) {

levelOrder(Node, i);

}

}

private void levelOrder(BinaryNode Node, int level) {

if (Node == null || level < 1) {

return;

}

if (level == 1) {

System.out.print(Node.element + " ");

return;

}

// 左子樹

levelOrder(Node.left, level - 1);

// 右子樹

levelOrder(Node.right, level - 1);

}

public int depth(BinaryNode Node) {

if (Node == null) {

return 0;

}

int l = depth(Node.left);

int r = depth(Node.right);

if (l > r) {

return l + 1;

} else {

return r + 1;

}

}

public static void main( String[] args )

{

int[] input = {4, 2, 6, 1, 3, 5, 7, 8, 10};

Tree tree = new Tree<>();

for(int i = 0; i < input.length; i++)

{

tree.insert(input[i]);

}

System.out.print( "前序遍歷 :" );

tree.preOrder(tree.root);

System.out.print( "\n中序遍歷 :" );

tree.midOrder(tree.root);

System.out.print( "\n后序遍歷 :" );

tree.posOrder(tree.root);

System.out.print("\n遞歸層序遍歷:");

tree.levelOrder(tree.root);

}

}

以上就完成了對二叉樹的四種遍歷,但是這只是遞歸方法的遍歷,下次我再來用非遞歸的方法實現對二叉樹的遍歷。

總結

以上是生活随笔為你收集整理的java实现递归层次遍历_Java实现二叉树的前序、中序、后序、层序遍历(递归方法)...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。