【数据算法】Java实现二叉树存储以及遍历
生活随笔
收集整理的這篇文章主要介紹了
【数据算法】Java实现二叉树存储以及遍历
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
二叉樹在java中我們使用數(shù)組的形式保存原數(shù)據(jù),這個(gè)數(shù)組作為二叉樹的數(shù)據(jù)來源,后續(xù)對(duì)數(shù)組中的數(shù)據(jù)進(jìn)行節(jié)點(diǎn)化操作。
步驟就是原數(shù)據(jù):數(shù)組
節(jié)點(diǎn)化數(shù)據(jù):定義 Node節(jié)點(diǎn)對(duì)象
存儲(chǔ)節(jié)點(diǎn)對(duì)象:通過LinkedList保存Node節(jié)點(diǎn)對(duì)象
在操作過程中我們需要將當(dāng)前結(jié)點(diǎn)和前一節(jié)點(diǎn)、后一節(jié)點(diǎn)進(jìn)行關(guān)系綁定
?
package tree; import java.util.LinkedList; import java.util.List; /** * 功能:把一個(gè)數(shù)組的值存入二叉樹中,然后進(jìn)行3種方式的遍歷 * * 參考資料0:數(shù)據(jù)結(jié)構(gòu)(C語言版)嚴(yán)蔚敏 * * 參考資料1:http://zhidao.baidu.com/question/81938912.html * * 參考資料2:http://cslibrary.stanford.edu/110/BinaryTrees.html#java * * @author ocaicai@yeah.net @date: 2011-5-17 * */ public class BinTreeTraverse2 { private int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; private static List<Node> nodeList = null; /** * 內(nèi)部類:節(jié)點(diǎn) * * @author ocaicai@yeah.net @date: 2011-5-17 * */ private static class Node { Node leftChild; Node rightChild; int data; Node(int newData) { leftChild = null; rightChild = null; data = newData; } } public void createBinTree() { nodeList = new LinkedList<Node>(); // 將一個(gè)數(shù)組的值依次轉(zhuǎn)換為Node節(jié)點(diǎn) for (int nodeIndex = 0; nodeIndex < array.length; nodeIndex++) { nodeList.add(new Node(array[nodeIndex])); } // 對(duì)前l(fā)astParentIndex-1個(gè)父節(jié)點(diǎn)按照父節(jié)點(diǎn)與孩子節(jié)點(diǎn)的數(shù)字關(guān)系建立二叉樹 for (int parentIndex = 0; parentIndex < array.length / 2 - 1; parentIndex++) { // 左孩子 nodeList.get(parentIndex).leftChild = nodeList .get(parentIndex * 2 + 1); // 右孩子 nodeList.get(parentIndex).rightChild = nodeList .get(parentIndex * 2 + 2); } // 最后一個(gè)父節(jié)點(diǎn):因?yàn)樽詈笠粋€(gè)父節(jié)點(diǎn)可能沒有右孩子,所以單獨(dú)拿出來處理 int lastParentIndex = array.length / 2 - 1; // 左孩子 nodeList.get(lastParentIndex).leftChild = nodeList .get(lastParentIndex * 2 + 1); // 右孩子,如果數(shù)組的長度為奇數(shù)才建立右孩子 if (array.length % 2 == 1) { nodeList.get(lastParentIndex).rightChild = nodeList .get(lastParentIndex * 2 + 2); } } /** * 先序遍歷 * * 這三種不同的遍歷結(jié)構(gòu)都是一樣的,只是先后順序不一樣而已 * * @param node * 遍歷的節(jié)點(diǎn) */ public static void preOrderTraverse(Node node) { if (node == null) return; System.out.print(node.data + " "); preOrderTraverse(node.leftChild); preOrderTraverse(node.rightChild); } /** * 中序遍歷 * * 這三種不同的遍歷結(jié)構(gòu)都是一樣的,只是先后順序不一樣而已 * * @param node * 遍歷的節(jié)點(diǎn) */ public static void inOrderTraverse(Node node) { if (node == null) return; inOrderTraverse(node.leftChild); System.out.print(node.data + " "); inOrderTraverse(node.rightChild); } /** * 后序遍歷 * * 這三種不同的遍歷結(jié)構(gòu)都是一樣的,只是先后順序不一樣而已 * * @param node * 遍歷的節(jié)點(diǎn) */ public static void postOrderTraverse(Node node) { if (node == null) return; postOrderTraverse(node.leftChild); postOrderTraverse(node.rightChild); System.out.print(node.data + " "); } public static void main(String[] args) { BinTreeTraverse2 binTree = new BinTreeTraverse2(); binTree.createBinTree(); // nodeList中第0個(gè)索引處的值即為根節(jié)點(diǎn) Node root = nodeList.get(0); System.out.println("先序遍歷:"); preOrderTraverse(root); System.out.println(); System.out.println("中序遍歷:"); inOrderTraverse(root); System.out.println(); System.out.println("后序遍歷:"); postOrderTraverse(root); } }?
轉(zhuǎn)載于:https://www.cnblogs.com/wentaos/p/7407006.html
與50位技術(shù)專家面對(duì)面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的【数据算法】Java实现二叉树存储以及遍历的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LightOJ - 1179 Josep
- 下一篇: 【tomcat】手动部署动态JavaWe