二进制搜索树按顺序前置或不删除节点

时间:2014-10-27 23:27:33

标签: java binary-search-tree

我一直试图让我的bst工作,但问题仍然存在,永远不会删除继承节点之后的节点。

[ <P, 1>, <C, 26>, <U, 3>, <B, 293>, <H, 1>, <R, 12>, <V, 42>, null, null, null, <K, 465>, <Q, 465>, <S, 42>, null, null, null, null, null, null, null, null, null, <L, 465>, null, null, null, <T, 465>, ]

[ <P, 1>, <C, 26>, **<T, 465>**, <B, 293>, <H, 1>, <R, 12>, <V, 42>, null, null, null, <K, 465>, <Q, 465>, <S, 42>, null, null, null, null, null, null, null, null, null, <L, 465>, null, null, null, **<T, 465>**, ]

并查看这些其他实现并调整我的树的适当属性:


public class BinarySearchTree<Key extends Comparable<Key>, Value extends Comparable<Value>>{

    class Node extends KeyValuePair<Key, Value> {
        private int position = -1;
        private Node leftChild = null;
        private Node rightChild = null;
        private KeyValuePair<Key, Value> pair = new KeyValuePair<Key, Value>();

        // getters and setters

        public Node findMax() {
            Node newCur = this;
            while (newCur.getRightChild() != null) {
                newCur = newCur.getRightChild();
            }
            return newCur;
        }
    }

    private int pos = -1; // helps keep track of where the node is in the tree
    private int bstSize = 0;
    private int largestPos = -1;
    protected Node root = null;


    private Node findKey(Key keyToMatch, Node current) {
        if (current == null || current.getKey() == keyToMatch) {
            return current;
        } else if (keyToMatch.compareTo(current.getKey()) < 0) {
            return findKey(keyToMatch, current.getLeftChild());
        } else {
            return findKey(keyToMatch, current.getRightChild());
        }
    }

    private Node put(Node node, Node temp) {
        if (node == null) {
            node = temp;
        }

        else if (temp.getKey().compareTo(node.getKey()) < 0) {
            node.setLeftChild(put(node.getLeftChild(), temp));
        }

        else if (temp.getKey().compareTo(node.getKey()) > 0) {
            node.setRightChild(put(node.getRightChild(), temp));
        }

        return node;
    }

    @Override
    public Value put(Key key, Value value) {
        if (key == null || value == null) {
            return null;
        }

        Node temp = new Node();
        temp.setKey(key);
        temp.setValue(value);

        Node oldNode = null;
        Value oldVal = null;

        if (root == null) {
            root = temp;
        } else {
            oldNode = findKey(key, root);

            if (oldNode != null) { // means there was a key match
                oldVal = oldNode.getValue();
                oldNode.setValue(value);
            }

            else {
                put(root, temp);
            }
        }

        return oldVal;
    }

    private Node remove(Node current, Key key) {
        Node temp = new Node();

        if(current == null) {
            return null;
        }
        else if(key.compareTo(current.getKey()) < 0) {
            remove(current.getLeftChild(), key);
        }
        else if(key.compareTo(current.getKey()) > 0) {
            remove(current.getRightChild(), key);
        }
        else {
            if (current.getRightChild() == null) return current.getLeftChild();
            else if(current.getLeftChild() == null) return current.getRightChild();
            current.setKey(current.getLeftChild().findMax().getKey());
            current.setValue(current.getLeftChild().findMax().getValue());
            current.setLeftChild(remove(current.getLeftChild(), current.getKey()));
        }

        return current;
    }

    @Override
    public Value remove(Key key) {
        if (key == null || root == null) {
            return null;
        }

        Node current = findKey(key, root);
        Value val = null;

        if(current == null) {
            return val;
        }
        else {
            val = current.getValue();

            remove(root, key);
        }
        return val;
    }

    public KeyValuePair<Key, Value>[] breadthFirstTraversal() {

        @SuppressWarnings("unchecked")
        KeyValuePair<Key, Value>[] bftArray = new KeyValuePair[largestPos];
        KeyValuePair<Key, Value> nodeVal = null;

        if (!isEmpty()) {
            for (int i = 1; i < bftArray.length + 1; i++) {
                keyPos.reset();
                if (positionFound(i)) {
                    nodeVal = findPosKey(root, i);
                    bftArray[i - 1] = nodeVal;
                } else {
                    bftArray[i - 1] = null;
                }

            }
        }

        return bftArray;
    }
}

0 个答案:

没有答案