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

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

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

2019-11-09 20:41:57
字體:
來源:轉載
供稿:網(wǎng)友

完全二叉樹性質。 . 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ā)表
主站蜘蛛池模板: 伊宁市| 嘉鱼县| 布拖县| 日土县| 中阳县| 台中县| 清苑县| 嵊泗县| 巴马| 广昌县| 阳原县| 尉犁县| 嘉义县| 嵊泗县| 西丰县| 焦作市| 郯城县| 大安市| 清水河县| 林芝县| 永平县| 青岛市| 邯郸市| 乌苏市| 新竹县| 分宜县| 昌宁县| 镇江市| 克拉玛依市| 临夏县| 区。| 常熟市| 元谋县| 双牌县| 界首市| 建宁县| 许昌市| 那坡县| 吐鲁番市| 增城市| 兴仁县|