Binary Search Tree - Java Implementation

后端 未结 6 945
陌清茗
陌清茗 2020-12-04 07:47

I\'m writing a program that utilizes a binary search tree to store data. In a previous program (unrelated), I was able to implement a linked list using an implementation pro

6条回答
  •  囚心锁ツ
    2020-12-04 08:24

    Here is a sample implementation:

    import java.util.*;
    
    public class MyBSTree implements MyTree{
        private BSTNode _root;
        private int _size;
        private Comparator _comparator;
        private int mod = 0;
    
        public MyBSTree(Comparator comparator){
            _comparator = comparator;
        }
    
        public Node root(){
            return _root;
        }
    
        public int size(){
            return _size;
        }
    
        public boolean containsKey(K key){
            if(_root == null){
                return false;
            }
    
            BSTNode node = _root;
    
            while (node != null){
                int comparison = compare(key, node.key());
    
                if(comparison == 0){
                    return true;
                }else if(comparison <= 0){
                    node = node._left;
                }else {
                    node = node._right;
                }
            }
    
            return false;
        }
    
        private int compare(K k1, K k2){
            if(_comparator != null){
                return _comparator.compare(k1,k2);
            }
            else {
                Comparable comparable = (Comparable)k1;
                return comparable.compareTo(k2);
            }
        }
    
    
        public V get(K key){
            Node node = node(key);
    
            return node != null ? node.value() : null;
        }
    
        private BSTNode node(K key){
            if(_root != null){
                BSTNode node = _root;
    
                while (node != null){
                    int comparison = compare(key, node.key());
    
                    if(comparison == 0){
                        return node;
                    }else if(comparison <= 0){
                        node = node._left;
                    }else {
                        node = node._right;
                    }
                }
            }
    
            return null;
        }
    
        public void add(K key, V value){
            if(key == null){
                throw new IllegalArgumentException("key");
            }
    
            if(_root == null){
                _root = new BSTNode(key, value);
            }
    
            BSTNode prev = null, curr = _root;
            boolean lastChildLeft = false;
            while(curr != null){
                int comparison = compare(key, curr.key());
                prev = curr;
    
                if(comparison == 0){
                    curr._value = value;
                    return;
                }else if(comparison < 0){
                    curr = curr._left;
                    lastChildLeft = true;
                }
                else{
                    curr = curr._right;
                    lastChildLeft = false;
                }
            }
    
            mod++;
            if(lastChildLeft){
                prev._left = new BSTNode(key, value);
            }else {
                prev._right = new BSTNode(key, value);
            }
        }
    
        private void removeNode(BSTNode curr){
            if(curr.left() == null && curr.right() == null){
                if(curr == _root){
                    _root = null;
                }else{
                    if(curr.isLeft()) curr._parent._left = null;
                    else curr._parent._right = null;
                }
            }
            else if(curr._left == null && curr._right != null){
                curr._key = curr._right._key;
                curr._value = curr._right._value;
                curr._left = curr._right._left;
                curr._right = curr._right._right;
            }
            else if(curr._left != null && curr._right == null){
                curr._key = curr._left._key;
                curr._value = curr._left._value;
                curr._right = curr._left._right;
                curr._left = curr._left._left;
            }
            else { // both left & right exist
                BSTNode x = curr._left;
                // find right-most node of left sub-tree
                while (x._right != null){ 
                    x = x._right;
                }
                // move that to current
                curr._key = x._key;
                curr._value = x._value;
                // delete duplicate data
                removeNode(x);
            }
        }
    
    
        public V remove(K key){
            BSTNode curr = _root;
            V val = null;
            while(curr != null){
                int comparison = compare(key, curr.key());
                if(comparison == 0){
                    val = curr._value;
                    removeNode(curr);
                    mod++;
                    break;
                }else if(comparison < 0){
                    curr = curr._left;
                }
                else{
                    curr = curr._right;
                }
            }
    
            return val;
        }
    
        public Iterator> iterator(){
            return new MyIterator();
        }
    
        private class MyIterator implements Iterator>{
            int _startMod;
            Stack> _stack;
    
            public MyIterator(){
                _startMod = MyBSTree.this.mod;
                _stack = new Stack>();
    
                BSTNode node = MyBSTree.this._root;
                while (node != null){
                    _stack.push(node);
                    node = node._left;
                }
            }
    
            public void remove(){
                throw new UnsupportedOperationException();
            }
    
            public boolean hasNext(){
                if(MyBSTree.this.mod != _startMod){
                    throw new ConcurrentModificationException();
                }
    
                return !_stack.empty();
            }
    
            public Node next(){
                if(MyBSTree.this.mod != _startMod){
                    throw new ConcurrentModificationException();
                }
    
                if(!hasNext()){
                    throw new NoSuchElementException();
                }
    
                BSTNode node = _stack.pop();
                BSTNode x = node._right;
                while (x != null){
                    _stack.push(x);
                    x = x._left;
                }
    
                return node;
            }
        }
    
        @Override
        public String toString(){
            if(_root == null) return "[]";
    
            return _root.toString();
        }
    
        private static class BSTNode implements Node{
            K _key;
            V _value;
            BSTNode _left, _right, _parent;
    
            public BSTNode(K key, V value){
                if(key == null){
                    throw new IllegalArgumentException("key");
                }
    
                _key = key;
                _value = value;
            }
    
            public K key(){
                return _key;
            }
    
            public V value(){
                return _value;
            }
    
            public Node left(){
                return _left;
            }
    
            public Node right(){
                return _right;
            }
    
            public Node parent(){
                return _parent;
            }
    
            boolean isLeft(){
                if(_parent == null) return false;
    
                return _parent._left == this;
            }
    
            boolean isRight(){
                if(_parent == null) return false;
    
                return _parent._right == this;
            }
    
            @Override
            public boolean equals(Object o){
                if(o == null){
                    return false;
                }
    
                try{
                    BSTNode node = (BSTNode)o;
                    return node._key.equals(_key) && ((_value == null && node._value == null) || (_value != null && _value.equals(node._value)));
                }catch (ClassCastException ex){
                    return false;
                }
            }
    
            @Override
            public int hashCode(){
                int hashCode = _key.hashCode();
    
                if(_value != null){
                    hashCode ^= _value.hashCode();
                }
    
                return hashCode;
            }
    
            @Override
            public String toString(){
                String leftStr = _left != null ? _left.toString() : "";
                String rightStr = _right != null ? _right.toString() : "";
                return "["+leftStr+" "+_key+" "+rightStr+"]";
            }
        }
    }
    

提交回复
热议问题