【Java】二叉树的实现

发布时间:2019-07-17 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了【Java】二叉树的实现脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

泛型简易实现

public class Tree<T> {
    private Node<T> root;   //根节点

    public Tree(Node<T> root) {
        this.root = root;
    }

    /*二叉树的节点*/
    private static class Node<T> {
        T element;  //节点的值
        Node<T> lchild, rchild; //左右孩子节点

        public Node(T element, Node<T> lchild, Node<T> rchild) {
            this.element = element;
            this.lchild = lchild;
            this.rchild = rchild;
        }   
    }

    /*先序遍历*/
    public void preorder(Node<T> root   ) {
        if(root != null) {
            System.out.println(root.element);
            preorder(root.lchild);
            preorder(root.rchild);
        }
    }

    /*main方法用于测试*/
    public static void main(String[] args) {
        Node<String> lchild = new Node<String>("B", null, null);
        Node<String> rchild = new Node<String>("C", null, null);
        Node<String> root = new Node<String>("A", lchild, rchild);

        Tree<String> tree = new Tree<String>(root);
        tree.preorder(tree.root);

    }
}

二叉查找树

插入

public class Tree<T extends Comparable<T>> {
    /*二叉树的节点*/
    private static class Node<T> {
        T element;  //节点的值
        Node<T> lchild, rchild; //左右孩子节点

        public Node(T element, Node<T> lchild, Node<T> rchild) {
            this.element = element;
            this.lchild = lchild;
            this.rchild = rchild;
        }   
    }

     Node<T> root = null;   //根节点

    public Tree(Node<T> root) {
        this.root = root;
    }   

    /*插入节点*/
    protected Node<T> insert(Node<T>root, Node<T>newNode) {
        if(root == null) {
            root = newNode;
        } else if (root.element.compareTo(root.element) < 0) {
            root.lchild = insert(root.lchild, newNode);
        } else {
            root.rchild = insert(root.rchild, newNode);
        }
        return root;
    }

    public void insert(T data) {
        Node<T> newNode = new Node<T>(data, null, null);
        root = insert(root, newNode);
    }

    /*先序遍历*/
    public void preorder(Node<T> root   ) {
        if(root != null) {
            System.out.println(root.element);
            preorder(root.lchild);
            preorder(root.rchild);
        }
    }

    /*main方法用于测试*/
    public static void main(String[] args) {
        Tree<String> tree = new Tree<String>(null);
        tree.insert("C");
        tree.insert("B");
        tree.insert("A");
        tree.preorder(tree.root);
    }
}

先序遍历迭代器

    /** Returns a preorder iterator for this tree. */ 
     public Iterator<T> preorderIterator() { 
     return new PreorderIterator(); 
     } 

    /*** inner class for a preorder iterator ***/
    private class PreorderIterator implements Iterator<T> {
        private Node<T> nextNode;

        private PreorderIterator() {
            // The traversal starts with the root node.
            nextNode = root;
        }

        public boolean hasNext() {
            return (nextNode != null);
        }

        public T next() {
            if (nextNode == null)
                throw new NoSuchElementException();

            // Store a copy of the key to be returned.
            T element = nextNode.element;

            // Advance nextNode.
            if (nextNode.lchild != null)
                nextNode = nextNode.lchild;
            else if (nextNode.rchild != null)
                nextNode = nextNode.rchild;
            else {
                // We've just visited a leaf node.
                // Go back up the tree until we find a node
                // with a right child that we haven't seen yet.
                Node parent = nextNode.parent;
                Node child = nextNode;
                while (parent != null
                        && (parent.rchild == child || parent.rchild == null)) {
                    child = parent;
                    parent = parent.parent;
                }

                if (parent == null)
                    nextNode = null; // the traversal is complete
                else
                    nextNode = parent.rchild;
            }

            return element;
        }

        @Override
        public void remove() {
            // TODO Auto-generated method stub  
        }
    }

脚本宝典总结

以上是脚本宝典为你收集整理的【Java】二叉树的实现全部内容,希望文章能够帮你解决【Java】二叉树的实现所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: