using namespace std;
#define MAX 10
struct node
{
int key_value;
node *ptr[MAX];
node *ptr_pre;
std::string pathName;
int index;
/*
* First and last pointer for link list
*/
node *ptr_next[1];
/*
*type = 0 is normal Directory
*/
int type;
};
class btree
{
public:
btree();
~btree();
void createRoot(int key,std::string path);
void addObject(int key,std::string path);
void createArray(int key,std::string path);
void addArray(int key,std::string path);
void getObjectName(std::string path,node* &ptr);
//void getObjectValue(std::string path,node* &ptr);
void deleteObject(std::string path);
void show();
void destroy();
void destroyArray(node* &ptr);
private:
void destroy(node *&leaf);
void createRoot(int key,std::string path,node *leaf);
void createSubDir(int key,std::string name, node *leaf);
void findSubDir(std::string path,node *leaf,node* &ptr);
void getsubObjectName(std::string path,node* &ptr);
void findDir(std::string path,node *leaf,node* &ptr);
void addArray(int key, std::string path,node *leaf);
void showArray(node* ptr);
void display(node *leaf);
node *root;
std::string root_path;
};
btree::btree()
{
root=NULL;
}
btree::~btree()
{
destroy();
}
void btree::destroy(node *&leaf)
{
if(leaf!=NULL)
{
if(leaf->type ==1)
{
destroyArray(leaf);
}//End arrray
if(leaf ==NULL)
{
return;
}
for (int i = 0; i < leaf->index; i++)
{
if(leaf->ptr[i] != NULL)
{
destroy(leaf->ptr[i]);
}
}
cout<<"Deallocate allocated memory :"<<leaf->key_value<<endl;
if(leaf!=NULL)
{
delete leaf;
leaf =NULL;
}
}
}
void btree::destroy()
{
destroy(root);
root =NULL;
}
void btree::createRoot(int key,std::string path,node *leaf)
{
leaf->ptr[leaf->index]=new node;
leaf->ptr[leaf->index]->key_value=key;
leaf->ptr[leaf->index]->index=0;
leaf->ptr[leaf->index]->pathName = path;
leaf->ptr[leaf->index]->ptr_pre = leaf;
for(int i = 0; i< MAX; i++)
{
leaf->ptr[leaf->index]->ptr[i]= NULL;
}
leaf->index++;
leaf->type = 0;
leaf->ptr_next[0] = NULL;
}
void btree::addObject(int key,std::string path)
{
if(path.find(this->root_path) ==0)
{
node *ptr;
getsubObjectName(path,ptr);
if(ptr != NULL)
{
int found = path.find_last_of(".");
if(found != std::string::npos)
{
std::string subPath = path.substr(found+1);
createSubDir(key,subPath,ptr);
}
}
}
}
void btree::createRoot(int key,std::string path)
{
if(root!=NULL)
{
createRoot(key,path,this->root);
}
else
{
root=new node;
root->key_value=key;
root->index = 0;
root->ptr_pre = NULL;
for(int i = 0; i< 10; i++)
{
root->ptr[i]=NULL;
}
this->root->pathName = path;
root_path = path;
root->ptr_next[0] = NULL;
root->type = 0;
}
}
void btree::createSubDir(int key,std::string name, node *leaf)
{
leaf->ptr[leaf->index]=new node;
leaf->ptr[leaf->index]->key_value=key;
leaf->ptr[leaf->index]->index=0;
leaf->ptr[leaf->index]->ptr_pre = leaf;
leaf->ptr[leaf->index]->pathName = name;
for(int i = 0; i< MAX; i++)
{
leaf->ptr[leaf->index]->ptr[i]= NULL;
}
leaf->index++;
leaf->type = 0;
leaf->ptr_next[0] = NULL;
}
void btree::createArray(int key,std::string path)
{
cout<<"index= "<<this->root->index<<endl;
node *ptr_root = NULL;
getsubObjectName(path,ptr_root);
if(ptr_root != NULL)
{
int found = path.find_last_of(".");
if(found != std::string::npos)
{
std::string subPath = path.substr(found+1);
ptr_root->ptr[ptr_root->index]=new node;
ptr_root->ptr[ptr_root->index]->key_value=key;
ptr_root->ptr[ptr_root->index]->index=0;
ptr_root->ptr[ptr_root->index]->type=1;
ptr_root->ptr[ptr_root->index]->pathName =subPath;
for(int i = 0; i< MAX; i++)
{
ptr_root->ptr[ptr_root->index]->ptr[i]= NULL;
}
ptr_root->ptr[ptr_root->index]->ptr_pre = ptr_root;
ptr_root->ptr[ptr_root->index]->ptr_next[0]= NULL;
ptr_root->index++;
}
}//Ennd if
cout<<"index= "<<this->root->index<<endl;
}
void btree::addArray(int key, std::string path,node *leaf)
{
node *ptr = NULL;
getObjectName(path,ptr);
if(ptr != NULL)
{
//cout<<endl<<"Type: "<< ptr->type<< " key="<< key<<endl;
int i= 0;
while (true)
{
//<<"i="<<i++<<endl;
if(ptr->ptr_next[0] == NULL)
{
//cout<<"add array with key="<<key <<" i="<< i<<endl;
ptr->ptr_next[0] = new node;
ptr->ptr_next[0]->key_value = key;
ptr->ptr_next[0]->index = 0;
ptr->ptr_next[0]->type = 0;
ptr->ptr_next[0]->ptr_next[0] = NULL;
break;
}
ptr = ptr->ptr_next[0];
}
}
}
void btree::addArray(int key, std::string path)
{
addArray(key,path, this->root);
}
void btree::deleteObject(std::string path)
{
node *ptr = NULL;
findDir(path,this->root,ptr);
if(ptr != NULL)
{
int i= 0;
int len =0;
if(ptr->ptr_pre !=NULL)
{
len = ptr->ptr_pre->index;
cout<<"Before remove key size ="<<len<<endl;
for( i = 0; i< len; i++)
{
if(ptr->ptr_pre->ptr[i] != NULL)
{
if(ptr == ptr->ptr_pre->ptr[i])
{
ptr->ptr_pre->ptr[i] = ptr->ptr_pre->ptr[len-1];
ptr->ptr_pre->ptr[len-1]= NULL;
ptr->ptr_pre->index--;
len =len-1;
}
}
}
cout<<"After remove key Size ="<<ptr->ptr_pre->index<<endl;
destroy(ptr);
}
else
{
/*
*Key is root
*/
destroy(ptr);
root = NULL;
}
}
}
void btree::findDir(std::string path,node *leaf,node* &ptr)
{
if(leaf != NULL)
{
if(path.find(leaf->pathName) ==0)
{
path = path.substr(leaf->pathName.length()+1);
if(path.compare(leaf->pathName) == 0)
{
ptr = leaf;
return;
}
else
{
for (int i = 0; i < leaf->index; i++)
{
if(leaf->ptr[i] != NULL)
{
if(path.compare(leaf->ptr[i]->pathName) == 0)
{
ptr = leaf->ptr[i];
break;
}
}
}//End for
for (int i = 0; i < leaf->index; i++)
{
if(leaf->ptr[i] != NULL)
{
if(leaf->ptr[i]->index >0)
{
findDir(path,leaf->ptr[i], ptr );
}
}
}//End for
}
}
}
}
void btree::getObjectName(std::string path,node* &ptr)
{
findDir(path,this->root,ptr);
}
void btree::getsubObjectName(std::string path,node* &ptr)
{
findSubDir(path,this->root,ptr);
}
void btree::findSubDir(std::string path,node *leaf,node* &ptr)
{
if(leaf != NULL)
{
if(path.find(leaf->pathName) ==0)
{
path = path.substr(leaf->pathName.length()+1);
if(path.find(".") == std::string::npos)
{
ptr = leaf;
return;
}
else
{
for (int i = 0; i < leaf->index; i++)
{
if(leaf->ptr[i] != NULL)
{
if(path.find(leaf->ptr[i]->pathName) != std::string::npos)
{
std::string subPath = path.substr(leaf->ptr[i]->pathName.length()+ 1);
if(subPath.find(".") == std::string::npos)
{
ptr = leaf->ptr[i];
break;
}
}
}
}//End for
for (int i = 0; i < leaf->index; i++)
{
if(leaf->ptr[i] != NULL)
{
if(leaf->ptr[i]->index >0)
{
findSubDir(path,leaf->ptr[i], ptr );
}
}
}//End for
}
}
}
}
void btree::showArray(node* ptr)
{
while (ptr!= NULL)
{
cout<<"{" <<ptr->key_value<<"}";
if(ptr->ptr_next[0] != NULL)
{
cout<<", ";
}
ptr =ptr->ptr_next[0];
}
}
void btree::destroyArray(node* &ptr)
{
while (ptr!= NULL)
{
if(ptr->ptr_next[0] != NULL)
{
destroyArray(ptr->ptr_next[0]);
}
cout<<"destroyArray: "<< ptr->key_value<<endl;
delete ptr;
ptr = NULL;
}
}
void btree::display(node *leaf)
{
cout<<"{ " ;
if(leaf != NULL)
{
if(leaf->type!=1)
{
cout<<leaf->key_value;
}
if(leaf->index>0)
{
cout<< " = ";
}
if(leaf->type ==1)
{
cout<<" [";
showArray(leaf);
cout<<"] ";
}
if(leaf->index>0)
{
for (int i = 0; i < leaf->index; i++)
{
if(leaf->ptr[i] != NULL)
{
display(leaf->ptr[i]);
if(i+1!= leaf->index)
{
cout<<", ";
}
}
}
}
}
cout<<"} ";
}
void btree::show()
{
display(root);
}
int main()
{
btree tree;
tree.createRoot(10,"smarthome");
tree.addObject(11,"smarthome.info");
tree.addObject(6,"smarthome.ir");
tree.addObject(4,"smarthome.status");
tree.addObject(45,"smarthome.info.server");
tree.addObject(5,"smarthome.camera");
tree.addObject(50,"smarthome.stun");
tree.addObject(1,"smarthome.door");
tree.addObject(45,"smarthome.info.server");
tree.addObject(40,"smarthome.info.user");
tree.addObject(113,"smarthome.info.server.abc");
tree.addObject(113,"smarthome.info.server.123");
tree.addObject(111,"smarthome.info.server.123.456");
tree.addObject(222,"smarthome.info.server.123.abc");
tree.addObject(111,"smarthome.info.server.123.234");
cout<<"show root"<<endl<<endl;
tree.show();
cout<<endl<<"Line "<< __LINE__<<": link list"<<endl;
tree.createArray(100,"smarthome.room");//smarthome.room[0]
tree.addArray(3,"smarthome.room");//smarthome.room[1]
tree.addArray(4,"smarthome.room");//smarthome.room[2]
tree.addArray(5,"smarthome.room");//smarthome.room[3];
cout<<"after adding array"<<endl;
tree.show();
cout<<endl<<"search key"<<endl;
node *ptr = NULL;
tree.getObjectName("smarthome.info.server.123.456",ptr);
if(ptr != NULL)
{
cout<<"result= "<< ptr->key_value<<endl;
}
cout<<endl<<"Delete object "<<endl;
tree.deleteObject("smarthome.stun");
cout<<"show root "<<endl;
tree.show();
tree.destroy();
cout<<"ThongLT"<<endl;
return 0;
}
No comments:
Post a Comment