当前位置:网站首页>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 .
边栏推荐
- 攻防演练中沙盘推演的4个阶段
- 【obs】Impossible to find entrance point CreateDirect3D11DeviceFromDXGIDevice
- 浪潮云溪分布式数据库 Tracing(二)—— 源码解析
- Kubernetes static pod (static POD)
- [Yugong series] go teaching course 006 in July 2022 - automatic derivation of types and input and output
- The standby database has been delayed. Check that the MRP is wait_ for_ Log, apply after restarting MRP_ Log but wait again later_ for_ log
- 从服务器到云托管,到底经历了什么?
- Reentrantlock fair lock source code Chapter 0
- Analysis of 8 classic C language pointer written test questions
- 大二级分类产品页权重低,不收录怎么办?
猜你喜欢
【愚公系列】2022年7月 Go教学课程 006-自动推导类型和输入输出
他们齐聚 2022 ECUG Con,只为「中国技术力量」
Thinkphp内核工单系统源码商业开源版 多用户+多客服+短信+邮件通知
What if the testing process is not perfect and the development is not active?
NVIDIA Jetson测试安装yolox过程记录
Where is the big data open source project, one-stop fully automated full life cycle operation and maintenance steward Chengying (background)?
ThinkPHP kernel work order system source code commercial open source version multi user + multi customer service + SMS + email notification
Kubernetes Static Pod (静态Pod)
华为交换机S5735S-L24T4S-QA2无法telnet远程访问
3年经验,面试测试岗20K都拿不到了吗?这么坑?
随机推荐
FOFA-攻防挑战记录
The method of server defense against DDoS, Hangzhou advanced anti DDoS IP section 103.219.39 x
How to add automatic sorting titles in typora software?
C language 001: download, install, create the first C project and execute the first C language program of CodeBlocks
Play sonar
Codeforces Round #804 (Div. 2)(A~D)
SDNU_ ACM_ ICPC_ 2022_ Summer_ Practice(1~2)
[OBS] the official configuration is use_ GPU_ Priority effect is true
Where is the big data open source project, one-stop fully automated full life cycle operation and maintenance steward Chengying (background)?
fabulous! How does idea open multiple projects in a single window?
Letcode43: string multiplication
《因果性Causality》教程,哥本哈根大学Jonas Peters讲授
The standby database has been delayed. Check that the MRP is wait_ for_ Log, apply after restarting MRP_ Log but wait again later_ for_ log
【obs】Impossible to find entrance point CreateDirect3D11DeviceFromDXGIDevice
Su embedded training - day4
“一个优秀程序员可抵五个普通程序员”,差距就在这7个关键点
3 years of experience, can't you get 20K for the interview and test post? Such a hole?
股票开户免费办理佣金最低的券商,手机上开户安全吗
爬虫实战(八):爬表情包
接口测试进阶接口脚本使用—apipost(预/后执行脚本)