泛型简易实现

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  
        }
    }

lifehack
134 声望6 粉丝