当前位置:网站首页>Implementation of adjacency table of SQLite database storage directory structure 2-construction of directory tree
Implementation of adjacency table of SQLite database storage directory structure 2-construction of directory tree
2022-07-08 00:53:00 【kupeThinkPoem】
Catalog
Two 、 Database storage directory structure adjacency table
2、 Code implementation build tree
One 、 summary
Storage directory structure ( Trees ) It's a common problem , There are many solutions . The main methods are adjacency tables 、 Advanced adjacency list 、 Improved preorder tree traversal 、 recursive query 、 Enumerate paths 、 Nested sets 、 Closure table, etc .
Two 、 Database storage directory structure adjacency table
The first and most elegant method we will try is called “ Adjacency table model ” or “ Recursive method ”. This is a good method , Because you only need a simple function to traverse your tree . In our food store , The adjacency table looks like this :
As you can see , In the adjacency table method , You saved the of each node “ Parent node ”. We can see ‘Pear’ yes ‘Green’ The son of , yes ‘Fruit’ My son, wait . The root node “Food” No parent value . For the sake of simplicity , I use “title” Value to identify each node . Of course , In a real database , You will use the number of each node id.
3、 ... and 、 Tree building
1、 data structure
Recently, I want to set multiple string information in a field of the database , How to intercept one of the strings ? Need to get the parentID This property performs the corresponding operation .
struct dirpro
{
char type;
char opType;
char parentID[32];
char rerserved[128-32-1-1];
};
struct DirProNode
{
int id;
std::string name;
std::string parId;
QLinkedList<DirProNode> children;
};
struct DirInfo
{
int id;
std::string name;
dirpro pro;
};
2、 Code implementation build tree
DirProNode getTree()
{
std::vector<DirInfo> vInfos =getdirproFromDb();// Read all directory information from the database
DirProNode root;// The root node
root.id=-1;
root.parId="NULL";
root.name="root";
int nsize=vInfos.size();
std::map<std::string,DirProNode*> mapDirThink;
std::vector<bool> vecMark;
vecMark.resize(nsize);
for(int i=0;i<nsize;++i)
{
vecMark[i]=false;
}
while(true)
{
int ncount=0;
for(int i=0;i<nsize;++i)
{
if(vecMark[i])
{continue;}
DirProNode dirproNode;
if(convert(vInfos[i],dirproNode)
{
if(dirproNode.parId=="NULL" || dirproNode.parId.isEmpty())
{
root.children.push)back(dirproNode);
mapDirThink[QString::number(dirproNode.id).c_str()]=&(root.children.back());
ncount++;
vecMark[i]=true;
}
else
{
mapDirThink[dirproNode.parId]->children.push_back(dirproNode);
mapDirThink[QString::number(dirproNode.id).c_str()]=&(root.children.back());
ncount++;
vecMark[i]=true;
}
}
else
{
vecMark[i]=false;
}
}
if(ncount==0)
{break;}
}
return root;
}
Four 、QTreeWidget Show tree
With DirProNode This data structure , We can use preorder traversal to build the directory structure and QTreeWidget Intermediate display .
边栏推荐
- Where is the big data open source project, one-stop fully automated full life cycle operation and maintenance steward Chengying (background)?
- Vscode software
- 基于卷积神经网络的恶意软件检测方法
- 【obs】Impossible to find entrance point CreateDirect3D11DeviceFromDXGIDevice
- RPA云电脑,让RPA开箱即用算力无限?
- Summary of weidongshan phase II course content
- 新库上线 | CnOpenData中华老字号企业名录
- Deep dive kotlin collaboration (the end of 23): sharedflow and stateflow
- letcode43:字符串相乘
- [C language] objective questions - knowledge points
猜你喜欢
基于卷积神经网络的恶意软件检测方法
Codeforces Round #804 (Div. 2)(A~D)
QT establish signal slots between different classes and transfer parameters
Malware detection method based on convolutional neural network
国外众测之密码找回漏洞
Development of a horse tourism website (optimization of servlet)
fabulous! How does idea open multiple projects in a single window?
接口测试要测试什么?
Jouer sonar
Kubernetes static pod (static POD)
随机推荐
韦东山第二期课程内容概要
How to add automatic sorting titles in typora software?
新库上线 | CnOpenData中国星级酒店数据
Installation and configuration of sublime Text3
[Yugong series] go teaching course 006 in July 2022 - automatic derivation of types and input and output
串口接收一包数据
服务器防御DDOS的方法,杭州高防IP段103.219.39.x
Kubernetes static pod (static POD)
5g NR system messages
哪个券商公司开户佣金低又安全,又靠谱
ThinkPHP kernel work order system source code commercial open source version multi user + multi customer service + SMS + email notification
AI遮天传 ML-初识决策树
The weight of the product page of the second level classification is low. What if it is not included?
Prompt configure: error: required tool not found: libtool solution when configuring and installing crosstool ng tool
[OBS] the official configuration is use_ GPU_ Priority effect is true
51 communicates with the Bluetooth module, and 51 drives the Bluetooth app to light up
应用实践 | 数仓体系效率全面提升!同程数科基于 Apache Doris 的数据仓库建设
Cve-2022-28346: Django SQL injection vulnerability
韦东山第三期课程内容概要
Deep dive kotlin synergy (XXII): flow treatment