二叉樹的基本操做

二叉樹的基本操做包含:ide

  判斷是否爲空,獲取節點數,先跟遍歷,中跟遍歷,後根遍歷,層級遍歷,查找元素post

二叉樹結構this

public class Node {
    Object value; //結點值
    Node leftChild;//左子樹的引用
    Node rightChild;//右子樹的引用

    public Node(Object value) {
        super();
        this.value = value;
    }

    public Node(Object value, Node leftChild, Node rightChild) {
        super();
        this.value = value;
        this.leftChild = leftChild;
        this.rightChild = rightChild;
    }

    @Override
    public String toString() {
        return "Node{" +
                "value=" + value +
                ", leftChild=" + leftChild +
                ", rightChild=" + rightChild +
                '}';
    }
}

判斷是否爲空樹:spa

   public boolean isEmpty() {
        return root == null;
    }

獲取節點數量:code

 public int size(Node root) {
        if (root == null)
            return 0;
        int left = size(root.leftChild);
        int right = size(root.rightChild);
        return left + right + 1;
    }

獲取高度:blog

    public int getHeight(Node root) {
        if (root == null)
            return 0;
        int left = getHeight(root.leftChild);
        int right = getHeight(root.rightChild);
        return Math.max(left, right) + 1;
    }

先根遍歷遞歸:遞歸

 public void preOrderTraverse(Node root) {
        if (root == null)
            return;
        //打印根節點
        System.out.print(root.value + "  ");
        //建立左子樹,進行先跟遍歷
        preOrderTraverse(root.leftChild);
        //建立右子樹,進行先跟遍歷
        preOrderTraverse(root.rightChild);
    }

中跟遞歸:get

   public void inOrderTraverse(Node root) {
        //出口
        if (root == null)
            return;
        //遍歷左子樹
        inOrderTraverse(root.leftChild);
        //遍歷根
        System.out.print(root.value + "  ");
        //遍歷右子樹
        inOrderTraverse(root.rightChild);
    }

中跟非遞歸:class

  @Override
    public void inOrderByStack() {
        Stack<Node> stack = new Stack<>();
        Node temp = root;
        while (temp != null || !stack.isEmpty()) {
            while (temp != null) {
                stack.push(temp);
                temp = temp.leftChild;
            }
            if (!stack.isEmpty()) {
                temp = stack.pop();
                System.out.print(temp.value + "  ");
                temp = temp.rightChild;
            }
        }
    }

後跟遞歸:List

 public void postOrderTraverse(Node root) {
        //出口
        if (root == null)
            return;
        //先左
        postOrderTraverse(root.leftChild);
        //再右
        postOrderTraverse(root.rightChild);
        //後根
        System.out.print(root.value + "  ");
    }

層次遍歷:

 public void levelOrderByStack() {
        if (root == null)
            return;
        Queue<Node> queue = new LinkedList<>();
        queue.add(root);
        int len;
        while ((len = queue.size()) != 0) {
            for (int i = 0; i < len; i++) {
                Node temp = queue.poll();
                System.out.print(temp.value + "  ");
                if (temp.leftChild != null)
                    queue.add(temp.leftChild);
                if (temp.rightChild != null)
                    queue.add(temp.rightChild);
            }
        }
    }

遞歸查找元素:

 public Node findKey1(int value, Node root) {
        if (root == null)
            return null;
        if (root.value.equals(value))
            return root;
        Node left = findKey1(value, root.leftChild);
        Node right = findKey1(value, root.rightChild);
        if (left != null)
            return left;
        if (right != null)
            return right;
        return null;
    }
相關文章
相關標籤/搜索