8

I have a tree data structure in which parent node can have any number of child nodes(>=0). I want to create such tree. One of the possible approach thought by me is creating a linked list as shown in my_approach picture. Linked list are connected as shown.

U can suggest alternative approach also

enter image description here enter image description here

So I wrote a code to search in tree.(sorry for long code)To help u to understand my notations

class node
{   public:
    node* boss;
    string name;
    node* next;
    int level;
    node* next_level;
    node* search(string);
    node() : boss(NULL), next(NULL), next_level(NULL){ }
    friend class my_tree;

};

node* ans=NULL;
class my_tree
{
    public:
    my_tree();
    void print(node*);
    node* search(string,node*);
    node* gethead();
    bool is_empty();
    void add(string, node*);
    node* head;
};

my_tree::my_tree()
{
    head=new node;
    head->boss=NULL;
    head->name=""; 
    head->next=NULL;
    head->level=0;
    head->next_level=NULL;
}


bool my_tree::is_empty()
{
    if(head->next_level==NULL)
        {return 1;}
    else if(head->next_level!=NULL)
        {return 0;}
}

node* my_tree::gethead()
{   
    return head;
}

node* my_tree::search(string employee, node* ptr)
{   
    cout<<"ptr="<<ptr<<endl;
    if (ptr==NULL)
        {return NULL;}
    else if(ptr->name==employee)
        {cout<<"yup"<<endl;
        ans=ptr;
        return ptr;}
    else if(ptr->name!=employee)
        {
        search(employee, ptr->next_level);
        search(employee, ptr->next);
        cout<<"in search ans : "<<ans<<endl;
        return ans;
        }

}

void my_tree::add(string employee, node* immediate_boss)
{
    node* temp;
    temp=new node;
    temp->name=employee;
    if(immediate_boss->next_level==NULL)
        {
        temp->boss=immediate_boss;
        temp->level=immediate_boss->level+1;
        immediate_boss->next_level=temp;
        }
    else if(immediate_boss->next_level!=NULL)
        {node* ptr=immediate_boss->next_level;
        while(ptr->next!=NULL)
            {ptr=ptr->next;}
        temp->boss=immediate_boss;
        temp->level=immediate_boss->level+1;
        ptr->next=temp;
        }
    cout<<"employee added:"<<temp->name<<" at "<<temp<<endl;
}


main()
{
    my_tree Company;
    char a;
    string line1;
    string line2;
    a=myfile.get();
    bool e;
    cout<<"head : "<<Company.gethead()<<endl;
    while(myfile.good() and myfile.is_open())
        {

        //I do some operations and get line2 and line1
            //search functions searches for element( here I called employee) and gives its pointer. I use this pointer to add line1 as child node of line2. 
                Company.add(line2,Company.search(line2,Company.gethead()));
                line1.clear();
                line2.clear();
                ans=NULL;
                }


            }


}

This works for 1st node but search gives incorrect result after adding >1 nodes. NOTE : I am new at c++ and don't know concept of vectors. So I have to do this without using vectors. Also ,U can suggest suitable structure if possible.

Nikhil Chilwant
  • 629
  • 3
  • 11
  • 31
  • I used recursive approach in search. – Nikhil Chilwant Sep 14 '13 at 12:02
  • U can suggest alternative structure also. But please give me some hint to solve this. – Nikhil Chilwant Sep 14 '13 at 12:09
  • Basically this is hierarchical structure of company. – Nikhil Chilwant Sep 14 '13 at 12:18
  • 1
    In your solution in function search you are recursively calling search () but you are not checking the result of these calls... so when you have more nodes, then you will actually never get right answer because you did not catch it.... – Dusan Plavak Sep 14 '13 at 12:25
  • Note that level 3 nodes are not connected(in my_approach picture). Also no. of child nodes are not fixed. So, to apply it here? – Nikhil Chilwant Sep 14 '13 at 12:26
  • @DusanPlavak Thank you. but how can I ensure to get out of recursion as soon as I get ans (the global variable) changed. One of the possible approach is using exceptions. ( In exceptions, I have to write catch {....} part just after try. So, it cannot take me to main() ). – Nikhil Chilwant Sep 14 '13 at 12:29
  • http://pastebin.com/BHexZA0V – Dusan Plavak Sep 14 '13 at 12:39
  • till not working. I shred u whole code and text file. Please remember to change text file address in code. http://temp-share.com/show/gFHc4Jx6Y – Nikhil Chilwant Sep 14 '13 at 12:52
  • Your code is quiet a mess, I am not sure what are you trying to do... try to describe behavior of your program and get some examples what are you expecting from the program... – Dusan Plavak Sep 14 '13 at 13:04
  • OK. Basically , I want to store, search and print structure of Company. So, I created node which will store these:pointer to its boss, pointer to the linked list of its employees, string to store his name. node* next stores pointer to employee on same level. Downlaod ppt here : http://temp-share.com/show/KdP0s51Ah – Nikhil Chilwant Sep 14 '13 at 13:18
  • For clutter free code see: http://temp-share.com/show/dPfsmp37W – Nikhil Chilwant Sep 14 '13 at 13:19
  • Ok I understand what you want to do... but do not understand how to create unique structure from your text file... I mean that what in your txt shows that from employee A should goes link to the next level.... – Dusan Plavak Sep 14 '13 at 16:59
  • yeah, program reads 2and does: Read the names in pairs. For each pair (A,B) add the employee A in the tree so that he reports to immediate boss B. Note that automatically level of A will be one more than that of B. – Nikhil Chilwant Sep 14 '13 at 17:06
  • can you make a pic, what it should be like if input is your txt? – Dusan Plavak Sep 14 '13 at 17:10
  • here : http://s16.postimg.org/xrtzapslh/structure.png – Nikhil Chilwant Sep 14 '13 at 17:16

1 Answers1

1

You may consider storing next pointers in vector (or array) of pointers:

class Node{
public:
    string name() const {return _name;}
....


private:
    string _name;  //some data stored in node
    vector<Node *> next;  //vector of childs
};

Then in your search method you iterate over this vector:

Node *search (string name)
{
    if (_name == name)
        return this;
    else
        for(int ix = 0; ix < next.size(); ++ix)
        {
            Node *temp = next[ix]->search(name);
            if (temp->name() == name)
                return temp;
        }
    return 0; //nothing found
}

}

cpp
  • 3,743
  • 3
  • 24
  • 38