小編給大家分享一下Java怎么實現(xiàn)的二叉樹常用操作,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),望花企業(yè)網(wǎng)站建設(shè),望花品牌網(wǎng)站建設(shè),網(wǎng)站定制,望花網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,望花網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。具體如下:
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.Stack;
//二叉樹的建樹,前中后 遞歸非遞歸遍歷 層序遍歷
//Node節(jié)點
class Node {
int element;
Node left;
Node right;
public Node() {
}
public Node(int element) {
this.element = element;
}
}
// BinaryTree
public class Tree {
// creat tree from array
public static Node creatTree(int[] data, int i) {
if (i >= data.length || data[i] == -1)
return null;
Node temp = new Node(data[i]);
temp.left = creatTree(data, i * 2 + 1);
temp.right = creatTree(data, i * 2 + 2);
return temp;
}
// pre前序遍歷遞歸
public static void pre(Node temp) {
if (temp == null)
return;
System.out.print(temp.element + " ");
pre(temp.left);
pre(temp.right);
}
// mid中序遍歷遞歸
public static void mid(Node temp) {
if (temp == null)
return;
mid(temp.left);
System.out.print(temp.element + " ");
mid(temp.right);
}
// last后序遍歷遞歸
public static void last(Node temp) {
if (temp == null)
return;
last(temp.left);
last(temp.right);
System.out.print(temp.element + " ");
}
// pre1前序遍歷非遞歸
public static void pre1(Node temp) {
Stack<Node> stack = new Stack<>();
while (temp != null || !stack.isEmpty()) {
while (temp != null) {
stack.push(temp);
System.out.print(temp.element + " ");
temp = temp.left;
}
if (!stack.isEmpty()) {
temp = stack.pop().right;
}
}
}
// mid1中序遍歷非遞歸
public static void mid1(Node temp) {
Stack<Node> stack = new Stack<>();
while (temp != null || !stack.isEmpty()) {
while (temp != null) {
stack.push(temp);
temp = temp.left;
}
if (!stack.isEmpty()) {
temp = stack.pop();
System.out.print(temp.element + " ");
temp = temp.right;
}
}
}
// last1后序遍歷非遞歸
public static void last1(Node temp) {
Stack<Node> stack = new Stack<>();
Stack<Node> stack2 = new Stack<>();
while (temp != null || !stack.isEmpty()) {
while (temp != null) {
stack.push(temp);
stack2.push(temp);
temp = temp.right;
}
if (!stack.isEmpty()) {
temp = stack.pop().left;
}
}
while (!stack2.isEmpty())
System.out.print(stack2.pop().element + " ");
}
// ceng層序遍歷
public static void ceng(Node temp) {
if (temp == null)
return;
Queue<Node> queue = new ArrayDeque<>();
queue.offer(temp);
while (!queue.isEmpty()) {
temp = queue.poll();
System.out.print(temp.element + " ");
if (temp.left != null)
queue.offer(temp.left);
if (temp.right != null)
queue.offer(temp.right);
}
}
// Demo
public static void main(String[] args) {
int[] array = { 1, 2, 3, 4, 5, 6, 7, -1, -1, 10, -1, -1, 13 };
Node tree = creatTree(array, 0);
System.out.println("創(chuàng)新互聯(lián)測試結(jié)果:");
pre(tree);
System.out.println();
pre1(tree);
System.out.println();
mid(tree);
System.out.println();
mid1(tree);
System.out.println();
last(tree);
System.out.println();
last1(tree);
System.out.println();
ceng(tree);
}
}運行結(jié)果:

看完了這篇文章,相信你對“Java怎么實現(xiàn)的二叉樹常用操作”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
網(wǎng)頁題目:Java怎么實現(xiàn)的二叉樹常用操作-創(chuàng)新互聯(lián)
網(wǎng)站地址:http://chinadenli.net/article48/cepshp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計、網(wǎng)站設(shè)計、網(wǎng)站建設(shè)、ChatGPT、網(wǎng)站改版、響應(yīng)式網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容