1. 程式人生 > 其它 >HMJAVA資料結構與演算法6【樹基礎、二叉樹】

HMJAVA資料結構與演算法6【樹基礎、二叉樹】

1、樹的基本定義

2、樹的相關術語

3、二叉樹的基本定義

4、二叉查詢樹的建立

5、二叉樹的基礎遍歷

6、二叉樹的層序遍歷

7、二叉樹的最大深度問題

//獲取整個樹的最大深度
    public int maxDepth(){
        return maxDepth(root);
    }


    //獲取指定樹x的最大深度
    private int maxDepth(Node x){
        if (x==null){
            return 0;
        }
        //x的最大深度
int max=0; //左子樹的最大深度 int maxL=0; //右子樹的最大深度 int maxR=0; //計算x結點左子樹的最大深度 if (x.left!=null){ maxL = maxDepth(x.left); } //計算x結點右子樹的最大深度 if (x.right!=null){ maxR = maxDepth(x.right); } //比較左子樹最大深度和右子樹最大深度,取較大值+1即可
max = maxL>maxR?maxL+1:maxR+1; return max; }
View Code
//測試程式碼
public class Test {
    public static void main(String[] args) throws Exception {
        BinaryTree<String, String> bt = new BinaryTree<>();
        bt.put("E", "5");
        bt.put("B", "2");
        bt.put(
"G", "7"); bt.put("A", "1"); bt.put("D", "4"); bt.put("F", "6"); bt.put("H", "8"); bt.put("C", "3"); int i = bt.maxDepth(); System.out.println(i); } }
View Code

8、摺紙問題

package cn.itcast.algorithm.test;

import cn.itcast.algorithm.linear.Queue;

public class PagerFoldingTest {

    public static void main(String[] args) {

        //模擬這隻過程,產生樹
        Node<String> tree = createTree(2);
        //遍歷樹,列印每個結點
        printTree(tree);

    }

    //通過模擬對摺N次紙,產生樹
    public static Node<String> createTree(int N){
        //定義根結點
        Node<String> root=null;
        for (int i = 0; i < N; i++) {

            //1.當前是第一次對摺
            if (i==0){
                root = new Node<>("down",null,null);
                continue;
            }
            //2.當前不是第一次對摺
            //定義一個輔助佇列,通過層序遍歷的思想,找到葉子結點,葉子結點新增子節點
            Queue<Node> queue = new Queue<>();
            queue.enqueue(root);

            //迴圈遍歷佇列
            while(!queue.isEmpty()){
                //從佇列中彈出一個結點
                Node<String> tmp = queue.dequeue();
                //如果有左子結點,則把左子結點放入到佇列中
                if (tmp.left!=null){
                    queue.enqueue(tmp.left);
                }
                //如果有右子結點,則把右子結點放入到佇列中
                if (tmp.right!=null){
                    queue.enqueue(tmp.right);
                }
                //如果同時沒有左子結點和右子結點,那麼證明該節點是葉子結點,只需要給該節點新增左子結點和右子結點即可
                if (tmp.left==null && tmp.right==null){
                    tmp.left = new Node<String>("down", null,null);
                    tmp.right = new Node<String>("up",null,null);
                }
            }
        }
        
        return root;
    }


    //列印樹中每個結點到控制檯
    public static void printTree(Node<String> root){
        //需要使用中序遍歷完成
        if (root==null){
            return;
        }

        //列印左子樹的每個結點
        if (root.left!=null){
            printTree(root.left);
        }
        //列印當前結點
        System.out.print(root.item+" ");
        //列印右子樹的每個結點
        if (root.right!=null){
            printTree(root.right);
        }

    }



    //結點類
    private static class Node<T>{
        public T item;//儲存元素
        public Node left;
        public Node right;

        public Node(T item, Node left, Node right) {
            this.item = item;
            this.left = left;
            this.right = right;
        }
    }


}
View Code