java二叉树

来源:互联网 发布:nginx 密码 编辑:程序博客网 时间:2024/06/14 01:10

java二叉树

节点

package com.ghg.data_structure.tree;public class Node {    public Integer value;    public Node leftChild;    public Node rightChild;    public Node() {        super();    }    public Node(Integer value) {        super();        this.value = value;    }    public Node(Integer value, Node leftChild, Node rightChild) {        super();        this.value = value;        this.leftChild = leftChild;        this.rightChild = rightChild;    }    public void display(){        System.out.print(this.value+" \t ");    }    @Override    public String toString() {        return "Node [value=" + value + "]";    }}

package com.ghg.data_structure.tree;public class BinaryTree {    public Node root;    /**     * 初始化     */    public BinaryTree(int value) {        root = new Node(value);        root.leftChild = null;        root.rightChild = null;    }    /**     * 插入     *      * @param value     * @return     */    public void insert(int value) {        Node newNode = new Node(value);        if (root == null) {            root = newNode;            root.leftChild = null;            root.rightChild = null;        } else {            /**             * 当前             */            Node current = root;            /**             * 父节点             */            Node parent = null;            while (true) {                if (value < current.value) {                    /**                     * 重新赋值                     */                    parent = current;                    current = current.leftChild;                    if (current == null) {                        parent.leftChild = newNode;                        return;                    }                } else if (value > current.value) {                    parent = current;                    current = parent.rightChild;                    if (current == null) {                        parent.rightChild = newNode;                        return;                    }                } else {                    /**                     * 等于当前节点的值                     */                    // throw new IllegalArgumentException("having same value in                    // binary tree");                    /**                     * 放在左侧                     */                    parent = current;                    current = parent.leftChild;                    parent.leftChild = newNode;                    newNode.leftChild = current;                    return;                }            }        }    }    /**     * 前序遍历     */    public void preOrderTraverse() {        System.out.println("================前序遍历  ========");        preOrderTraverse(root);        System.out.println();    }    /**     * 递归前序     *      * @param node     */    public void preOrderTraverse(Node node) {        if (node == null) {            return;        }        node.display();        /**         * 先遍历左侧         */        preOrderTraverse(node.leftChild);        /**         * 再遍历右侧         */        preOrderTraverse(node.rightChild);    }    /**     * 中序遍历递归操作     */    public void inOrderTraverse() {        System.out.println("================中序遍历  ========");        inOrderTraverse(root);        System.out.println();    }    public void inOrderTraverse(Node node) {        if (node == null) {            return;        }        inOrderTraverse(node.leftChild);        node.display();        inOrderTraverse(node.rightChild);    }    public void postOrderTraverse() {        System.out.println("================后序遍历  ========");        postOrderTraverse(root);        System.out.println();    }    public void postOrderTraverse(Node node) {        if (node == null) {            return;        }        postOrderTraverse(node.leftChild);        postOrderTraverse(node.rightChild);        node.display();    }    /**     * 查找     *      * @param value     * @return     */    public Node findKey(int value) {        Node curret = root;        while (true) {            if (value == curret.value) {                return curret;            } else if (value > curret.value) {                curret = curret.rightChild;            } else if (value < curret.value) {                curret = curret.leftChild;            }            if (curret == null) {                return null;            }        }    }    /**     * 获取小值     *      * @return     */    public Node getMin() {        Node current = root;        while (true) {            if (current.leftChild == null) {                return current;            }            current=current.leftChild;        }    }    /**     * 获取最大值     * @return     */    public Node getMax(){        Node current=root;        while(true){            if(current.rightChild==null){                return current;            }            current=current.rightChild;        }    }}

测试

package com.ghg.data_structure.tree;public class Test1 {    public static void main(String[] args) {        BinaryTree binaryTree = new BinaryTree(1);        binaryTree.insert(3);        binaryTree.insert(-5);        binaryTree.insert(-5);        binaryTree.insert(3);        binaryTree.insert(1);        binaryTree.insert(-212);        binaryTree.insert(255);        binaryTree.preOrderTraverse();        binaryTree.inOrderTraverse();        binaryTree.postOrderTraverse();        System.out.println(binaryTree.findKey(1));        System.out.println("max : "+binaryTree.getMax());        System.out.println("min : "+binaryTree.getMin());    }}

结果

================前序遍历  ========1    1   -5      -5      -212    3   3   255     ================中序遍历  ========-212     -5      -5      1   1   3   3   255     ================后序遍历  ========-212     -5      -5      1   3   255     3   1   Node [value=1]max : Node [value=255]min : Node [value=-212]