简体   繁体   English

基于数组的二叉搜索树C ++

[英]Array Based Binary Search Tree C++

Im trying to build an array based, "Binary Search Tree" by following the algorithm at: 我试图通过遵循以下算法构建基于数组的“二进制搜索树”:

http://highered.mcgraw-hill.com/olcweb/cgi/pluginpop.cgi?it=gif%3A:600%3A:388%3A%3A/sites/dl/free/0070131511/25327/tree%5Finsert.gif%3A%3ATREE-INSERT http://highered.mcgraw-hill.com/olcweb/cgi/pluginpop.cgi?it=gif%3A:600%3A:388%3A%3A/sites/dl/free/0070131511/25327/tree%5Finsert。 GIF%3A%3ATREE-INSERT

Up until I need to realloacte, my tree resembles: 直到我需要重新生成树,我的树才像:

     R
    / 
   A
    \
     F
      \
       L
      /
     B
      \
       C
        \
         T  

Recursively. 递归。 However, im notice that I need to get back to the root, "R"....Trying to do that now.. 但是,我注意到我需要回到根目录“ R”。...现在尝试这样做。

void BST::insert(const data& aData)
{
item *y = NULL;   // Algorithm calls for NULL assignment..
item *x = new item();       

     // How do i Init LEFT and RIGHT?
     // With no nested copy ctor for struct item?
if ( items->empty ) 
{
    items = new item();
    items->empty = false;
    items->theData = aData; // Get the data.
    ++size;
} 
else if ( size == maxSize ) this->reallocate();
else  
{   
        if ( aData < items->theData )
        {
            x[x->LEFT].theData = aData;
            x->LEFT = x->LEFT + 1;
            this->insert(items->theData);
        }
        else if ( items->theData < aData )
        {
            x[x->RIGHT].theData = aData;
            x->RIGHT = x->RIGHT + 1;
            this->insert(items->theData);
        } 
                       else this->insert(items->theData);
}

Here is my struct for the items array in the private section of the BST class object file: ... 这是我的BST类对象文件的private部分中的items数组的结构:...

 private:
    int size;  // size of the ever growing/expanding tree :)
    int maxSize;
    struct item
    {
              bool empty;
         int  LEFT;
              int  RIGHT;
              data theData;     
         };

    item *items;    // The tree array
    item *oldRoot;
         int root_index;   // index for the root(s)

Hmm. 嗯。 Im also overloading the assignment operator...I dont know what to say. 我还在重载赋值运算符...我不知道该说些什么。 Its hard. 这个很难(硬。 I've looked at so many examples and lectures online; 我在网上看了很多例子和讲座。 as well as algorithms.... 以及算法...

The relloaction method as requested: 要求的展开方法:

void BST::reallocate()
{
    item *new_array = new item[size*2];
    for ( int array_index = 0; array_index < size; array_index++ ) 
    {
        new_array[array_index].theData = items[array_index].theData;
        new_array[array_index].empty = false;
    }
    size *= 2;
    delete [] items;

    items = NULL;
    items = new_array;
}

There is well established way to implement binary tree as array, which says that root is sitting at index 0, LEFT of element at index i will be found at 2i + 1 and RIGHT will be found at 2i + 2. You do not need to have LEFT and RIGHT as part of your structure. 有一种公认的方法可以将二叉树实现为数组,它表示根位于索引0处,索引i处元素的LEFT将位于2i + 1处,而RIGHT将位于2i + 2处。您不需要将LEFT和RIGHT作为结构的一部分。 it has to be 它一定要是

struct item
{
    int index;
    data theData;
};

Do NOT store left index and right index in your structure. 不要左手食指和右手食指存储在你的结构。 Also you don't need to keep track of root index. 另外,您不需要跟踪根索引。 It is always 0. Check out wiki article on binary trees. 它始终为0。请查看有关二叉树的Wiki文章 Search for "Methods for storing binary trees" string. 搜索“用于存储二进制树的方法”字符串。 This implementation allows easy traversal down and up the tree if needed. 如果需要,此实现允许轻松遍历树。

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

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