当前位置: 首页 > 知识库问答 >
问题:

实现二进制搜索树的add方法

经博延
2023-03-14

我和我的搭档正在为数据结构实现二进制搜索树

public class BinarySearchTree<Type extends Comparable<? super Type>> implements SortedSet<Type>
{


BinaryNode<Type> thisRoot;

/**
 * Constructor for this BinarySearchTree
 */
public BinarySearchTree()
{
    thisRoot = null;
}

/**
 * Adds the specified item to this BinarySearchTree, if it is
 * not already contained in this BinarySearchTree.
 * 
 * @param Type item
 * 
 * @return boolean
 */
public boolean add(Type item) {

    // If the specified item is null, throw an exception.
    if(item == null)
        throw new NullPointerException();

    // Otherwise, add the item.
    return addItem(item, thisRoot);

}

private boolean addItem(Type item, BinaryNode<Type> thisRoot)
{
    // Base case - check if thisRoot is null.  If it is null,
    // we have reached the base case where the item is not contained
    // in this BinarySearchTree.  Insert the item.
    if(thisRoot == null)
    {   
        thisRoot = new BinaryNode<Type>(item);
        return true;
    }

    // Reduction step - recursively call the helper method until the
    // specified item is found or added.

    // If the item is less than the data in thisNode, then go to 
    // the left in this BinarySearchTree.
    if(item.compareTo(thisRoot.getData()) < 0)
        return addItem(item, thisRoot.getLeft());

    // If the item is greater than the data in thisNode, then go
    // to the right in this BinarySearchTree.
    else if (item.compareTo(thisRoot.getData()) > 0)
        return addItem(item, thisRoot.getRight());
    else
        // Item is already contained in this BinarySearchTree.
        return false;

}

在我们的测试案例中,我们没有得到我们预期的结果。我们最初创建了一个空的BinarySearchTree,并调用add方法。从这里,我们将一个整数对象(10)传递给该方法。完成此操作后,应该调用递归addItem方法。thisRoot当前应该引用null(因为我们创建了一个空的BinarySearchTree),因此thisRoot现在应该引用新的BinaryNode对象。但是,在方法调用之后,BST中不包含10。thisRoot仍然指向null。如果有人对原因有任何建议或见解,我们将不胜感激。

共有2个答案

贡和裕
2023-03-14

这是一种相对简单的方法

此外,如果您看到任何未声明的数据类型,请考虑将其添加到完整代码的某个地方。

   public void addNode (int key, String name) {

    // Create a new Node and initialize it

    Node newNode = new Node(key, name);

    if (root == null) {

        root = newNode;

    } else {

       Node focusNode = root;
        Node parent;

        while (true) {



            parent = focusNode;

            // Check if the new node should go on
            // the left side of the parent node

            if (key < focusNode.key) {

                // Switch focus to the left child

                focusNode = focusNode.leftChild;


                if (focusNode == null) {


                    parent.leftChild = newNode;
                    return; 

                }

            } else {  the right

                focusNode = focusNode.rightChild;



                if (focusNode == null) {



                    parent.rightChild = newNode;
                    return; 
漆雕硕
2023-03-14

addItem方法中,thisRoot只是一个局部变量(绑定到该方法的第二个参数)。重置它不会改变任何东西,除了方法内部。您必须分配新的二进制节点

(如果我含糊其辞,那是因为我不想给出答案。)

 类似资料:
  • 问题内容: 我正在编写一个使用二进制搜索树存储数据的程序。在以前的程序中(无关),我能够使用Java SE6随附的实现来实现链表。二进制搜索树是否有类似的东西,还是我需要“从头开始”? 问题答案: 您可以使用。被实现为一棵红黑树,这是一个自平衡二进制搜索树。

  • 我正在尝试实现一个二叉查找树,但是“搜索”函数对于除了根之外的每个条目都返回了错误的值。 该函数应返回其值与键参数匹配的节点的地址,如果节点不存在,则返回 NULL。 当我运行代码时,我得到以下内容: 我知道每个节点的“左”和“右”指针链接正确,因为“delAll”函数成功删除了所有节点。 将“cout”语句添加到“search”函数表明该函数似乎返回了正确的地址。为什么从主主调用时会打印错误的地

  • 我正在一个实验室工作,该实验室要求我为二进制搜索树创建一个删除方法。这是我的remove方法的代码。 运行代码时得到的输出是: 移除90后的树。70 80 85 98 100 120 移除70. 80 85 98 100 120后的树 移除85后的树。80 98 100 120 移除98后的树。80 100 120 移除80后的树。100 120 移除120后的树。100 移除100后的树。100

  • 我在C中实现了一个二进制搜索树。 对于delete方法,除了最后一种情况外,其他情况都可以使用,即唯一的树是父树,并且它指向两个空的子树。现在的问题是:我希望在删除子树后,打印出父树的左子树和右子树等于什么。它们和父项都应该为NULL,但是当我试图输出这些值时,我得到了一个状态访问冲突。 下面是有关删除的代码。我希望删除父节点,并设置树- 主要:

  • 我使用这个二进制搜索函数得到一个较大数据集的索引错误。当我输入一个较小的数据集时,即[1,2,3,4,5]搜索5。算法按预期运行。但是,当我获取下面的文本时,使用空参数列表(delimeter char为“”)调用string对象的split方法,并将字符串拆分为列表值,其中每个元素都是字符串,然后搜索单词“culpa”,我最终会出现以下错误: 索引错误:列表索引超出范围 非常感谢你的帮助。非常感

  • 我正在尝试使用递归在python中实现二进制搜索树。我被困在程序中发生的一些无限递归中。我通过传递地址和数据对函数RecursBST进行递归调用,直到顶部遍历到它的左或右子级的None值为止。 我运行到以下错误: