java:二叉树(完全二叉树)

1.二叉树节点:
包括左子树,右子树。数据。

	//二叉树节点类
	private static class Node {  
        Node leftChild;  
        Node rightChild;  
        int data;  
  
        Node(int newData) {  
            leftChild = null;  
            rightChild = null;  
            data = newData;  
        }  
	}

2.将数据创建成二叉树

private int[] array = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9};

3.存储二叉树,用链表做

private static List nodeList = null;
nodeList = new LinkedList();
        for (int nodeIndex = 0; nodeIndex < array.length; nodeIndex++) {  
            nodeList.add(new Node(array[nodeIndex]));  
        }

3.按照节点更新创建二叉树

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);  
        }  
                // 最后一个父节点:因为最后一个父节点可能没有右孩子,所以单独拿出来处理  
        int lastParentIndex = array.length / 2 - 1;  
        // 左孩子  
        nodeList.get(lastParentIndex).leftChild = nodeList  
                .get(lastParentIndex * 2 + 1);  
        // 右孩子,如果数组的长度为奇数才建立右孩子  
        if (array.length % 2 == 1) {  
            nodeList.get(lastParentIndex).rightChild = nodeList  
                    .get(lastParentIndex * 2 + 2);  
        }

4,遍历

    //前序遍历(递归法)
    public static void preOrderTraverse(Node head) {  
        if (head == null)  
            return;  
        System.out.print(head.data + " ");//根  
        preOrderTraverse(head.leftChild);//根左   
        preOrderTraverse(head.rightChild); //根右
    }
    //中序遍历
    public static void inOrderTraverse(Node head) {  
        if (head == null)  
            return;  
        inOrderTraverse(head.leftChild);  
        System.out.print(head.data + " ");  
        inOrderTraverse(head.rightChild);  
    }
    //后续遍历
    public static void postOrderTraverse(Node head) {  
        if (head == null)  
            return;  
        postOrderTraverse(head.leftChild);  
        postOrderTraverse(head.rightChild);  
        System.out.print(head.data + " ");  
    }

5.二叉树深度

    //二叉树深度
    public static int deep(Node root) {
    	if(root == null) {
    		return 0;
    	}
    	int left = deep(root.leftChild);
    	int rigth = deep(root.rightChild);
    	return Math.max(left, rigth) + 1;
    }

6.应用

public static void main(String[] args) {  
    	NodeTree binTree = new NodeTree();  
        binTree.createBinTree(); 
        
        // nodeList中第0个索引处的值即为根节点  
        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);
        
        System.out.println();  
        System.out.println(deep(root));  
    }  

输出:
先序遍历:
1 2 4 8 5 3 6 7
中序遍历:
8 4 2 5 1 6 3 7
后序遍历:
8 4 5 2 6 7 3 1
4

你可能感兴趣的:(java,java,二叉树)