導航:首頁 > 編程語言 > 先序遍歷非遞歸java

先序遍歷非遞歸java

發布時間:2025-09-25 15:45:49

『壹』 求數據結構(java版)實驗樹和二叉樹題目答案

/**
* @param args
之前在大學的時候寫的一個二叉樹演算法,運行應該沒有問題,就看適不適合你的項目了 */
public static void main(String[] args) {

BiTree e = new BiTree(5);
BiTree g = new BiTree(7);
BiTree h = new BiTree(8);
BiTree l = new BiTree(12);
BiTree m = new BiTree(13);
BiTree n = new BiTree(14);
BiTree k = new BiTree(11, n, null);
BiTree j = new BiTree(10, l, m);
BiTree i = new BiTree(9, j, k);
BiTree d = new BiTree(4, null, g);
BiTree f = new BiTree(6, h, i);
BiTree b = new BiTree(2, d, e);
BiTree c = new BiTree(3, f, null);
BiTree tree = new BiTree(1, b, c);
System.out.println("遞歸前序遍歷二叉樹結果: ");
tree.preOrder(tree);
System.out.println();
System.out.println("非遞歸前序遍歷二叉樹結果: ");
tree.iterativePreOrder(tree);
System.out.println();
System.out.println("遞歸中序遍歷二叉樹的結果為:");
tree.inOrder(tree);
System.out.println();
System.out.println("非遞歸中序遍歷二叉樹的結果為:");
tree.iterativeInOrder(tree);
System.out.println();
System.out.println("遞歸後序遍歷二叉樹的結果為:");
tree.postOrder(tree);
System.out.println();
System.out.println("非遞歸後序遍歷二叉樹的結果為:");
tree.iterativePostOrder(tree);
System.out.println();
System.out.println("層次遍歷二叉樹結果: ");
tree.LayerOrder(tree);
System.out.println();
System.out.println("遞歸求二叉樹中所有結點的和為:"+getSumByRecursion(tree));
System.out.println("非遞歸求二叉樹中所有結點的和為:"+getSumByNoRecursion(tree));

System.out.println("二叉樹中,每個節點所在的層數為:");
for (int p = 1; p <= 14; p++)
System.out.println(p + "所在的層為:" + tree.level(p));
System.out.println("二叉樹的高度為:" + height(tree));
System.out.println("二叉樹中節點總數為:" + nodes(tree));
System.out.println("二叉樹中葉子節點總數為:" + leaf(tree));
System.out.println("二叉樹中父節點總數為:" + fatherNodes(tree));
System.out.println("二叉樹中只擁有一個孩子的父節點數:" + oneChildFather(tree));
System.out.println("二叉樹中只擁有左孩子的父節點總數:" + leftChildFather(tree));
System.out.println("二叉樹中只擁有右孩子的父節點總數:" + rightChildFather(tree));
System.out.println("二叉樹中同時擁有兩個孩子的父節點個數為:" + doubleChildFather(tree));
System.out.println("--------------------------------------");
tree.exChange();
System.out.println("交換每個節點的左右孩子節點後......");
System.out.println("遞歸前序遍歷二叉樹結果: ");
tree.preOrder(tree);
System.out.println();
System.out.println("非遞歸前序遍歷二叉樹結果: ");
tree.iterativePreOrder(tree);
System.out.println();
System.out.println("遞歸中序遍歷二叉樹的結果為:");
tree.inOrder(tree);
System.out.println();
System.out.println("非遞歸中序遍歷二叉樹的結果為:");
tree.iterativeInOrder(tree);
System.out.println();
System.out.println("遞歸後序遍歷二叉樹的結果為:");
tree.postOrder(tree);
System.out.println();
System.out.println("非遞歸後序遍歷二叉樹的結果為:");
tree.iterativePostOrder(tree);
System.out.println();
System.out.println("層次遍歷二叉樹結果: ");
tree.LayerOrder(tree);
System.out.println();

System.out.println("遞歸求二叉樹中所有結點的和為:"+getSumByRecursion(tree));
System.out.println("非遞歸求二叉樹中所有結點的和為:"+getSumByNoRecursion(tree));

System.out.println("二叉樹中,每個節點所在的層數為:");
for (int p = 1; p <= 14; p++)
System.out.println(p + "所在的層為:" + tree.level(p));
System.out.println("二叉樹的高度為:" + height(tree));
System.out.println("二叉樹中節點總數為:" + nodes(tree));
System.out.println("二叉樹中葉子節點總數為:" + leaf(tree));
System.out.println("二叉樹中父節點總數為:" + fatherNodes(tree));
System.out.println("二叉樹中只擁有一個孩子的父節點數:" + oneChildFather(tree));
System.out.println("二叉樹中只擁有左孩子的父節點總數:" + leftChildFather(tree));
System.out.println("二叉樹中只擁有右孩子的父節點總數:" + rightChildFather(tree));
System.out.println("二叉樹中同時擁有兩個孩子的父節點個數為:" + doubleChildFather(tree));
}
}

『貳』 非遞歸的二叉樹前序遍歷演算法有什麼用途

  1. 遞歸和非遞歸只是解決問題的方法的不同,本質還是一樣的。

2. 遞歸演算法相對於非遞歸演算法來說效率通常都會更低

2.1 遞歸演算法會有更多的資源需要壓棧和出棧操作(不僅僅是參數,還有函數地址等)

2.2 由於編譯器對附加的一些棧保護機制會導致遞歸執行的更加低效

3. 使用循環代替遞歸演算法,通常可以獲得更好的執行效率和空間效率,在二叉樹層次較深的情況下,採用非遞歸方式遍歷能夠有效的提升遍歷的性能。

『叄』 如何不用遞歸遍歷二叉樹

非遞歸的方法是用存儲代替計算,就是在建立樹時,實現了存儲展開,相當於存儲了未來需要遍歷的路徑,所以就快了。遞歸是送快遞,一層層往下遞,非遞歸是先建好區域倉庫,由各地倉庫儲存發貨,所以速度更快,但需要倉庫儲存(內存佔用更多)。
二叉樹遍歷在數據結構中用得多,這種演算法是從kb時代的內存來的,主要用於理解概念,提升編程時的思想用。
實際用途中
如果用於商業一般用資料庫代替,根本用不到二叉樹,是用存儲代替計算。速度快,可以用內存資料庫,如我用h2 database的Memory Mode 在java下可以實現1秒1百萬次插入。用sqlite內存模式代替以前在c++需要手工管理的數據結構。數據量大一個電腦存不下時,用hadoop/spark/redis,對分布式大數據支持比較好。
如果用於計算量大的任務或內核結構,可以用矩陣數組,鏈表,k/v這種比較直觀模式存儲。
對於樹和圖這種在內存中復雜的數據結構,盡量不要在生產環境下使用,容易內存泄露,用簡單方式代替。對於圖結構,可以使用圖資料庫,如neo4j。對於樹結構,可以在資料庫中存儲一棵樹。實際上資料庫的存儲多用樹,如B樹、B-樹、B+樹、B*樹。
當然如果你寫加密演算法,這種要求極高的程序時,還是需要考慮性能最大化的,否則一般用存儲代替遍歷計算,因為內存和硬碟,現在很便宜了,而cpu還是一種寶貴的資源。

閱讀全文

與先序遍歷非遞歸java相關的資料

熱點內容
gps埠檢測工具下載 瀏覽:482
什麼app可以儲存流量 瀏覽:41
linux怎麼重命名文件 瀏覽:964
99的壓縮文件怎麼打開 瀏覽:675
升級ios103黑 瀏覽:482
文件夾放桌面被刪了還能找回嗎 瀏覽:575
酷狗音樂40版本下載地址 瀏覽:715
我要看賽羅奧特曼升級 瀏覽:286
車六方宏程序怎麼編程序 瀏覽:300
蘋果5sa1528怎麼用移動4g 瀏覽:699
86版本爆頭兵 瀏覽:820
先序遍歷非遞歸java 瀏覽:591
尋找文件觸發路徑 瀏覽:398
struts2多個配置文件 瀏覽:487
app指紋鎖在哪裡設置 瀏覽:684
稱重pLc怎麼編程稱重 瀏覽:404
讀class文件包路徑 瀏覽:226
圖像編程基礎怎麼學 瀏覽:434
俠客密碼查看器能看qq密碼嗎 瀏覽:533
如何在吃雞中顯示CPU等數據 瀏覽:818

友情鏈接