【二叉树】java源码实现

BiTree 是 Binary Tree 的缩写,中文意思是 二叉树

Node.java

public class Node {
    public T data;
    public Node left;
    public Node right;

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


    public Node(T data) {
        this.data = data;
    }


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

    public Node() {
    }
}

BiTree.java

public class BiTree {
    public Node root;

    public BiTree() {
    }

    public BiTree(Node root) {
        this.root = root;
    }
    public BiTree(T data, Node lp , Node rp) {
        this.root = new Node(data,lp,rp);
    }
    public boolean isEmpty() {
        return root == null;
    }
    public Node getRoot() {
        return root;
    }
    public Node getLeft(Node p) {
        return p.left;
    }
    public Node getRight(Node p) {
        return p.right;
    }
    public void insertL(T val,Node p) {
        Node tmp = new Node(val);
        tmp.left = p.left;
        p.left = tmp;

    }
    public void insertR(T val,Node p) {
        Node tmp = new Node(val);
        tmp.right = p.right;
        p.right = tmp;
    }
    public Node deleteL(Node p) {
        if ((p==null) || (p.left == null)) {
            return null;
        }
        Node tmp = p.left;
        tmp.left = null;
        return tmp;
    }
    public Node deleteR(Node p) {
        if ((p==null) || (p.right == null)) {
            return null;
        }
        Node tmp = p.right;
        tmp.right = null;
        return tmp;
    }
    public boolean isLeaf(Node p) {
        if ((p != null) && (p.left == null) && (p.right == null))
        {
            return true;
        }
        else{
            return false;
        }
    }
//    前序
    public void preOrder(Node root) {
        if (root == null) {
            return;
        }
        System.out.println(root.data);
        preOrder(root.left);
        preOrder(root.right);
    }
//    中序遍历
    public void inOrder(Node root) {
        if (root == null) {
            return;
        }
        inOrder(root.left);
        System.out.println(root.data);
        inOrder(root.right);
    }
//    后序遍历
    public void postOrder(Node root) {
        if (root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.println(root.data);
    }
//    深度
    public int getDepth(Node root) {
        if (root == null) {
            return 0;
        }
        int leftDepth = getDepth(root.left);
        int rightDepth = getDepth(root.right);
        return leftDepth > rightDepth ? leftDepth + 1 : rightDepth + 1;
    }
    public void getParent(Node cur, Node target) {
        if (cur == null) {
            return;
        }
        if (cur.left == target || cur.right == target) {
            System.out.println(cur.data);
            return;
        }
        getParent(cur.left, target);
        getParent(cur.right, target);
    }
    public int countNode(Node p){
        if (p == null) {
            return 0;
        }
        return 1+countNode(p.left)+countNode(p.right);
    }
    public int countleaves(Node p){
        if (p == null) {
            return 0;
        }
        if (isLeaf(p)) {
            return 1;
        }
        return countleaves(p.left)+countleaves(p.right);
    }
}

练习:

【二叉树】java源码实现_第1张图片

BiTreeMain.java

public class BiTreeMain {
    public static void main(String[] args) {
        // 创建根节点(根节点为 'A')
        Node root = new Node('A');
        BiTree tree = new BiTree(root);

        // 插入左孩子 'B' 和右孩子 'C'
        tree.insertL('B', root);
        tree.insertR('C', root);

        // 获取左右孩子节点
        Node leftChild = tree.getLeft(root); // 'B'
        Node rightChild = tree.getRight(root); // 'C'

        // 继续插入节点
        tree.insertL('D', leftChild);  // 'B' 的左孩子是 'D'
        tree.insertR('E', leftChild);  // 'B' 的右孩子是 'E'
        tree.insertL('F', rightChild); // 'C' 的左孩子是 'F'
        tree.insertR('G', rightChild); // 'C' 的右孩子是 'G'

        System.out.println("前序遍历:");
        tree.preOrder(root); // A, B, D, E, C, F, G

        System.out.println("\n中序遍历:");
        tree.inOrder(root); // D, B, E, A, F, C, G

        System.out.println("\n后序遍历:");
        tree.postOrder(root); // D, E, B, F, G, C, A

        System.out.println("\n树的深度:");
        System.out.println(tree.getDepth(root)); // 3

//        输出所有节点数
        System.out.println("\n树的节点个数:");
        System.out.println(tree.countNode(root));
//        输出所有叶子节点
        System.out.println("\n树的树叶节点个数:");
        System.out.println(tree.countleaves(root));

    }
}

你可能感兴趣的:(算法,数据结构)