0

So I've made this function to recover the closest number from what I've searched in an AVL Tree (like, if the numbers are 1 4 5 6 and if I search 5, it'll return 4).

I store this number in the variable 'closer' and check if the node I'm comparing is the last node in the tree. If I print closer in the auxiliary function it prints the exact number I wanted, but when I return it to the main function it's non-sense.

void searchCloser(AVL_TREE* tree, void* search) {

    if (AVL_Retrieve(tree,search)!= NULL)
    {
        NODE* node=tree->root;
        while (node->left)
            node=node->left;
        void* closer=node->dataPtr;     //First Element of Tree

        node=tree->root;
        while (node->right)
            node=node->right;
        void* end=node;                 //End of Tree

        closer=_searchCloser(tree->root,search,closer,end);
        printf("--%d",*(int*)closer);
    } else
        printf("Nao Encontrado");
}

void _searchCloser(NODE* root, void* search, void* closer, void*end) {

    if (root->left)
        _searchCloser(root->left,search,closer,end);

    if(compare(search,root->dataPtr)==1 && compare(search,closer)==1)  //Compare returns 1 for A bigger then B
        closer=root->dataPtr;

    if (root->right)
        _searchCloser(root->right,search,closer,end);

    if (end==root)
        return(closer);
}
Jonny Henly
  • 4,023
  • 4
  • 26
  • 43

0 Answers0