当前位置:网站首页>Sqlite数据库存储目录结构邻接表的实现2-目录树的构建
Sqlite数据库存储目录结构邻接表的实现2-目录树的构建
2022-07-07 22:03:00 【kupeThinkPoem】
目录
一、概述
存储目录结构(树)是一个常见的问题,有多种解决方案。方法主要有邻接表、进阶邻接列表、改进的前序树遍历、递归查询、枚举路径、嵌套集、闭包表等。
二、数据库存储目录结构邻接表
我们将尝试的第一种也是最优雅的方法叫做“邻接表模型”或“递归方法”。这是一个很好的方法,因为你只需要一个简单的函数来遍历你的树。在我们的食品商店中,邻接表看起来像这样:

如你所见,在邻接表方法中,你保存了每个节点的“父节点”。我们可以看到‘Pear’是‘Green’的子,是‘Fruit’的子等等。根节点“Food”没有父值。为了简单起见,我使用“title”值来标识每个节点。当然,在真实的数据库中,您将使用每个节点的数字id。
三、目录树的构建
1、数据结构
最近想在数据库的某一字段里面设置多个字符串信息,如何截取其中的某个字符串?需要在数据库获取parentID这个属性进行相应的操作。
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、代码实现构建树
DirProNode getTree()
{
std::vector<DirInfo> vInfos =getdirproFromDb();//从数据库中读取所有的目录信息
DirProNode root;//根节点
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;
}四、QTreeWidget显示目录树
有了DirProNode这个数据结构,我们就可以使用先序遍历构建目录结构并在QTreeWidget中进行显示。
边栏推荐
- Orthodontic precautions (continuously updated)
- At the age of 35, I made a decision to face unemployment
- limit 与offset的用法(转载)
- The difference between get and post
- Enterprise application demand-oriented development of human resources department, employee attendance records and paid wages business process cases
- CoinDesk评波场去中心化进程:让人们看到互联网的未来
- LinkedBlockingQueue源码分析-新增和删除
- @Detailed introduction of configuration annotation
- Chisel tutorial - 02 Chisel environment configuration and implementation and testing of the first chisel module
- 单机高并发模型设计
猜你喜欢

面试题详解:用Redis实现分布式锁的血泪史

DataGuard active / standby cleanup archive settings

Detailed explanation of interview questions: the history of blood and tears in implementing distributed locks with redis

The result of innovation in professional courses such as robotics (Automation)

用語雀寫文章了,功能真心强大!
![[programming problem] [scratch Level 2] March 2019 draw a square spiral](/img/fa/ae9dabdd36ba77b1f4644dd23bee93.png)
[programming problem] [scratch Level 2] March 2019 draw a square spiral

Opengl3.3 mouse picking up objects

FFA and ICGA angiography

光流传感器初步测试:GL9306

自动化测试:Robot FrameWork框架90%的人都想知道的实用技巧
随机推荐
HDU - 1260 Tickets(线性DP)
Redis caching tool class, worth owning~
P1067 [noip2009 popularity group] polynomial output (difficult, pit)
Kubectl's handy command line tool: Oh my Zsh tips and tricks
Orthodontic precautions (continuously updated)
QT and OpenGL: load 3D models using the open asset import library (assimp)
Magic fast power
Alibaba cloud MySQL cannot connect
Handwriting a simulated reentrantlock
new和delete的底层原理以及模板
2022.7.7-----leetcode.648
Chisel tutorial - 00 Ex.scala metals plug-in (vs Code), SBT and coursier exchange endogenous
Solutions to problems in sqlserver deleting data in tables
The difference between -s and -d when downloading packages using NPM
Tools for debugging makefiles - tool for debugging makefiles
单机高并发模型设计
Pigsty:开箱即用的数据库发行版
【編程題】【Scratch二級】2019.12 飛翔的小鳥
Anaconda+pycharm+pyqt5 configuration problem: pyuic5 cannot be found exe
95. (cesium chapter) cesium dynamic monomer-3d building (building)