简体   繁体   English

为什么插入BST两次插入根?

[英]Why does Insertion into BST insert root twice?

I have created a binary search tree class. 我创建了一个二进制搜索树类。 I created my insertion method, height method, and print method. 我创建了插入方法,高度方法和打印方法。 When I insert, everything looks fine. 当我插入时,一切看起来都很好。 IF the root is null, I create a new root and set the item. 如果根为空,则创建一个新的根并设置该项。 But when I call my height method, it prints out 2 instead of 1. When I call my print method, it prints out all the elements including root twice. 但是,当我调用我的height方法时,它打印出2而不是1。当我调用我的print方法时,它打印出包括root在内的所有元素两次。 For example, I inserted the following elements in the following order: 9, 5, 4, 55, 555 例如,我按以下顺序插入了以下元素:9、5、4、55、555

When I call my PREorderPRINT method, it prints out: 9, 5, 4, 9, 55, 555 当我调用PREorderPRINT方法时,它会打印出:9,5,4,9,55,555

The values printed are correct except for the duplicate 9 in the middle which I never inserted. 打印的值是正确的,除了中间从未出现过的重复项9。 My insert method does NOT use recursion. 我的插入方法不使用递归。 But my height and print methods use recursion. 但是我的身高和打印方法使用递归。

My recursive preOrderPrint checks if root!=null, then it prints item, gets left, and gets right. 我的递归preOrderPrint检查root!= null,然后打印项目,左移,右移。 When I call preorder in my public preorder method, I first check it tree is empty, if not, then I print it by passing in root to preOrderPRint(root) 在我的公共预订方法中调用预订时,我首先检查树是否为空,如果不是,则通过将根传递给preOrderPRint(root)进行打印

My recursive height method checks if root is null and returns zero. 我的递归高度方法检查root是否为null并返回零。 If not, it gets height of left and right subtrees of root, compares them, and returns either left+1 or right+1. 如果不是,它将获取根的左和右子树的高度,对其进行比较,然后返回left + 1或right + 1。 When I call my public height method, i check if root is null and return zero, else call recursive height passing in root. 当我调用public height方法时,我检查root是否为null并返回零,否则调用递归高度传递给root。

When I insert one element, 9, into the tree, and call my height method, it prints out the height is 2. But my size method prints out the correct size, 1. There has to be something wrong with my recursive preorderPrint and height methods. 当我在树中插入一个元素9并调用我的height方法时,它会打印出高度为2。但是我的size方法会打印出正确的大小为1。我的递归preorderPrint和height一定有问题方法。 I can't seem to figure out what I'm missing. 我似乎无法弄清楚我所缺少的。 Is there a special case I forgot to add? 我有没有忘记添加的特殊情况?

EDIT: I posted code and deleted it. 编辑:我张贴代码并将其删除。 Fixed the problem. 解决了问题。 All I had to do was change an else to an else-if and add a condition to compare root with item being inserted. 我要做的就是将else更改为else-if,并添加条件以将root与要插入的项目进行比较。 A silly mistake by me. 我一个愚蠢的错误。

My problem was in my insertion method. 我的问题出在我的插入方法上。 My first case checked to see if the root node was null. 我的第一种情况检查根节点是否为空。 If it is null, it creates a node equal to the root and sets the item. 如果为null,则创建一个等于根的节点并设置项目。

My second case compared my root and checked to see if item being inserted was less than root, if so, it "gets" the node to the left. 我的第二种情况比较了我的根,并检查要插入的项是否小于根,如果是,则“获取”左侧的节点。 After this, if the item being inserted is greater than the root, it "gets" the node to the right. 此后,如果要插入的项目大于根,它将“获得”该节点到右侧。

Next I had another if-else block that set the item to the left if item was less than root. 接下来,我还有另一个if-else块,如果item小于root,则将该项设置为左侧。 After this, I only had an else statement that set the item to the node to the right regardless if it was already inserted in the root==null case. 此后,我只有一个else语句,可以将该项设置为右侧的节点,无论该项是否已经插入root == null情况下。

To fix my problem, I had to change this else statement to an if-else statement and add a condition that checks to see if item being inserted is greater than root. 为了解决我的问题,我不得不将此else语句更改为if-else语句,并添加条件以检查要插入的项是否大于root。 This prevented the root from being inserted twice. 这样可以防止将根插入两次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM