Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 二叉搜索树,以不删除节点为顺序_Java_Binary Search Tree - Fatal编程技术网

Java 二叉搜索树,以不删除节点为顺序

Java 二叉搜索树,以不删除节点为顺序,java,binary-search-tree,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

我一直在尝试让我的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>**, ]
[,,,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,]
[,**,,,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,***,]
查看这些其他实现,并根据我的树的适当属性进行调整:


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;
    }
}
公共类二进制搜索树{
类节点扩展了KeyValuePair{
私有整数位置=-1;
私有节点leftChild=null;
私有节点rightChild=null;
private KeyValuePair=新的KeyValuePair();
//接球手和接球手
公共节点findMax(){
节点newCur=this;
while(newCur.getRightChild()!=null){
newCur=newCur.getRightChild();
}
返回newCur;
}
}
private int pos=-1;//帮助跟踪节点在树中的位置
私有大小=0;
private int largestPos=-1;
受保护的节点根=null;
专用节点findKey(密钥匹配,节点当前){
if(current==null | | current.getKey()==keyToMatch){
回流;
}else if(keyToMatch.compareTo(current.getKey())<0){
返回findKey(keyToMatch,current.getLeftChild());
}否则{
返回findKey(keyToMatch,current.getRightChild());
}
}
专用节点put(节点、节点临时){
if(node==null){
节点=温度;
}
else if(temp.getKey().compareTo(node.getKey())<0){
setLeftChild(put(node.getLeftChild(),temp));
}
else if(temp.getKey().compareTo(node.getKey())>0){
setRightChild(put(node.getRightChild(),temp));
}
返回节点;
}
@凌驾
公共值put(键、值){
if(key==null | | value==null){
返回null;
}
节点温度=新节点();
温度设定键(键);
温度设定值(值);
Node oldNode=null;
值oldVal=null;
if(root==null){
根=温度;
}否则{
oldNode=findKey(键,根);
if(oldNode!=null){//表示存在密钥匹配
oldVal=oldNode.getValue();
oldNode.setValue(值);
}
否则{
放置(根、温度);
}
}
返回oldVal;
}
私有节点移除(节点当前,密钥){
节点温度=新节点();
如果(当前==null){
返回null;
}
else if(key.compareTo(current.getKey())<0){
移除(current.getLeftChild(),键);
}
如果(key.compareTo(current.getKey())>0,则为else{
移除(current.getRightChild(),键);
}
否则{
if(current.getRightChild()==null)返回current.getLeftChild();
else if(current.getLeftChild()==null)返回current.getRightChild();
current.setKey(current.getLeftChild().findMax().getKey());
current.setValue(current.getLeftChild().findMax().getValue());
current.setLeftChild(删除(current.getLeftChild(),current.getKey());
}
回流;
}
@凌驾
公共值删除(密钥){
if(key==null | | root==null){
返回null;
}
节点电流=findKey(键,根);
值val=null;
如果(当前==null){
返回val;
}
否则{
val=current.getValue();
删除(根、键);
}
返回val;
}
公钥值对[]宽度第一遍历(){
@抑制警告(“未选中”)
KeyValuePair[]bftArray=新的KeyValuePair[largestPos];
KeyValuePair nodeVal=null;
如果(!isEmpty()){
对于(int i=1;i
通过使用调试器逐步执行此操作,您学到了什么?@DavidWallace我没有找到任何东西。。。我已经做了5个小时了。正在解决其他问题。。这恰好出现在其中一个案例下。好的,但是调试器应该立即告诉您为什么没有删除该内容。因此,分享调试器告诉你的信息会很有帮助——这会为每个想帮助你的人节省时间。@DavidWallace我的调试器没有这样说。我所做的调试工作就是在代码中的点上使用print语句来破译正在发生的事情。一切都很好。只是前面的节点(前置节点)不接受
=null
语句。老实说,我不知道我的代码中发生了什么。好吧,在这种情况下,我强烈建议学习使用适当的调试器。然后,您将能够逐步浏览代码,查看哪些部分运行,哪些部分不运行,以及每个变量在每个相关点上的值。我可以保证,如果您使用适当的调试器,您将需要几秒钟的时间来了解这里发生了什么。这基本上是作为一名程序员你能学到的最重要的技能之一。如果你成为一名专业程序员,那么正确地使用调试器将在你的职业生涯中为你节省数年的时间。