java实现二叉查找树

package com.algorithm;


import java.util.Comparator;
import java.util.Iterator;
import java.util.Stack;

/**
 * 功能简述<p>:
 * 功能详述:
 * 
 * author:wangzha
 */
public class BinarySearchTree<T> {
    int size;
    BinaryNode<T> node;
    Comparator<? super T> c;

    BinarySearchTree(){
        size = 0;
        node = null;
        c = null;
    }

    BinarySearchTree(T key,T value){
        node = new BinaryNode<>(key,value);
        size++;
    }

    public void put(T key,T value){
        BinaryNode temp = node;
        if (temp == null) {
            node = new BinaryNode<>(key, value);
            size = 1;
            return;
        }
        /**临时父节点*/
        BinaryNode tempParent = temp;
        int res = 0;
        if (c!=null){
            do{
                /**每次变换后,重新赋值临时父节点*/
                tempParent = temp;
                c = (Comparator<? super T>) key;
                res = c.compare(key,(T) temp.key);
                /**通过key找位置*/
                if (res>0){
                    temp = temp.right;
                }else if(res<0){
                    temp = temp.left;
                }else
                    /**若key已经存在,则value覆盖*/
                    temp.value = value;
            }while(temp!=null);
        }else {
            do{
                tempParent = temp;
                Comparable<? super T> c = (Comparable<? super T>) key;
                res = c.compareTo((T) temp.key);
                if (res>0){
                    temp = temp.right;
                }else if(res<0){
                    temp = temp.left;
                }else
                    temp.value = value;
            }while(temp!=null);
        }
        /**将新增节点和临时父节点接上*/
        temp = new BinaryNode(key,value);
        if (res>0)
            tempParent.right = temp;
        else
            tempParent.left = temp;
        size++;
    }

    public T get(T key) throws Exception{
        if (key==null)
            throw new Exception("key不能为空");
        if (node==null)
            return null;
        BinaryNode temp = node;

        int res = 0;
        if (c!=null){
            c = (Comparator<? super T>) key;
            res = c.compare(key,(T)temp.key);
            do {
                if (res>0)
                    temp = temp.right;
                else temp = temp.left;
            }while (res==0);
        }else {
            Comparable<? super T> comparable = (Comparable<? super T>)key;
            res = comparable.compareTo((T)temp.key);
            do {
                if (res>0)
                    temp = temp.right;
                else temp =temp.left;
            }while (res==0);
        }
        return (T) temp.value;
    }

    private static class BinaryNode<T>{
        T key;
        T value;
        BinaryNode<T> left;
        BinaryNode<T> right;
        BinaryNode(T key,T value){
            this(key,value,null,null);
        }
        BinaryNode(T key,T value,BinaryNode left,BinaryNode right){
            this.key = key;
            this.value = value;
            this.left = left;
            this.right = right;
        }
    }

    class InnerIterator<T> implements Iterator<T>{
        Stack<BinaryNode> stack;
        /**把根节点和左节点全部放到栈里*/
        public InnerIterator() {
            BinaryNode temp = node;
            if (temp==null)
                return;
            stack = new Stack<>();
            while (temp!=null){
                stack.push(temp);
                temp = temp.left;
            }
        }

        @Override
        public boolean hasNext() {
            return !stack.isEmpty();
        }

        /**由于我们采用的是中序遍历(左中右),先从栈里推出最上面的左节点(先进后出)
         * 如果被推出的左节点没有右节点,则之间推出栈中的第二个左节点;如果被推出
         * 的左节点有右节点,则将右边的节点推入栈中*/
        @Override
        public T next() {
            BinaryNode binaryNode = stack.pop();
            BinaryNode temp = binaryNode.right;
            while (temp!=null){
              stack.push(temp);
              temp = temp.left;
            }
            return (T) binaryNode.value;
        }
    }

    public static void main(String[] args) throws Exception{
        BinarySearchTree btr = new BinarySearchTree();
        btr.put("a","aa");
        btr.put("c","aac");
        btr.put("b","aab");
        System.out.println(btr.get("c"));
        BinarySearchTree.InnerIterator innerIterator = btr.new InnerIterator();
        while (innerIterator.hasNext()){
            System.out.println(innerIterator.next());
        }

        System.out.println(btr.get("c"));
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_39471249/article/details/80058441