简体   繁体   English

C ++使用数组插入二进制搜索树

[英]C++ Insert Into Binary Search Tree Using Array

So, within this particular program , I have nodes stored inside an vector of nodes. 因此,在此特定程序中,我将节点存储在节点向量中。 These nodes contain a user input int ID, int age, and string Name. 这些节点包含用户输入的int ID,int年龄和字符串Name。

This vector is meant to act as a binary search tree that is organized by ID number, and is not worried about being balanced. 此向量旨在充当由ID号组织的二叉搜索树,并且不担心被平衡。

Currently I am attempting to write an insert function in which, after the creation of a new node of user input values, it compares it against the root, which is stored at index 1 of the vector. 当前,我正在尝试编写一个插入函数,在该函数中,在创建用户输入值的新节点之后,将其与根进行比较,该根存储在向量的索引1中。

If it is smaller than the ID at the root, then it is placed into position (root*2). 如果它小于根的ID,则将其放置在位置(根* 2)。 If it is larger than the root, then it is placed into position (root*2 + 1) 如果它大于根,则将其放置在位置(根* 2 +1)

However, I am having an extreme amount of difficulty placing into the vector if these two spots are already being taken up by another node. 但是,如果这两个点已经被另一个节点占用,那么放入向量中的难度将非常大。

Is there any way that I could create a loop that would check against the value in the filled spot, and continue looping until it finds an empty spot? 有什么办法可以创建一个循环来检查填充点中的值,并继续循环直到找到空点?

A current example that I'm working with 我正在使用的当前示例

ID 50, ID 100, ID 75 ID 50,ID 100,ID 75

ID 50 is in position 1. ID 50在位置1。

ID 100 is inserted in position 3, since it is empty (2*1) ID 100插入位置3,因为它为空(2 * 1)

ID 75 is attempted to be inserted, but position 3 is filled, so then I need to check it against ID in position 3 using the same algorithm. 尝试插入ID 75,但填充了位置3,因此我需要使用相同算法将其与位置3中的ID进行比较。 In this case, since 75 is lower than 100, it would be placed in position 4. 在这种情况下,由于75小于100,它将被放置在位置4。

I'm completely stumped as to how to to implement such an algorithm into a loop 我完全不知道如何将这样的算法实现为循环

Any help would be much appreciated. 任何帮助将非常感激。

Here is my code. 这是我的代码。

void BST::insert() { 无效的BST :: insert(){

int ID;
int AGE;
string NAME;
bool done = false;

int root = 1;

cout << "Please enter the ID number, age and name" << endl;
do
{
    cin >> ID >> AGE >> NAME;
} while (ID <= 0);



Node *tree = new Node(ID, AGE, NAME);


if (!binaryTree.empty())
{

    do
    {
        Node &n = binaryTree.at(root);
        if (n.ID == 0)
        {
            n.ID = ID;
            n.age = AGE;
            n.name = NAME;
            done = true;
            break;
        }
        else if (ID < n.ID)
        {
            root = 2 * root;
        }
        else
        {
            root = 2 * root + 1;
        }
    } while (done = true);
}

if (binaryTree.empty())
{
    binaryTree.push_back(*tree);
}


start();

} }

I am still very new to this language, so any help would be much appreciated! 我对这门语言还很陌生,因此对您的帮助将不胜感激!

EDIT: 编辑:

I fixed some discrepancies and added the code that was suggested, however, now an out_of_bounds exception is being thrown. 我修复了一些差异,并添加了建议的代码,但是,现在引发了out_of_bounds异常。

Thanks! 谢谢!

My question is essentially, how would I turn such an alogrithm into a while loop/do while loop, etc 我的问题本质上是,我如何将这样的算法转换为while循环/执行while循环等

Here's an answer that uses pseudocode. 这是使用伪代码的答案。 You should re-write the loop on your own with a different constuct such as a while , do while or for loop. 您应该使用不同的构造(例如whiledo whilefor循环)自行重写循环。

But first, some problems with your code. 但是首先,您的代码存在一些问题。

  1. You are comparing Node.ID (an int) to NULL to check if a location in the vector is "empty". 您正在将Node.ID (一个int)与NULL以检查向量中的位置是否为“空”。 NULL has the same semantics as 0 so I am going to assume that no IDs will be 0 . NULL0具有相同的语义,因此我将假设没有ID为0 Your code doesn't actually check this, so you may want to handle that case and/or address these design issues. 您的代码实际上并未对此进行检查,因此您可能需要处理这种情况和/或解决这些设计问题。

  2. I have no idea why you are dynamically allocating memory with new if you are using a vector to store your nodes. 我不知道如果使用向量存储节点,为什么要动态分配new内存。 This is likely going to lead to a memory leak. 这可能会导致内存泄漏。

  3. I am assuming you are handling creating a vector of the appropriate size in code you have not posted. 我假设您正在处理尚未发布的代码中的适当大小的向量。 If you don't do this, then your code will crash when at throws an exception when root is outside the bounds of the vector. 如果不这样做,那么当root在向量的边界之外时, at抛出异常时,您的代码将崩溃。

If it is smaller than the ID at the root, then it is placed into position (root*2). 如果它小于根的ID,则将其放置在位置(根* 2)。 If it is larger than the root, then it is placed into position (root*2 + 1) 如果它大于根,则将其放置在位置(根* 2 +1)

  1. What if the ID is equal to the ID at the root? 如果ID等于根目录的ID怎么办?

And finally, the code with the goto loop. 最后,带有goto循环的代码。

int root = 1;

BEGIN LOOP            
    Node &n = binaryTree.at(root);
    if (n.ID == 0)
    {
        n.ID = ID;
        n.age = AGE;
        n.name = NAME;
        BREAK OUT OF LOOP
    }
    else if (ID < n.ID)
    {
        root = 2*root;
    }
    else
    {
        root = 2*root + 1;
    }
REPEAT

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

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