Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
1.67/5 (6 votes)
See more:
Dear Friends
I have senario where I can have many child node to the parent node and also the child node can have multiple number of node

I need to have the code to insert and search function.

Please help
Posted
Updated 19-Aug-12 1:20am
v3
Comments
[no name] 19-Aug-12 8:57am    
You would need to go to www.vworker.com and post there.

Please research the B+ tree[^]. N can then be the order[^] of your B+ tree.

If you want to look into an existing implementation of a B+ tree in C++ have a look into this one here: Transwiki: B+ tree[^].

Regards,

Manfred
 
Share this answer
 
Comments
pasztorpisti 19-Aug-12 7:24am    
Most beginners are unable to understand such code and unable to extract the parts they need. Get my 5 though.
Let's kickstart you solution.
C++
struct node
{
    struct node* parent;
    int number_of_sons;
    struct node* sons;
    int data_int;
    // char[64] data_str;
    // void* data_ptr;
}


Now, you have to implement:
C++
int insert_tree(int data)
{
}

and

C++
struct node* search_tree(int data)
{
}
 
Share this answer
 
v2
Comments
Richard MacCutchan 5-Jun-12 5:02am    
Fixed HTML tags, use <pre> tags for code blocks (see the code button).
Stefan_Lang 5-Jun-12 11:38am    
A good start, but I would put the sons into a std::vector. Then you don't need to care about allocation/deallocation of each individual node.
pasztorpisti 19-Aug-12 7:18am    
Agree, this is C++. The data members should also be stored as private. Either going with C pointer or std::vector the OP should store the sons in a sorted array and should use std::lower_bound to perform binary search in both the insert and search opeartions.
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!
 
Share this answer
 
#include <map>

class tree_1
{
public:
tree_1()
{
data = NULL;
}
protected:
std::map<size_t,tree_1*> next_level_map;
void *data;
};
</map>
 
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