国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 独山县| 革吉县| 平乡县| 白水县| 邹城市| 阜新市| 迁西县| 周口市| 河南省| 江永县| 澄江县| 玉门市| 湘西| 新营市| 海林市| 临泽县| 彝良县| 永城市| 海伦市| 贵德县| 横峰县| 龙陵县| 旬阳县| 梅河口市| 漾濞| 长乐市| 梓潼县| 安平县| 孝义市| 六安市| 无棣县| 泰宁县| 泾源县| 怀宁县| 亳州市| 三亚市| 庆阳市| 堆龙德庆县| 体育| 大城县| 辽阳市|