开发者

Using Retrieve Method to Find for Remove, BST C++

开发者 https://www.devze.com 2022-12-12 04:22 出处:网络
To make deletion of a leaf easier, im wondering how i can use my retreive method(which finds a node) to first retreive it before i delete. So that way i dont have to recode the same looping scheme tha

To make deletion of a leaf easier, im wondering how i can use my retreive method(which finds a node) to first retreive it before i delete. So that way i dont have to recode the same looping scheme that is in retreive:

bool BST::retrieve(const char *key, data& aData) const
{
    for ( int i = 0; i < maxSize / 2; i++ )
    {//iterate
        if (! items[2*i+1].empty &&//proceed
              items[2*i+1].theData.getName() < key )
        {// then search leftward
            if ( items[2*i+1].theData == key )
            {
         开发者_如何学C       aData.setName(key);
                return true;
            }
        }
        else if (! items[2*i+2].empty && 
                   items[2*i+2].theData.getName() > key )
        {   // then search rightward
            if ( items[2*i+2].theData == key )
            {
                aData.setName(key);
                return true;
            }
        }
    }// reiterate on condition.
    return false;
}

bool BST::remove(const char* key)
{   
    this->retrieve(key, items[Position].theData );
         // get the position of the node we want to delete.
         if ( items[2*Position+1].empty &&
              items[2*Position+2].empty )
         {
             // then it is a leaf.
         }
}

BST::BST(int capacity) : items(new item[capacity]), Position(0), 
leftChild(0), rightChild(0), maxSize(capacity), size(0), isLeaf(0)
{

}

Position varaible is coming up as 0. and if i declare a plain old int isLeaf at the start of the retreive method, and then when we find it, assign it, isLeaf = 2*i+1; or isleaf = 2*i+2.

I would then need to pass that POD, isLeaf as a paramater. Therefore, I would need a helper function for the remove method?

It seems that it would be extraneous to code my remove method to retreive a node..

Basically im after the index.


I would suggest you change your retrieve function to use a function that finds the index of the data you're trying to find then populate the aData field. Then you can use the new function in both the retrieve and remove function to find the index.

0

精彩评论

暂无评论...
验证码 换一张
取 消