Question: I am trying to read a binary tree into my program using the following function that i wrote.

```
void readTree(struct Dnode *root)
{
    struct Dnode *temp;
    struct Dnode *input;
    char initData;

    scanf(" %c",&initData);
    input=CreateNode(initData);

    if(initData==122){
        printf("Leaf\n");
        return;
    }
    else {
        root=input;
        temp=root;
        printf("going to left of %c\n",initData);
        readTree(root->lLink);
        printf("going to right of %c\n",initData);
        readTree(root->rLink);
    }
}

```

Everything is working fine except the fact that , in main the tree doesnt exist. Here is my main program:

```
int main()
{
    int n=0;
    struct Dnode *root=NULL;
    printf("Enter the tree as it asks. Enter z whenever there is no children :\n");
    readTree(root);
    printf("%c",root->data);
    printf("In main");
    return;

 }

```

The program suddenly crashes after inputting the tree.Please help me.

Answer: 
```
switch(whichFunction) {
    case 1: function1(); break;
    case 2: function2(); break;
    case 3: function3(); break;
}

```
You can use the *polymorphism* concept (via virtual functions and inheritance).

Here is a very basic scheme:

```
#include <iostream>
#include <vector>

using namespace std;

class Obj
{
public:
    virtual void function() = 0;
};

class Obj1 : public Obj
{
public:
    virtual void function() {cout << "Function No. 1 was called!";}
};

class Obj2 : public Obj
{
public:
    virtual void function() {cout << "Function No. 2 was called!";}
};

class Obj3 : public Obj
{
public:
    virtual void function() {cout << "Function No. 3 was called!";}
};

int main()
{
    vector<Obj*> objects;
    objects.push_back(new Obj1);
    objects.push_back(new Obj2);
    objects.push_back(new Obj3);

    cout << "Which function do you want to call?";
    int whichFunction;
    cin >> whichFunction;
    if (1 <= whichFunction && whichFunction <= objects.size())
        objects[whichFunction-1]->function();

    for (auto object : objects)
        delete object;

    return 0;
}

```