【问题标题】:Infinite loop while inserting nodes to BST将节点插入 BST 时出现无限循环
【发布时间】:2017-05-13 15:28:40
【问题描述】:

我有一种根据字母顺序在 BST 中插入节点的方法,但是当我比较 2 个字符串时我有一个无限循环我认为当它通过比较时值永远不会改变,所以它再次与相同值导致无限循环。我认为auxTnodes 没有使用递归方法更新值,因此它会一遍又一遍地比较相同的值。

class BST {

    BSTNode root;

    public BST() {
        root = null;
    }

    BSTNode aux = new BSTNode();

    BSTNode insertNames(BSTNode T , int data, String name, double salary) {
        if (root == null) {
            T = new BSTNode();  
            T.setName(name);
            root = T;
        } else {
            aux = root;

            if (name.compareTo(aux.getName()) < 0)
                aux.setLeft(insertNames(aux.getLeft(),data, name, salary));
            else if (name.compareTo(aux.getName()) >= 0)
                aux.setRight(insertNames(aux.getRight(),data, name, salary));
        }

        return T;
    }    
}

class Main{
public static void main(String[] args){

        BST alpha=new BST();
        BSTNode root = new BSTNode();
        alpha.insertNames(root, 0, "Roy", 0);
        alpha.insertNames(root, 0, "Joseph", 0);
}
}

【问题讨论】:

  • 逐行调试代码时发现了什么?这应该告诉你为什么它有一个无限循环。
  • 值没有改变,但我不知道为什么,当它到达else 语句中的第一个if 时,它会将值插入到aux 的左侧再次出现的递归部分方法内部的参数没有改变

标签: java data-structures binary-search-tree


【解决方案1】:
package com.gati.dsalgo.string;

class BST {

    BSTNode root;

    public BST() {
        root = null;
    }

    void insertNames(int data, String name, double salary) {
        root = insertNames(root, data, name, salary);
    }

    BSTNode insertNames(BSTNode root, int data, String name, double salary) {
        if (root == null) {
            root = new BSTNode();
            root.setName(name);
            return root;
        }

        if (name.compareTo(root.getName()) < 0)
            root.setLeft(insertNames(root.getLeft(), data, name, salary));
        else if (name.compareTo(root.getName()) >= 0)
            root.setRight(insertNames(root.getRight(), data, name, salary));

        return root;
    }
}

public class Main1 {
    public static void main(String[] args) {

        BST alpha = new BST();
        alpha.insertNames(0, "Roy", 0);
        alpha.insertNames(0, "Joseph", 0);
        System.out.println("hello");
    }
}

class BSTNode {
    private String name;
    BSTNode left;
    BSTNode right;

    public void setName(String name) {
        this.name = name;

    }

    public void setRight(BSTNode right) {
        this.right = right;
    }

    public void setLeft(BSTNode left) {
        this.left = left;
    }

    public BSTNode getRight() {

        return right;
    }

    public BSTNode getLeft() {
        return left;
    }

    public String getName() {

        return name;
    }

}

【讨论】:

    【解决方案2】:

    请在递归结束逻辑中返回节点。

           /* If the tree is empty, return a new node */
    if (root == null) {
        root = new BSTNode();
        root.setName(name);
        return root;
    }
    
    /* Otherwise, recur down the tree */
    if (name.compareTo(root.getName()) < 0)
        root.setLeft(insertNames(root.getLeft(), data, name, salary));
    else if (name.compareTo(aux.getName()) >= 0)
        root.setRight(insertNames(root.getRight(), data, name, salary));
    
    /* return the (unchanged) node pointer */
    return root;
    

    也可以迭代求解

     if (localRoot == null) {
        newNode = new Node < V > (value, null);
        root = newNode;
        size++;
        return true;
    }
    if (comparator != null) {
    //Some code
    } else {
        Comparable << ? super V > v = (Comparable << ? super V > ) value;
        while (localRoot != null) {
            parent = localRoot;
            cmp = v.compareTo(localRoot.getValue());
            if (cmp < 0) {
                localRoot = localRoot.getLeftChield();
            } else if (cmp > 0) {
                localRoot = localRoot.getRightChield();
            } else {
                localRoot.incrementBy(nCopies);
                return true;
            }
        }
        newNode = new Node < V > (value, parent);
        if (cmp < 0) {
            parent.setLeftChield(newNode);
        } else if (cmp > 0) {
            parent.setRightChield(newNode);
        }
        size++;
        return true;
    }
    return false;
    

    【讨论】:

    • 我尝试了递归方式,但我仍然遇到由无限循环产生的相同堆栈溢出错误,我将 main 类添加到描述中,我很确定 @987654324 alpha.insertNames中的@参数是正确的,我也检查了调试器,一切似乎都很好。
    • 我一步一步调试并意识到值永远不会改变,我总是在比较相同的,所以它确实做了if句子root.setLeftroot.setRight中的内容,但值当它再次出现时不要改变,所以它会一遍又一遍地做同样的事情。
    • 我已经调试了你的代码根,你的返回是不同的,然后根你的检查。 BSTNode T 参数应检查递归结束条件。
    猜你喜欢
    • 2017-06-14
    • 1970-01-01
    • 2018-06-15
    • 1970-01-01
    • 1970-01-01
    • 2019-09-06
    • 1970-01-01
    • 2016-01-16
    • 1970-01-01
    相关资源
    最近更新 更多