二叉搜索树。插入方法插入不正确

发布于 2024-10-21 10:18:10 字数 1650 浏览 3 评论 0原文

我遇到一个问题,二叉树中的项目插入不正确。我在每个节点中插入字符串。我想我可能做错了什么,因为我似乎总是选择错误的树。即

A、B、C

我应该有

   B
  /  \
 A    C

,但不知何故我最终得到了类似:

   C
  / \
 B   A

或不同的东西,具体取决于我在树中插入的顺序。

这是我的树类:

这是我的插入方法和插入辅助方法。你能看一下我做错了什么吗?提前致谢。

void BinarySortTree::insert(string key)
{
    if(root != NULL)
    {
        insert(key, root);
    }
    else
    {
        root = new TreeNode;
        root->item = key;
        root->left = NULL;
        root->right = NULL;
    }
}

void BinarySortTree::insert(string key, TreeNode *node)
{
    bool done = false;

    while(!done)
    {
        if(key.compare(node->item) < 0)
        {
            if(node->left != NULL)
            {
                node = node->left;
            }
            else
            {
                node->left = new TreeNode;
                node->left->item = key;
                node->left->left = NULL;
                node->left->right = NULL;
                done = true;
            }
        }
        else if(key.compare(node->item) > 0)
        {
            if(node->right != NULL)
            {
                node = node->right;
            }
            else
            {
                node->right = new TreeNode;
                node->right->item = key;
                node->right->left = NULL;
                node->right->right = NULL;
                done = true;
            }
        }
        else if(key.compare(node->item) == 0)
        {
            done = true;
        }
    }

}

I have an issue where my items in my binary tree are being inserted incorrectly. I'm inserting strings in each node. I think I might be doing something wrong because it seems like I always end up with the wrong tree. i.e.

A, B, C

I should have

   B
  /  \
 A    C

but somehow I end up with like:

   C
  / \
 B   A

or something different depending in which order I insert in my tree.

This is my tree class:

This is my insert method and insert helper method. Can you take a look and see what I'm doing wrong? Thanks in advance.

void BinarySortTree::insert(string key)
{
    if(root != NULL)
    {
        insert(key, root);
    }
    else
    {
        root = new TreeNode;
        root->item = key;
        root->left = NULL;
        root->right = NULL;
    }
}

void BinarySortTree::insert(string key, TreeNode *node)
{
    bool done = false;

    while(!done)
    {
        if(key.compare(node->item) < 0)
        {
            if(node->left != NULL)
            {
                node = node->left;
            }
            else
            {
                node->left = new TreeNode;
                node->left->item = key;
                node->left->left = NULL;
                node->left->right = NULL;
                done = true;
            }
        }
        else if(key.compare(node->item) > 0)
        {
            if(node->right != NULL)
            {
                node = node->right;
            }
            else
            {
                node->right = new TreeNode;
                node->right->item = key;
                node->right->left = NULL;
                node->right->right = NULL;
                done = true;
            }
        }
        else if(key.compare(node->item) == 0)
        {
            done = true;
        }
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绮烟 2024-10-28 10:18:10

这是因为当你从不改变之前插入的内容时,例如,如果你首先插入 C,那么你插入的下一个项目(例如 b)永远不会出现在根中,所以如果你插入“C”,“B”,“A”按照这个顺序,您将得到一棵形状如下的树:

    C
   /
  B
 /
A

您必须检查每个当前节点是否必须更改!并重新插入你的钥匙,因为你的根可能会在每次插入时改变!并且您绘制的树永远不会生成您的代码生成的唯一树的形式是给定输入的这些树:

 ABC        ACB      BAC        CBA     CAB
                     BCA        
 A          A         B            C       C            
  \          \       / \          /       /      
   B          C     A   C        B       A            
    \        /                  /         \ 
     C      B                  A           B

it's becouse when you never change what was periviusly inserted, for example if you insert C at first the next item you insert(for example b) never can take place in root so if you insert "C","B","A" in this order you will have a tree shaped like :

    C
   /
  B
 /
A

you have to check every if the current node has to be changed! and reinsert you key couse your root may change at every insert! and the tree you draw would never generate the only pissible forms of trees your code generate are these for given inputs :

 ABC        ACB      BAC        CBA     CAB
                     BCA        
 A          A         B            C       C            
  \          \       / \          /       /      
   B          C     A   C        B       A            
    \        /                  /         \ 
     C      B                  A           B
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文