#include <stdio.h>
Class XObject
{
int id;
char *type;
}
Class XSubObject : XObject
{
int remark;
char* place;
}
**Sorry for my bad example, but more or less data looks like this.
std::vector objects;
data stored in objects are like this:
#1=XObject(1001,"chair"), #2=XObject(1002,"spoon"), #3=XSubObject(1004,"table",2,"center"), #4=XSubObject(1005,"item",0,"left") and so..on
we cna have different XObjects with same types.
Class XStructure
{
XObject parent;
}
Class XStructureRow
{
XObject child;
XStructure parentStruct;
}
std::vector structures;
data stored in Structures are like this:
#5=XStructure(NULL), #7=XStructure(#1),#8=XStructure(#2),#9=XStructure(#3),#10=XStructure(#4) and so..on
std::vector structurerows;
data stored in Structures are like this:
XStructureRow(#4,#5), XStructureRow(#2,#1),XStructureRow(#2,#7),XStructureRow(#3,#10),XStructureRow(#4,#8) and so..on
How can i write a fast alogirthm that starts with XObject and finds it in which structurerow and fetching its structure and fetching its parent. For ex, I want to retrieve all the parents of Object with name “table”
and retrive its parents with name “chair”.
My written algorithm is:
std::vector<XObject> getParents(XObject "chair")
{
std::vector<XObject> objs;
for (int i=0;i<structurerows.size() ;i++ )
{
XStructurerow sr=structurerows[i];
XStructutre parent= sr.fetchParent();
if(parent!=NULL)
{
if(parent.fetchName()=="chair")
objs.push_back(parent);
}
}
return objs;
}
if i have to fetch all the objects parents then it is taking too much time if i have huge data. I mean is there any solution that helps to find the parent objects at O(1) way instead iterating the complete loop? I want to fetch these parents with minimal iterations. Here the complexity is O(n) which i am no satisfied. I hope i made some valid points. Suggestions please..
The only way to “find” something with O(1) complexity is to use a hash-table. The process of creating a hash-value from a key-value and then accessing the object indexed into the table by that hash-value will have O(1) complexity. Otherwise any other search algorithm will at best be O(log n) for a sorted list or sorted tree-type structure.