0

I am trying to create an AVL perfectly balanced tree from a vector of elements. I have started on a small number of elements(8) so that I can check the correctness of the algorithm. My problem appears when printing the values from the TREE, I keep getting the next exception

"Exception thrown: read access violation.
nod->stanga was 0x4."

when i get to the (root)->(right)->left-:value, even though I check if the pointer is null before printing anything. the structure is:

typedef struct node
{   int key;
    int size;
    node *stanga;
    node *dreapta;
}TreeNode;

the array: int vector[8] = { 1, 2, 3, 4, 5, 6, 7, 8 }; the print function:

void printElements(TreeNode *nod)
{
    if (nod != NULL)
    {
        printf("Nodul  este : %d \n", nod->key);
        if (nod->dreapta != NULL && nod->stanga != NULL)
        {
            printf("Nodul  dreapta al  nodului  %d este  : %d \n", nod->key, nod->dreapta->key);
            printf("Nodul  stamga al  nodului  este %d : %d\n ", nod->key, nod->stanga->key);

        }
        if (nod->dreapta != NULL)
        {
            printf("ramura dreapta a nodului  %d  cu valoare dreapta este : %d\n", nod->key,nod->dreapta->key);
            printElements(nod->dreapta);
        }
        if (nod->stanga != NULL)
        {
            printf("ramura dreapta a nodului  %d cu valoare stanga este : %d \n ",nod->key, nod->stanga->key);
            printElements(nod->stanga);
        }
    }
    else
    {
        printf("the end of the tree");
    }
}

called with :

TreeNode  *nod = (TreeNode*)malloc(sizeof(TreeNode));
    nod=Build_tree(0, 7);
    printElements(nod);

build tree is my building function:

TreeNode* Build_tree(int start, int end)
{

    if (start < end)
    {
        int medium = (start + end) / 2;
        TreeNode  *n1 = (TreeNode*)malloc(sizeof(TreeNode));
        n1->key = vector[medium];
        n1->size = 1;
        if (n1->stanga == NULL)
        {
            n1->size = n1->dreapta->size + 1;//alocam sizeul nodului din drepata
        }
        if (n1->dreapta == NULL)
        {
            n1->size = n1->stanga->size + 1;//altefl alocam sizeul nodului din stanga
        }

        n1->stanga = Build_tree(start, medium-1);
        n1->dreapta = Build_tree(medium+1,end);
        return n1;
    }

}

I am a bit rusty on using pointers and balanced trees. Could somebody help me with a clue?

Diana G
  • 261
  • 3
  • 15
  • 1
    Don't know how relevant, but function `Build_tree` does not always return a value. If you posted the question without noticing this, perhaps there are other errors which you have also overlooked. Compile with all warnings enabled. – Weather Vane Apr 24 '18 at 21:30
  • Why does it not always return a value ? on which path?(on debug i can only look at the root's value and the left/right of the root values, not any further(like root -> left->left)) – Diana G Apr 24 '18 at 21:36
  • When `if (start < end)` is false. If you follow my advice and enable all compiler warnings . . . please do that right now. – Weather Vane Apr 24 '18 at 21:38
  • enable all compiler warnings by using /w3#### in the projects properties? – Diana G Apr 24 '18 at 21:50
  • Please look at function `Build_tree`. When the `if (start < end)` code block is executed, the function returns `n1`. If that code block is not executed, what does the function return? Look very closely, it has nothing to do with any problem statement, but control paths. – Weather Vane Apr 24 '18 at 21:56
  • it should return NULL or the current node. I think? – Diana G Apr 24 '18 at 22:02

1 Answers1

0

The if (start < end) statement in Build_tree has an else condition, where you fall off the function, thus returning something undefined. I inserted a fprintf(stderr, “Error: cannot get here (%d,%d)\n”, start,end), and got the following output:

Error: cannot get here (0, 0)
Error: cannot get here (2, 2)
Error: cannot get here (4, 4)
Error: cannot get here (6, 5)
Error: cannot get here (7, 7)
Nodul  este : 4 
Nodul  dreapta al  nodului  4 este  : 6 
Nodul  stamga al  nodului  este 4 : 2
 ramura dreapta a nodului  4  cu valoare dreapta este : 6
Nodul  este : 6 
ramura dreapta a nodului  6  cu valoare dreapta este : 7
Nodul  este : 7 
ramura dreapta a nodului  4 cu valoare stanga este : 2 
 Nodul  este : 2 

now, what should be done on the other side?

mevets
  • 10,070
  • 1
  • 21
  • 33
  • Honestly, the algorithm does not mention a continuation on the else statement. I am guessing it has something to do with returning the current node, because when the indexes were equal I should have went over all the numbers in my array – Diana G Apr 24 '18 at 21:49