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

歡迎訪問 生活随笔!

生活随笔

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

java

java遍历树结构数据_Java数据结构——二叉树的遍历(汇总)

發布時間:2023/12/9 java 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java遍历树结构数据_Java数据结构——二叉树的遍历(汇总) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

二叉樹的遍歷分為深度優先遍歷(DFS)和廣度優先遍歷(BFS)

DFS遍歷主要有:

前序遍歷

中序遍歷

后序遍歷

一、遞歸實現DFS

Node.java:

public class Node {

private Object data;

Node richild;

Node lechild;

public Object getData() {

return data;

}

public void setData(Object data) {

this.data = data;

}

public Node getRichild() {

return richild;

}

public void setRichild(Node richild) {

this.richild = richild;

}

public Node getLechild() {

return lechild;

}

public void setLechild(Node lechild) {

this.lechild = lechild;

}

public Node(Object data, Node lechild, Node richild) {

super();

this.data = data;

this.richild = richild;

this.lechild = lechild;

}

public Node() {

super();

}

}

遞歸遍歷:

public class BTree {

private static Node root;

//構造樹

public static void init() {

Node node1 = new Node("A", null, null);

Node node2 = new Node("B", node1, null);

Node node3 = new Node("C", null, null);

Node node4 = new Node("D", node2, node3);

Node node5 = new Node("E", null, null);

Node node6 = new Node("F", null, node5);

Node node7 = new Node("G", node4, node6);

root = node7;

}

//訪問節點

public static void visited(Node n) {

System.out.print(n.getData() + " ");

}

//前序遍歷

public static void preOrder(Node n) {

if (n != null) {

visited(n);

preOrder(n.getLechild());

preOrder(n.getRichild());

}

}

//中序遍歷

public static void inOrder(Node n) {

if (n != null) {

inOrder(n.getLechild());

visited(n);

inOrder(n.getRichild());

}

}

//后序遍歷

public static void postOrder(Node n) {

if (n != null) {

postOrder(n.getLechild());

postOrder(n.getRichild());

visited(n);

}

}

public static void main(String[] args) {

init();

System.out.print("遞歸前序:");

preOrder(root);

System.out.println();

System.out.print("遞歸中序:");

inOrder(root);

System.out.println();

System.out.print("遞歸后序:");

postOrder(root);

System.out.println();

}

}

二、非遞歸實現DFS(依靠棧)

//前序遍歷

public static void preOrder(Node n) {

System.out.print("非遞歸前序:");

Stack stack = new Stack<>();

int index = 0;

while (n != null || index > 0) {

while (n != null) {

System.out.print(n.getData() + " ");

stack.push(n);

index++;

n = n.getLechild();

}

n = stack.pop();

index--;

n = n.getRichild();

}

}

//中序遍歷

public static void inOrder(Node n) {

System.out.print("非遞歸中序:");

Stack stack = new Stack<>();

int index = 0;

while (n != null || index > 0) {

while (n != null) {

stack.push(n);

index++;

n = n.getLechild();

}

n = stack.pop();

System.out.print(n.getData() + " ");

index--;

n = n.getRichild();

}

}

//后序遍歷

public static void postOrder(Node n) {

System.out.print("非遞歸后序:");

Stack stack = new Stack<>();

int index = 0;

Node lastVisited = null;

while (n != null || index > 0) {

while (n != null) {

stack.push(n);

index++;

n = n.getLechild();

}

n = stack.peek();

if (n.getRichild() == null || n.getRichild() == lastVisited) {

System.out.print(n.getData() + " ");

lastVisited = n;

index--;

stack.pop();

n = null;

} else {

n = n.getRichild();

}

}

}

三、實現層序遍歷(依靠隊列)

public static void LevenOrder(Node root) {

if (root == null) {

return;

}

Queue queue = new LinkedList<>();

queue.add(root);

Node temp = null;

while (!queue.isEmpty()) {

temp = queue.poll();

visited(temp);

if (temp.getLeChild() != null) {

queue.add(temp.getLeChild());

}

if (temp.getRChild() != null) {

queue.add(temp.getChild());

}

}

}

總結

以上是生活随笔為你收集整理的java遍历树结构数据_Java数据结构——二叉树的遍历(汇总)的全部內容,希望文章能夠幫你解決所遇到的問題。

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