挺好:
- package com.itheima.TechnologyScore;
- /**
- * Description: 创建二叉树,并遍历,按下面的样子建二叉树
- * 1
- * 2 3
- * 4 5 6 7
- *8 9 10 11 12 13 14 15
- * @author hzChen
- * @date 2014-8-2
- */
- public class BinaryTreeTest
- {
- public static void main(String[] args) {
- Integer[] intArray = new Integer[15];
- for (int i = 0; i < 15; i++) {
- intArray[i] = i + 1;
- }
- //写的二叉树构造不限制顺序,不过为了看起来方便,构造数组从1到15
- //创建一个二叉树
- BinaryTree bt = new BinaryTree(intArray);
- String result = null;
- //前序遍历一个二叉树
- result = bt.preAllBinaryTree(bt.getRootNode(), new StringBuilder()).toString();
- System.err.println(result);
- //中序遍历一个二叉树
- result = bt.middleAllBinaryTree(bt.getRootNode(), new StringBuilder()).toString();
- System.err.println(result);
- //后序遍历一个二叉树
- result = bt.lastAllBinaryTree(bt.getRootNode(), new StringBuilder()).toString();
- System.err.println(result);
- }
- }
- /**
- *
- * <p>定义一个二叉树</p>
- */
- class BinaryTree
- {
- /** 根节点 */
- private Node rootNode = new Node();
-
-
- /**构造方法,通过传入的数组创建二叉树*/
- BinaryTree(Integer[] intArray){
- createTree(rootNode, intArray, 0);
- }
-
- /**
- * 功能描述:递归构建二叉树
- * @param parent 父节点
- * @param intArray 完整数组
- * @param index 父节点在数组中的索引值
- */
- private void createTree(Node parent, Integer[] intArray, Integer index){
- parent.setNodeValue(intArray[index]);
- if(2 * index + 1 < intArray.length){//存在左叶子
- Node leftNode = new Node(intArray[2 * index + 1]);
- parent.setLeftNode(leftNode);
- createTree(leftNode, intArray, 2 * index + 1);
- }
- if(2 * index + 2 < intArray.length){//存在右叶子
- Node rightNode = new Node(intArray[2 * index + 2]);
- parent.setRightNode(rightNode);
- createTree(rightNode, intArray, 2 * index + 2);
- }
-
-
- }
-
- /** 递归遍历而二叉树 前序 根左右 */
- public StringBuilder preAllBinaryTree(Node parent, StringBuilder sb){
- if(null != parent){//到叶子节点停止递归
- sb.append(parent.getNodeValue()+" ");
- preAllBinaryTree(parent.getLeftNode(), sb);
- preAllBinaryTree(parent.getRightNode(), sb);
- }
- return sb;
- }
-
- /** 递归遍历而二叉树 中序 左根右 */
- public StringBuilder middleAllBinaryTree(Node parent, StringBuilder sb) {
- if(null != parent){//到叶子节点停止递归
- preAllBinaryTree(parent.getLeftNode(), sb);
- sb.append(parent.getNodeValue()+" ");
- preAllBinaryTree(parent.getRightNode(), sb);
- }
- return sb;
- }
-
- /** 递归遍历而二叉树 后序 左右根 */
- public StringBuilder lastAllBinaryTree(Node parent, StringBuilder sb) {
- if(null != parent){//到叶子节点停止递归
- preAllBinaryTree(parent.getLeftNode(), sb);
- preAllBinaryTree(parent.getRightNode(), sb);
- sb.append(parent.getNodeValue()+" ");
- }
- return sb;
- }
- public Node getRootNode() {
- return rootNode;
- }
- public void setRootNode(Node rootNode) {
- this.rootNode = rootNode;
- }
- }
- /**
- * <p>定义节点</p>
- */
- class Node
- {
- private Node leftNode;
- private Node rightNode;
- private Integer nodeValue;
- Node(){
- }
- // 构造方法
- Node(Integer nodeValue)
- {
- this.leftNode = null;
- this.rightNode = null;
- this.nodeValue = nodeValue;
- }
- public Node getLeftNode() {
- return leftNode;
- }
- public void setLeftNode(Node leftNode) {
- this.leftNode = leftNode;
- }
- public Node getRightNode() {
- return rightNode;
- }
- public void setRightNode(Node rightNode) {
- this.rightNode = rightNode;
- }
- public Integer getNodeValue() {
- return nodeValue;
- }
- public void setNodeValue(Integer nodeValue) {
- this.nodeValue = nodeValue;
- }
- }
复制代码 |