功能实现:
1.用结点保存学校、学院、专业、班级、学生的名称和代码信息。
2.正确实现建树、对树上结点的增加、删除、修改操作。
3.对节点进行查询统计
4.实现一般搜索及模糊搜索
源代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<queue>
#include<conio.h>
using namespace std;
#define name_size 100//树中每个节点中存储的内容的最大值
#define child_max_number 100//孩子节点的最大个数
#define name_size 100//节点名称的长度
struct Tree//定义树的数据结构
{
char name[name_size];//树中每个节点中存储的内容
int chile_num;
struct Tree *child[child_max_number + 1];//存储指向孩子节点的指针
struct Tree *parent;//指向双亲节点
}*root = NULL;
queue<Tree *> Q_father;//存父亲指针(在建树的时候发挥作用)
queue<Tree *> Q_Traverse;//负责遍历时存储数据的队列
queue<int> Q_chile_num;//用在遍历输出的时候便于分层输出(记录子树的孩子个数)
//KMP算法实现部分
int next1[name_size];
void get_next(char *s2)
{
int i, k = 0, len2;
len2 = strlen(s2 + 1);
next1[1] = 0;
for (i = 2; i <= len2; i++)
{
while (k>0 && *(s2 + k + 1) != *(s2 + i))
{
k = next1[k];
}
if (*(s2 + k + 1) == *(s2 + i))
{
k += 1;
}
next1[i] = k;
}
}
int KMP(char *s1, char *s2)
{
int i, k = 0, len1, len2, num = 0;//初始化k = 2,无语
len1 = strlen(s1 + 1);
len2 = strlen(s2 + 1);
for (i = 1; i <= len1; i++)
{
while (k>0 && *(s1 + i) != *(s2 + k + 1))
{
k = next1[k];
}
if (*(s1 + i) == *(s2 + k + 1))
{
k += 1;
}
if (k == len2)
{
num++;
k = next1[k];
}
}
return num;
}
void Create_Tree()
{
FILE *file;
fopen_s(&file,"D:\\Study\\课\\数据结构课程设计\\树\\输入文件.txt", "rt");//以只读形式打开输入文件
char tc1[name_size], tc2[name_size];
while (!Q_father.empty())
{
Q_father.pop();
}
while (1)
{
fscanf_s(file, "%s%s", tc1, name_size - 1, tc2,name_size - 1);
// scanf_s("%s%s", tc1, name_size-1, tc2, name_size-1);//注意scanf_s和scanf区别
if (tc1[0] == '*')//输入结束标志
{
break;
}
if (tc1[0] == '#')//根节点标志
{
Tree * t = (Tree *)malloc(sizeof(Tree));//申请空间后一定要立即把初始化工作做好
t->chile_num = 0;
for (int i = 0; i <= child_max_number; i++)//将孩子节点的指针先清空
{
t->child[i] = NULL;
}
root = t;
strcpy_s(t->name, name_size,tc2);
t->parent = NULL;
Q_father.push(t);
}
else
{
Tree *t = (Tree *)malloc(sizeof(Tree));
t->chile_num = 0;//申请空间后一定要立即把初始化工作做好
for (int i = 0; i <= child_max_number; i++)//将当前