当前位置:网站首页>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 .
边栏推荐
- 股票开户免费办理佣金最低的券商,手机上开户安全吗
- ReentrantLock 公平锁源码 第0篇
- DNS 系列(一):为什么更新了 DNS 记录不生效?
- How to insert highlighted code blocks in WPS and word
- C # generics and performance comparison
- Deep dive kotlin collaboration (the end of 23): sharedflow and stateflow
- A brief history of information by James Gleick
- jemter分布式
- Lecture 1: the entry node of the link in the linked list
- 新库上线 | CnOpenData中华老字号企业名录
猜你喜欢
NVIDIA Jetson测试安装yolox过程记录
Jouer sonar
8道经典C语言指针笔试题解析
DNS series (I): why does the updated DNS record not take effect?
Letcode43: string multiplication
Analysis of 8 classic C language pointer written test questions
国外众测之密码找回漏洞
Class head up rate detection based on face recognition
赞!idea 如何单窗口打开多个项目?
Cancel the down arrow of the default style of select and set the default word of select
随机推荐
【obs】官方是配置USE_GPU_PRIORITY 效果为TRUE的
Where is the big data open source project, one-stop fully automated full life cycle operation and maintenance steward Chengying (background)?
5G NR 系统消息
The weight of the product page of the second level classification is low. What if it is not included?
服务器防御DDOS的方法,杭州高防IP段103.219.39.x
Su embedded training - day4
国外众测之密码找回漏洞
Stock account opening is free of charge. Is it safe to open an account on your mobile phone
搭建ADG过程中复制报错 RMAN-03009 ORA-03113
Handwriting a simulated reentrantlock
Basic types of 100 questions for basic grammar of Niuke
ABAP ALV LVC template
C language 001: download, install, create the first C project and execute the first C language program of CodeBlocks
Codeforces Round #804 (Div. 2)(A~D)
爬虫实战(八):爬表情包
新库上线 | CnOpenData中华老字号企业名录
[C language] objective questions - knowledge points
【obs】Impossible to find entrance point CreateDirect3D11DeviceFromDXGIDevice
New library online | information data of Chinese journalists
《因果性Causality》教程,哥本哈根大学Jonas Peters讲授