mirror of
https://github.com/Didnelpsun/CS408.git
synced 2026-11-03 05:43:42 +08:00
更新树
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#include "head.h"
|
||||
|
||||
// 链栈
|
||||
typedef struct LinkStackNode{
|
||||
// 数据
|
||||
element_type data;
|
||||
|
||||
7
Code/Code/head/link_tree.h
Normal file
7
Code/Code/head/link_tree.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#include "head.h"
|
||||
|
||||
// 二叉树结点
|
||||
typedef struct BinaryTreeNode {
|
||||
element_type data;
|
||||
BinaryTreeNode *left_child, *right_child;
|
||||
} BinaryTreeNode, *BinaryTree;
|
||||
@@ -5,9 +5,9 @@ typedef struct {
|
||||
// 数据
|
||||
char *data;
|
||||
// 长度
|
||||
unsigned int length;
|
||||
int length;
|
||||
// 最大容量
|
||||
unsigned int max_size;
|
||||
int max_size;
|
||||
} SequenceString;
|
||||
|
||||
bool InitSequenceString(SequenceString &string) {
|
||||
@@ -38,4 +38,110 @@ SequenceString InitSequenceString(int max_size) {
|
||||
string->max_size = max_size;
|
||||
string->length = 0;
|
||||
return (SequenceString &) string;
|
||||
}
|
||||
|
||||
// 简单字符串匹配
|
||||
int LocateSimple(SequenceString string, SequenceString pattern) {
|
||||
int i = 0, j = 0;
|
||||
while (i < string.length && j < pattern.length) {
|
||||
// 匹配就继续后移
|
||||
if (string.data[i] == pattern.data[j]) {
|
||||
i++;
|
||||
j++;
|
||||
}
|
||||
// 不匹配就撤回
|
||||
else {
|
||||
i = i - j + 1;
|
||||
j = 0;
|
||||
}
|
||||
}
|
||||
if (j >= pattern.length) {
|
||||
return i - pattern.length;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 获取KMP的next数组
|
||||
int *GetNext(SequenceString string) {
|
||||
auto *next = (int *) malloc(sizeof(int) * string.length);
|
||||
next[0] = 0;
|
||||
// i为当前主串正在匹配的字符位置,也就是next数组的索引
|
||||
int i = 0, j = 0;
|
||||
while (i < string.length) {
|
||||
if (j == 0 || string.data[i] == string.data[j]) {
|
||||
next[++i] = ++j;
|
||||
} else {
|
||||
j = next[j];
|
||||
}
|
||||
}
|
||||
return next;
|
||||
}
|
||||
|
||||
// 获取KMP的nextval数组
|
||||
int *GetNextVal(SequenceString string) {
|
||||
auto *nextval = (int *) malloc(sizeof(int) * string.length);
|
||||
nextval[0] = 0;
|
||||
// i为当前主串正在匹配的字符位置,也就是next数组的索引
|
||||
int i = 0, j = 0;
|
||||
while (i < string.length) {
|
||||
if (j == 0 || string.data[i] == string.data[j]) {
|
||||
++i;
|
||||
++j;
|
||||
if (string.data[i] != string.data[j])
|
||||
nextval[i] = j;
|
||||
else
|
||||
nextval[i] = nextval[j];
|
||||
} else {
|
||||
j = nextval[j];
|
||||
}
|
||||
}
|
||||
return nextval;
|
||||
}
|
||||
|
||||
int LocateKMP(SequenceString string, SequenceString pattern, const int *next) {
|
||||
int i = 0, j = 0;
|
||||
while (i < string.length && j < pattern.length) {
|
||||
if (j == 0 || string.data[i] == pattern.data[j]) {
|
||||
// 匹配则继续比较
|
||||
++i;
|
||||
++j;
|
||||
} else {
|
||||
// 模式串右移
|
||||
j = next[j];
|
||||
}
|
||||
}
|
||||
if (j >= pattern.length) {
|
||||
// 匹配成功
|
||||
return i - pattern.length;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
int LocateKMP(SequenceString string, SequenceString pattern) {
|
||||
int *next = GetNext(pattern);
|
||||
int i = 0, j = 0;
|
||||
while (i < string.length && j < pattern.length) {
|
||||
if (j == 0 || string.data[i] == pattern.data[j]) {
|
||||
// 匹配则继续比较
|
||||
++i;
|
||||
++j;
|
||||
} else {
|
||||
// 模式串右移
|
||||
j = next[j];
|
||||
}
|
||||
}
|
||||
if (j >= pattern.length) {
|
||||
// 匹配成功
|
||||
return i - pattern.length;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
bool DestroySequenceString(SequenceString &string) {
|
||||
free(string.data);
|
||||
string.max_size = 0;
|
||||
string.length = 0;
|
||||
return true;
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
// ???????
|
||||
// 测试
|
||||
#include <iostream>
|
||||
#include "../head/sequence_list.h"
|
||||
#include "../head/link_list.h"
|
||||
@@ -11,6 +11,7 @@
|
||||
#include "../head/link_queue.h"
|
||||
#include "../head/sequence_string.h"
|
||||
#include "../head/link_string.h"
|
||||
#include "../head/link_tree.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user