Click here to Skip to main content
15,918,516 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi,
I am new in coding and I am new here.
I need to build an abstract binary search tree, with the following functions:
1. create a generic BST
2. distory a BST
3. search in a BST
4. add an element to a BST

I am working on the first function and I have created a pointer so the creation of the tree will be genraic, but I can not manage to assign the function to the point called GetNewTree

C++
#include <stdio.h>
#include <stdlib.h>

typedef struct Tree {
    void *data;
    struct Tree *left;
    struct Tree *right;
}Tree;

void* GetNewTree();

Tree* GetNewTreeInt(){
    Tree* newTree = (Tree*)malloc(sizeof(Tree));
    newTree->data = malloc(sizeof(int));
    newTree->left = newTree->right = NULL;
    return newTree;
}

Tree* GetNewTreeChar(){
    Tree* newTree = (Tree*)malloc(sizeof(Tree));
    newTree->data = malloc(sizeof(char));
    newTree->left = newTree->right = NULL;
    return newTree;
}

int main() {
    int choose;
    int number;
    scanf("%d", &choose);
    switch (choose){
        case (0) : 	(Tree*)GetNewTree = &GetNewTreeInt();
            break;
        case (1) : 	(Tree*)GetNewTree = &GetNewTreeChar();
            break;
    }


        
    
    
    return 0;
}
Posted
Comments
Andreas Gieriet 27-Jan-15 17:06pm    
The case(0) and case(1) lines are broken.
You might want something like Tree* root = 0; ... case(0): root = GetNewTreeInt(); ...
Cheers
Andi
Sergey Alexandrovich Kryukov 27-Jan-15 19:06pm    
Could you use a spell checker and dictionary to make sure all words are correct English words? I cannot understand some. Besides, this is not really a question.
What does it mean, "assign a function to a point"?
—SA

1 solution

There are a few things going wrong here.

1. Nothing to hold the reference to the function you pick.
This;
void* GetNewTree();

Does not declare a function pointer, it's a forward declaration of a method that's not yet defined.
What you want to do instead is change that to a variable of type function pointer, that points to a function matching the GetNewTreeChar/Int function.
Tree* (*GetNewTree)();

That might look weird but that's the syntax for a function pointer.

2. Taking a pointer to the pointer when you want the function.
This;
(Tree*)GetNewTree = &GetNewTreeInt()
;
Does not need the & before the GetNewTreeInt, also you don't want the () at the end of it, as that is calling the method and you don't want that at that point, you want to assign the address of the function to the GetNewTree variable.

3. Casting an l-value.
It makes no sense to cast GetNewTree to (Tree*), you cast on assignment, not the assigned value.


I think something like this is closer to what you want;
C
#include <stdio.h>
#include <stdlib.h>

typedef struct Tree {
    void *data;
    struct Tree *left;
    struct Tree *right;
} Tree;

Tree* (*GetNewTree)();

Tree* GetNewTreeInt(){
    Tree* newTree = (Tree*)malloc(sizeof(Tree));
    newTree->data = malloc(sizeof(int));
    newTree->left = newTree->right = NULL;
    return newTree;
}

Tree* GetNewTreeChar(){
    Tree* newTree = (Tree*)malloc(sizeof(Tree));
    newTree->data = malloc(sizeof(char));
    newTree->left = newTree->right = NULL;
    return newTree;
}

int main() {
    int choose;
    int number;

    scanf("%d", &choose);
    switch (choose){
    case (0) : GetNewTree = GetNewTreeInt;
        break;
    case (1) : GetNewTree = GetNewTreeChar;
        break;
    }

    // Try to call it here to make sure it works;
    Tree* myTree = GetNewTree();
    return 0;
}


Hope this helps,
Fredrik
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900