代码拉取完成,页面将自动刷新
#include<iostream>
#include<string>
using namespace std;
typedef string ElmentType;
typedef struct DirTreeNode
{
ElmentType data;
bool isDir;//判断当下的文件是否是目录。
struct DirTreeNode* firstChild;//孩子指针
struct DirTreeNode* brother;//兄弟指针
}DirTreeNode, *DirTree;
void CreateDirTree(DirTree& T);
void InitializeTree(DirTree& T);
void SubCreateDirTree(DirTree& newTreeNode,string str,int type);
void InsertIntoDirTree(DirTree& LastTNode, DirTree newTNode);
void PrePrint(DirTree T, int height);
int main()
{
DirTree tree;//整棵树;
CreateDirTree(tree);
PrePrint(tree, 1);
return 0;
}
void InitializeTree(DirTree& T)
{
T = new DirTreeNode;
T->isDir = true;
T->data = "root";
T->brother = NULL;
T->firstChild = NULL;
}
void SubCreateDirTree(DirTree& newTreeNode, string str, int type)
{
/*当type为1代表要建的是目录,为2代表要建的是文件*/
//[简单的建树结点]
newTreeNode = new DirTreeNode;
newTreeNode->data = str;
newTreeNode->brother = NULL;
newTreeNode->firstChild = NULL;
if (type == 1)
{
newTreeNode->isDir = true;
}
else if (type == 2)
{
newTreeNode->isDir = false;
}
}
void CreateDirTree(DirTree& T)
{
int N;
cin >> N;
string input_str;//读入的数据串;
InitializeTree(T);
for (int i = 0; i < N; i++)
{
cin >> input_str;
//对单行进行操作
int level = 1;//用来标志当下所在的层数;
string substr;
int index = -1;// 只储存'\\'所在的下标;
int start = 0;// 只储存新子串头部所在的下标;
DirTree LastTNode = T;//保存上一次插入的那个树结点的位置,默认在root。
//处理目录信息;
while (input_str.find('\\', start) != -1)
{
DirTree NewNode;
//update the position of '\\'
index = input_str.find('\\', start);
substr = input_str.substr(start, index - start);
//cout << substr << endl;//测试子串的拆分是否完成
SubCreateDirTree(NewNode, substr, 1);//创建目录结点
InsertIntoDirTree(LastTNode, NewNode);//插入新结点
//update the position of start
start = index + 1;
}
//处理文件信息;
if (index != input_str.size() - 1)//代表最后一个元素是文件
{
DirTree NewNode;
substr = input_str.substr(start, input_str.size() - start);
//cout << substr << endl;//测试子串的拆分是否完成
SubCreateDirTree(NewNode, substr, 2);//创建文件结点
InsertIntoDirTree(LastTNode, NewNode);//插入新结点
}
}
}
void InsertIntoDirTree(DirTree& LastTNode, DirTree newTNode)
{
//先考虑插入作为孩子:(讨论是否头插)
if (!LastTNode->firstChild)//当前结点无首孩
{
LastTNode->firstChild = newTNode;
//新建完结点后,都要更新LastTNode的位置。
LastTNode = newTNode;
return;
}
else
{
if (newTNode->isDir == true)//新结点是目录结点时;
{
if (LastTNode->firstChild->isDir == false)//首孩是文件
{
newTNode->brother = LastTNode->firstChild;
LastTNode->firstChild = newTNode;
//新建完结点后,都要更新LastTNode的位置。
LastTNode = newTNode;
return;
}
else if (LastTNode->firstChild->isDir == true//首孩是目录
&& LastTNode->firstChild->data == newTNode->data)
{
LastTNode = LastTNode->firstChild;
delete newTNode;
return;
}
else if (LastTNode->firstChild->isDir == true
&& LastTNode->firstChild->data > newTNode->data)
{
newTNode->brother = LastTNode->firstChild;
LastTNode->firstChild = newTNode;
//新建完结点后,都要更新LastTNode的位置。
LastTNode = newTNode;
return;
}
}
else//新结点是文件结点时;
{
if (LastTNode->firstChild->isDir == false//首孩是文件
&& LastTNode->firstChild->data == newTNode->data)
{
LastTNode = LastTNode->firstChild;
delete newTNode;
return;
}
}
}
//如果插入不作为孩子就是要插入作为兄弟:(插入排序)
DirTree pre = LastTNode;
while (pre->brother)
{
if (newTNode->isDir = true)//新结点是目录结点
{
if (pre->brother->isDir != newTNode->isDir)//首兄弟是文件的;
{
newTNode->brother = pre->brother;
pre->brother = newTNode;
LastTNode = newTNode;
return;
}
else
{
if (pre->brother->data > newTNode->data)
{
newTNode->brother = pre->brother;
pre->brother = newTNode;
LastTNode = newTNode;
return;
}
else if (pre->brother->data == newTNode->data)//如果发现该结点已经存在
{
LastTNode = pre->brother;
delete newTNode;
return;
}
}
}
else if (newTNode->isDir = false)//新结点是文件结点
{
if (pre->brother->data > newTNode->data && pre->brother->isDir == newTNode->isDir)
{
newTNode->brother = pre->brother;
pre->brother = newTNode;
LastTNode = newTNode;
return;
}
else if (pre->brother->data == newTNode->data)//如果发现该结点已经存在
{
LastTNode = pre->brother;
delete newTNode;
return;
}
}
pre = pre->brother;
}
if (!pre->brother && pre->data != "root")//pre的兄弟为空且当前结点不是根结点,直接尾插
{
pre->brother = newTNode;
}
}
void PrePrint(DirTree T,int height)
{
DirTree ptr;
if (!T)
{
return;
}
for (int i = 0; i < (height-1); i++)
{
cout << " ";
}
cout << T->data << endl;
ptr = T->firstChild;
while (ptr != NULL)
{
PrePrint(ptr, height + 1);
ptr = ptr->brother;
}
}
//测试数据一
/*
7
b
c\
ab\cd
a\bc
ab\d
a\d\a
a\d\z\
*/
//
/*
3
ab\cd
ab\d
a\d\a
*/
//另一个测试数据
/*
7
a\
b\c\
c\b\
x\aa\
m\n\tt
ab
xcv\sdf\sdfs\sd
*/
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。