Jump to content

Hey guys I'm working on functions to go through my binary search tree recursively.

However, I cannot seem to correctly call my recursive functions while also moving to the

left or right child respectively.  Any help is appreciated.  

The functions are:

int tree::insert(const tree & toAdd)//Insert a new node into the tree
{
    node * temp= new node; 
    temp -> entry= toAdd;
    if(!temp)
    {
        delete temp;
        return 0;
    }
    
    if(!root)
    {
        node * root = new node;
        root -> entry = toAdd;
        return 0; //Return it was inserted
    }
    if(root -> entry.chapter > toAdd.chapter)
    {
        insert(root -> left, toAdd);
    }
    else if(root -> entry.chapter < toAdd.chapter)
    {
        insert(root -> right, toAdd);
    }
}

int tree::edit(char * keySearch, char * noteEdit, tree & found)
{
    //Edits the description of the note if it is found
    if(!root)
        return 0;
    //If keyword matches key search
    if(root -> entry.keyword == keySearch)
    {
        // return the FOUND node back to main
        // Set roots description to noteEdit
        root -> entry.notes = noteEdit;
    }
    //If keyword is larger than search keyword
    if(root -> entry.keyword > keySearch)
    {
        root = root -> right;
        edit(keySearch, &noteEdit, &found);
    }
    //If keyword is smaller than search keyword
    else if(root -> entry.keyword < keySearch)
    {
        root = root -> left;
        edit(keySearch, &noteEdit,&found);
    }
}

I am also not sure if my retrieve function works and if it would actually return the found match

back to main.  

int retrieve(char * keySeach, tree & found) const;
int tree::retrieve(char * keySearch, tree & found)const//Search for the tree that matches
{
    if(!root)
        return 1;//That it was not found because empty.
    
    if(root -> entry.keyword == keySearch)
    {
             
        retrieve(keySearch, found);
        return found;
    }
    else
        retrieve(keySearch, found);
    
}

 

i5 4670k| Asrock H81M-ITX| EVGA Nex 650g| WD Black 500Gb| H100 with SP120s| ASUS Matrix 7970 Platinum (just sold)| Patriot Venom 1600Mhz 8Gb| Bitfenix Prodigy. Build log in progress 

Build Log here: http://linustechtips.com/main/topic/119926-yin-yang-prodigy-update-2-26-14/

Link to comment
https://linustechtips.com/topic/600787-recursive-function-call-help/
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×