1 Star 0 Fork 0

雷正伟/Leizw-blogs

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
目录树 2.40 KB
一键复制 编辑 原始数据 按行查看 历史
雷正伟 提交于 2021-07-08 14:56 +08:00 . add 目录树.
#include<iostream>
#include<string>
using namespace std;
typedef struct node* BTree;
typedef struct node
{
string name;
BTree Catalog;
BTree Brother;
bool flag;
}BTnode;
void DealStr(string str, BTree bt);
BTree InsertFile(BTree bt, string name);
BTree Insertcatalog(BTree bt_Ca, string name);
int GetHeight(BTree BT, int h, string name, int flag);
void PrintfCat(BTree T, int h);
int main()
{
int n, i = 0;
string str;
BTree T = new BTnode;
T->name = "root";
T->Catalog = NULL;
T->Brother = NULL;
T->flag = 1;
BTree bt = T;
cin >> n;
while (i < n)
{
cin >> str;
DealStr(str, bt);
i++;
}
PrintfCat(T, 1);
return 0;
}
void DealStr(string str, BTree bt)
{
while (str.size() > 0)
{
int pos = str.find("\\");
if (pos == -1)
{
bt->Catalog = InsertFile(bt->Catalog, str);
return;
}
else
{
string name;
name.assign(str, 0, pos);
bt->Catalog = Insertcatalog(bt->Catalog, name);
bt = bt->Catalog;
while (bt != NULL && bt->flag == 1 && bt->name != name)
bt = bt->Brother;
str.erase(0, pos + 1);
}
}
}
BTree InsertFile(BTree bt, string name)
{
if (bt == NULL || bt->flag == 0 && bt->name.compare(name) > 0)
{
BTree NeweFile = new BTnode;
NeweFile->Brother = bt;
NeweFile->flag = 0;
NeweFile->Catalog = NULL;
NeweFile->name = name;
}
if (bt->flag != 1 && bt->name = name)
{
return bt;
}
bt->Brother = InsertFile(bt->Brother, name);
return bt;
}
BTree Insertcatalog(BTree bt_Ca, string name)
{
if (bt_Ca == NULL || bt_Ca->flag == 0 || bt_Ca->name.compare(name) > 0)
{
BTree NewNode = new BTnode;
NewNode->name = name;
NewNode->flag = 1;
NewNode->Catalog = NULL;
NewNode->Brother = bt_Ca;
return NewNode;
}
if (bt_Ca->name == name)
return bt_Ca;
bt_Ca->Brother = Insertcatalog(bt_Ca->Brother, name);
return bt_Ca;
}
int GetHeight(BTree BT, int h, string name, int flag)
{
if (BT == NULL)
return 0;
else if (BT->name == name && BT->flag == flag)
return h;
else
{
int x = GetHeight(BT->Brother, h, name, flag);
if (x)
return x;
else
return GetHeight(BT->Catalog, h + 1, name, flag);
}
}
void PrintfCat(BTree T, int h)
{
if (T)
{
h = GetHeight(T, h, T->name, T->flag);
for (int i;i < h;i++)
{
cout << " ";
}
cout << T->name << endl;
PrintfCat(T->Catalog, h + 1);
PrintfCat(T->Catalog, h);
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/lei-zhengwei/leizw-blogs.git
git@gitee.com:lei-zhengwei/leizw-blogs.git
lei-zhengwei
leizw-blogs
Leizw-blogs
master

搜索帮助