国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

完全二叉樹的前序遍歷,中序遍歷,后序遍歷

2019-11-10 16:55:27
字體:
來源:轉載
供稿:網友

完全二叉樹性質。 . 1. 若根結點的層次為i則二叉樹第i層最多有2的(i-1)次方個結點。 2.在高度為K的二叉樹中,則最多有2的K次方-1個節(jié)點(k>0) 3.設一棵二叉樹個數(shù)為n,則父節(jié)點個數(shù)n/2。 若2i+1<<>n,則i的左孩子結點為2i+1,否則i無左孩子。 若2i+2<<>n,則i的右孩子結點序號為2i+2,否則i無有孩子。

這里寫代碼片public class MyNode<E>{ MyNode<E> left; MyNode<E> right; int date; public MyNode(MyNode left,date, MyNode right){ this.left = left; this.date = date; this.right =right; }}public class BinaryTree { // 數(shù)據(jù) int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; LinkedList<MyNode<Integer>> list = new LinkedList<MyNode<Integer>>(); // 構建森林 public void genForest() { for (int i = 0; i < arr.length; i++) { MyNode<Integer> mynode = new MyNode<Integer>(null, arr[i], null); list.add(mynode); } } // 構建樹 public void genTree() { // 總結點個數(shù) int totalNum = arr.length; for (int i = 0; i < list.size(); i++) { MyNode<Integer> PReNode = list.get(i); if (2 * i + 1 < totalNum) preNode.childLeft = list.get(2 * i + 1); if (2 * i + 2 < totalNum) preNode.childRight = list.get(2 * i + 2); } } // 前序 public void preFor(MyNode<Integer> root) { if (root == null) { return; } // 我自己有沒有, System.out.print(root.date + ","); // 左邊有沒有 preFor(root.childLeft); // 右邊有沒有, preFor(root.childRight); } public void midFor(MyNode<Integer> root) { if (root == null) { return; } // 左邊有沒有 midFor(root.childLeft); // 自己有沒有 System.out.print(root.date + ","); // 右邊有沒有 midFor(root.childRight); } public void lastFor(MyNode<Integer> root) { if (root == null) { return; } // 左邊有沒有 lastFor(root.childLeft); // 右邊有沒有 lastFor(root.childRight); // 自己有沒有 System.out.print(root.date + ","); }}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 宁河县| 盘山县| 内江市| 电白县| 庆安县| 板桥市| 安远县| 象州县| 土默特左旗| 铅山县| 麻江县| 霍城县| 江华| 黔江区| 老河口市| 桂东县| 英吉沙县| 沅陵县| 隆林| 郓城县| 安达市| 东莞市| 河曲县| 鱼台县| 嘉峪关市| 襄樊市| 永兴县| 延津县| 县级市| 梅州市| 宁津县| 泸定县| 梓潼县| 赣榆县| 堆龙德庆县| 昂仁县| 静乐县| 镇沅| 石泉县| 万山特区| 泾川县|