抽象数据类型线性表
单链表基本操作
1 |
|
相关测试代码…可能还是有bug的1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43void Test1() {
ADTList *L;
InitList(L);
cout << ListEmpty(L) << endl;
ListInsert(L, 1, 3);
ListTraverse(L);
cout << "ok" << endl;
ListInsert(L, 1, 4);
ListTraverse(L);
cout << "ok" << endl;
ListInsert(L, 2, 5);
ListTraverse(L);
cout << "ok" << endl;
ListInsert(L, 4, 6);
ListTraverse(L);
cout << "ok" << endl;
cout << ListLength(L) << endl;
int x;
ListDelete(L, 2, x);
cout << x << endl;
ListTraverse(L);
cout << "ok" << endl;
cout << ListLength(L) << endl;
cout << "----------------------" << endl;
ListInsert(L, 3, 7);
ListTraverse(L);
cout << "ok" << endl;
cout << ListLength(L) << endl;
NextElem(L, 6, x);
cout << x << endl;
PriorElem(L, 6, x);
cout << x << endl;
NextElem(L, 7, x);
cout << x << endl;
GetElem(L, 3, x);
cout << x << endl;
cout << ListEmpty(L) << endl;
DestoryList(L);
cout << ListEmpty(L) << endl;
}
单链表操作 - 翻转
1 | void ListReverse(ADTList *L) { |
测试1
2
3
4
5
6
7
8
9
10void Test3() {
ADTList *La;
InitList(La);
ListInsert(La, 1, 10);
ListInsert(La, 1, 9);
ListInsert(La, 1, 7);
ListTraverse(La);
ListReverse(La);
ListTraverse(La);
}
多链表操作
1 | void Union(ADTList* &La, ADTList* Lb) { |
相关测试代码,同上…可能有错1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19void Test2() {
ADTList *La, *Lb;
InitList(La); InitList(Lb);
ListInsert(La, 1, 10);
ListInsert(La, 1, 9);
ListInsert(La, 1, 7);
ListTraverse(La);
ListInsert(Lb, 1, 8);
ListInsert(Lb, 1, 6);
ListInsert(Lb, 1, 5);
ListTraverse(Lb);
// Union(La, Lb);
// UnionWithOrder(La, Lb);
ADTList *Lc;
MergeList(La, Lb, Lc);
ListTraverse(Lc);
}
双向链表
1 |
|
相关测试代码,用的上面的1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43void Test1() {
DLL *L;
InitList(L);
cout << ListEmpty(L) << endl;
ListInsert(L, 1, 3);
ListTraverse(L);
cout << "ok" << endl;
ListInsert(L, 1, 4);
ListTraverse(L);
cout << "ok" << endl;
ListInsert(L, 2, 5);
ListTraverse(L);
cout << "ok" << endl;
ListInsert(L, 4, 6);
ListTraverse(L);
cout << "ok" << endl;
cout << ListLength(L) << endl;
int x;
ListDelete(L, 2, x);
cout << x << endl;
ListTraverse(L);
cout << "ok" << endl;
cout << ListLength(L) << endl;
cout << "----------------------" << endl;
ListInsert(L, 3, 7);
ListTraverse(L);
cout << "ok" << endl;
cout << ListLength(L) << endl;
NextElem(L, 6, x);
cout << x << endl;
PriorElem(L, 6, x);
cout << x << endl;
NextElem(L, 7, x);
cout << x << endl;
GetElem(L, 3, x);
cout << x << endl;
cout << ListEmpty(L) << endl;
DestoryList(L);
cout << ListEmpty(L) << endl;
}
多项式
1 |
|
测试代码 仅经过小数据测试2331
2
3
4
5
6
7
8
9
10
11
12
13void test() {
Polynomial *Pa, *Pb;
CreatePolyn(Pa, 3);
PrintPolyn(Pa);
CreatePolyn(Pb, 3);
PrintPolyn(Pb);
// AddPolyn(Pa, Pb);
// SubtractPolyn(Pa, Pb);
MultiplyPolyn(Pa, Pb);
PrintPolyn(Pa);
}
栈和队列
栈的几个应用
进制转换
1 |
|
括号匹配
1 |
|
栈
1 |
|
测试代码
1 | void Test() { |
1234出栈顺序
DFS搜索回溯 一共14种1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using namespace std;
const int N = 105;
struct Stack {
int stk[N], top;
Stack() {
memset(stk, 0, sizeof(stk));
top = 0;
}
};
void InitStack(Stack* &S) {
S=new Stack();
}
void DestoryStack(Stack *&S) {
if(S!=NULL) delete S;
}
void ClearStack(Stack *&S) {
memset(S->stk, 0, sizeof(S->stk));
S->top=0;
}
int StackEmpty(Stack *S) {
return S==NULL || S->top == 0;
}
int StackLength(Stack *S) {
return S==NULL ? 0 : S->top;
}
void GetTop(Stack *S, int &e) {
e=(S==NULL?0:S->stk[S->top]);
}
void Push(Stack* &S, int e) {
if(S==NULL) return;
S->stk[++S->top] = e;
}
void Pop(Stack* &S, int &e) {
if(S==NULL || S->top==0) { e=0; return; }
e = S->stk[S->top--];
}
void StackTraverse(Stack *S) {
if(S==NULL) return;
for(int i=1; i<=S->top; i++) cout << S->stk[i] << "- "[i==S->top];
cout << endl;
}
void StackCopy(Stack *S, Stack *T) {
if(T) delete T;
InitStack(T);
memcpy(T->stk ,S->stk, sizeof(S->stk));
T->top = S->top;
}
void DFS(int i, Stack *S, Stack *rec) {
if(i>4) {
int tmp=StackLength(rec), tmp1;
while(!StackEmpty(S)) {
int e; Pop(S, e);
Push(rec, e);
} StackTraverse(rec);
while(StackLength(rec) != tmp) {
Pop(rec, tmp1);
Push(S, tmp1);
}
return;
}
int tmp, ls = StackLength(S);
// 不出栈
Push(S, i); // 入栈
DFS(i+1, S, rec);
Pop(S, tmp);
// 出栈
for(int j=1; j<=ls; j++) {
for(int k=0; k<j; k++) {
int tmp1;
Pop(S, tmp1);
Push(rec, tmp1);
}
Push(S, i);
DFS(i+1, S, rec);
Pop(S, tmp);
for(int k=0; k<j; k++) {
int tmp1;
Pop(rec, tmp1);
Push(S, tmp1);
}
}
}
int main() {
Stack *S, *rec;
InitStack(S); InitStack(rec);
DFS(1, S, rec);
return 0;
}
迷宫问题
输出所有可能路径1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using namespace std;
const int N = 105;
struct Stack {
int stk[N], top;
Stack() {
memset(stk, 0, sizeof(stk));
top = 0;
}
};
void InitStack(Stack* &S) {
S=new Stack();
}
void DestoryStack(Stack *&S) {
if(S!=NULL) delete S;
}
void ClearStack(Stack *&S) {
memset(S->stk, 0, sizeof(S->stk));
S->top=0;
}
int StackEmpty(Stack *S) {
return S==NULL || S->top == 0;
}
int StackLength(Stack *S) {
return S==NULL ? 0 : S->top;
}
void GetTop(Stack *S, int &e) {
e=(S==NULL?0:S->stk[S->top]);
}
void Push(Stack* &S, int e) {
if(S==NULL) return;
S->stk[++S->top] = e;
}
void Pop(Stack* &S, int &e) {
if(S==NULL || S->top==0) { e=0; return; }
e = S->stk[S->top--];
}
void StackTraverse(Stack *S) {
if(S==NULL) return;
for(int i=1; i<=S->top; i++) cout << "(" << S->stk[i]/10 << "," << S->stk[i]%10 << ")" << "- "[i==S->top];
cout << endl;
}
void StackCopy(Stack *S, Stack *T) {
if(T) delete T;
InitStack(T);
memcpy(T->stk ,S->stk, sizeof(S->stk));
T->top = S->top;
}
int mp[10][10] = {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 1, 0, 0, 0, 1, 0, 1},
{1, 0, 0, 1, 0, 0, 0, 1, 0, 1},
{1, 0, 0, 0, 0, 1, 1, 0, 0, 1},
{1, 0, 1, 1, 1, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 1, 0, 0, 0, 0, 1},
{1, 0, 1, 0, 0, 0, 1, 0, 0, 1},
{1, 0, 1, 1, 1, 0, 1, 1, 0, 1},
{1, 1, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};
int tx[] = {0, 0, 1, -1};
int ty[] = {1, -1, 0, 0};
void Search(int x, int y, int dx, int dy, Stack *S) {
Push(S, x*10+y); mp[x][y]=1;
if(x==dx && y==dy) {
StackTraverse(S);
int tmp;
Pop(S, tmp); mp[x][y]=0;
return;
}
for(int i=0; i<4; i++) {
int ttx=x+tx[i], tty=y+ty[i];
if(!mp[ttx][tty]) {
Search(ttx, tty, dx, dy, S);
}
}
int tmp;
Pop(S, tmp); mp[x][y]=0;
}
int main() {
Stack *S; InitStack(S);
Search(1, 1, 8, 8, S);
return 0;
}
队列
1 |
|
测试不全面1
2
3
4
5
6
7
8
9
10
11
12
13
14
15void Test() {
Queue *Q; int e;
InitQueue(Q);
EnQueue(Q, 1);
GetHead(Q, e);
cout << e << endl;
cout << QueueLength(Q) << endl;
EnQueue(Q, 2);
EnQueue(Q, 4);
QueueTraverse(Q);
cout << QueueLength(Q) << endl;
DeQueue(Q, e);
cout << e << endl;
QueueTraverse(Q);
}
串
我的下标是从0开始的,课本是从1开始的,所以有很多pos--
的操作1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using namespace std;
const int N = 255;
typedef char SString[N];
void StrAssign(SString &T, char* chars) {
// strcpy(T, chars);
for(int i=0; *chars; chars++, i++) T[i]=*chars;
}
void StrCopy(SString &T, SString S) {
if(strlen(S)!=0) StrAssign(T, S);
}
int StrEmpty(SString S) {
if(S==NULL || strlen(S)==0) return true;
return false;
}
int StrLength(SString S) {
int l=0;
for( ;S[l++];);
return l-1;
}
int StrCompare(SString S, SString T) {
// return strcmp(S, T);
for(int i=0; i<min(StrLength(S), StrLength(T)); i++)
if(S[i]>T[i]) return 1;
else if(S[i]<T[i]) return -1;
if(StrLength(S) < StrLength(T)) return 1;
else if(StrLength(S) == StrLength(T)) return 0;
return -1;
}
void ClearString(SString &S) {
memset(S, 0, sizeof(S));
}
void Concat(SString &T, SString S1, SString S2) {
if(StrEmpty(S1)) { StrCopy(T, S2); return; }
if(StrEmpty(S2)) { StrCopy(T, S1); return; }
StrCopy(T, S1);
for(int i=0; i<StrLength(S2); i++) T[StrLength(S1)+i]=S2[i];
}
void SubString(SString &sub, SString S, int pos, int len) {
pos--;
ClearString(sub);
for(int i=pos; i<min(StrLength(S), pos+len); i++)
sub[i-pos]=S[i];
}
int Index(SString S, SString T, int pos) {
pos--;
if(pos<0) return 0;
int n=StrLength(S), m=StrLength(T);
for(int i=pos; i<n-m+1; i++) {
SString sub;
SubString(sub, S, i+1, m);
if(StrCompare(sub, T)==0) return i+1;
} return 0;
}
void StrInsert(SString &S, int pos, SString T) {
pos--;
if(pos<0 || pos>StrLength(S)) return;
int lt=StrLength(T), ls=StrLength(S);
for(int i=ls-1; i>=pos; --i) {
S[i+lt] = S[i];
}
for(int i=pos; i<pos+lt; i++) S[i]=T[i-pos];
}
void StrDelete(SString &S, int pos, int len) {
pos--;
if(pos<0 || pos+len>StrLength(S)) return;
int ls=StrLength(S);
for(int i=pos+len; i<ls; i++) S[i-len]=S[i];
for(int i=ls-len; i<ls; i++) S[i]=0;
}
void Replace(SString &S, SString T, SString V) {
int lt=StrLength(T), lv=StrLength(V);
for(int i=0; i<StrLength(S); i++) {
SString sub;
SubString(sub, S, i+1, lt);
if(StrCompare(sub, T)==0) {
StrDelete(S, i+1, lt);
StrInsert(S, i+1, V);
i=i+lv-1;
}
}
}
void DestroyString(SString &S) {
delete S;
}
测试1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27void test() {
SString S1, S2, S3;
ClearString(S1), ClearString(S2), ClearString(S3);
StrAssign(S1, "hello");
StrAssign(S2, "world");
cout << S1 << endl;
cout << S2 << endl;
cout << strlen(S1) << " " << StrLength(S1) << endl;
cout << strlen(S2) << " " << StrLength(S2) << endl;
// assert(strlen(S1)==StrLength(S1));
// assert(strlen(S2)==StrLength(S2));
cout << StrCompare(S1, S2) << endl;
cout << StrCompare("a", "aa") << endl;
cout << StrCompare("a", "a") << endl;
cout << StrCompare("aa", "a") << endl;
Concat(S3, S1, S2); cout << S3 << endl;
SubString(S3, S1, 2, 3); cout << S3 << endl;
cout << Index(S1, "l", 1) << endl;
cout << Index(S1, "l", 5) << endl;
StrDelete(S1, 2, 3); cout << S1 << endl;
StrInsert(S1, 1, "ool"); cout << S1 << endl;
Replace(S1, "o", "l"); cout << S1 << endl;
Replace(S1, "l", "ll"); cout << S1 << endl;
}
数组
没时间了…不是自己写的,照着打的…1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using namespace std;
typedef int ElemType;
typedef struct {
ElemType *base;
int dim;
int *bounds;
int *constants;
} Array;
int InitArray(Array &A, int dim, ...) {
if(dim<1 || dim>MAX_ARRAY_DIM) return 0;
A.dim=dim;
A.bounds=(int*) malloc(dim*sizeof(int));
int elemtotal=1;
va_list ap;
va_start(ap, dim);
for(int i=0; i<dim; i++) {
A.bounds[i]=va_arg(ap, int);
if(A.bounds[i]<0) return 0;
elemtotal *= A.bounds[i];
}
va_end(ap);
A.base=(ElemType *)malloc(elemtotal * sizeof(ElemType));
if(A.base) return 0;
A.constants=(int*)malloc(dim*sizeof(int));
A.constants[dim-1]=1;
for(int i=dim-2; i>=0; --i)
A.constants[i]=A.bounds[i+1]*A.constants[i+1];
return 1;
}
int DestroyArray(Array &A) {
if(!A.base) return 0;
free(A.base); A.base=NULL;
if(!A.bounds) return 0;
free(A.bounds); A.bounds=NULL;
if(!A.constants) return 0;
free(A.constants); A.constants=NULL;
return 1;
}
int Locate(Array A, va_list ap, int &off) {
off=0;
for(int i=0; i<A.dim; i++) {
int ind = va_arg(ap, int);
if(ind<0 || ind>=A.bounds[i]) return 0;
off+=A.constants[i]*ind;
} return 1;
}
int Value(Array A, ElemType &e, ...) {
va_list ap;
va_start(ap, e);
int result, off;
if((result=Locate(A, ap, off))<=0) return result;
e=*(A.base+off);
return 1;
}
int Assign(Array &A, ElemType e,...) {
va_list ap;
va_start(ap, e);
int result, off;
if((result=Locate(A, ap, off))<=0) return result;
*(A.base+off)=e;
return 1;
}
树
二叉树
基本功能和操作1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
using namespace std;
typedef int TElemType;
struct BiTNode {
TElemType data;
BiTNode *lc, *rc, *f;
BiTNode() {
this->data = 0;
this->lc = this->rc = this->f = NULL;
}
BiTNode(TElemType d, BiTNode *l=NULL, BiTNode *r=NULL, BiTNode *f = NULL) {
this->data = d;
this->lc = l;
this->rc = r;
this->f = f;
}
};
typedef BiTNode *BiTree;
void InitBiTree(BiTree &T) {
if(T) delete T;
T = NULL;
}
void DestroyBiTree(BiTree &T) {
if(!T) return;
DestroyBiTree(T->lc);
DestroyBiTree(T->rc);
if(T) delete T;
T = NULL;
}
void CreateBiTree(BiTree &T) {
int x; cin >> x;
if(x==0) { T=NULL; return; }
T = new BiTNode(x);
CreateBiTree(T->lc);
CreateBiTree(T->rc);
if(T->lc) T->lc->f=T;
if(T->rc) T->rc->f=T;
}
void ClearBiTree(BiTree &T) {
if(!T) return;
ClearBiTree(T->lc);
ClearBiTree(T->rc);
delete T; T=NULL;
}
int BiTreeEmpty(BiTree T) {
return T?1:0;
}
int BiTreeDepth(BiTree T) {
if(!T) return 0;
return max(BiTreeDepth(T->lc), BiTreeDepth(T->rc));
}
BiTree Root(BiTree T) {
while(T->f) T=T->f;
return T;
}
int Value(BiTree T, BiTree e) {
if(e) return e->data;
// if(!T) return 0;
// if(T==e) return e->data;
// int lcv = Value(T->lc, e);
// int rcv = Value(T->rc, e);
// if(lcv) return lcv;
// if(rcv) return rcv;
// return 0;
}
void Assign(BiTree T, BiTree &e, TElemType value) {
if(e) e->data = value;
// if(!T) return 0;
// if(T==e) { e->data = value; return; }
// Assign(T->lc, e, value);
// Assign(T->rc, e, value);
}
BiTree Parent(BiTree T, BiTree e) {
if(e) return e->f;
return NULL;
}
BiTree LeftChild(BiTree T, BiTree e) {
if(e) return e->lc;
return NULL;
}
BiTree RightChild(BiTree T, BiTree e) {
if(e) return e->rc;
return NULL;
}
BiTree LeftSibling(BiTree T, BiTree e) {
if(!e->f) return NULL;
if(e==e->f->rc) return e->f->lc;
return NULL;
}
BiTree RightSibling(BiTree T, BiTree e) {
if(!e->f) return NULL;
if(e==e->f->lc) return e->f->rc;
return NULL;
}
void InsertChild(BiTree T, BiTree p, int LR, BiTree c) {
if(LR==0) {
c->rc = p->lc;
if(p->lc) p->lc->f = c;
c->f = p; p->lc = c;
} else {
c->rc = p->rc;
if(p->rc) p->rc->f = c;
c->f = p; p->rc = c;
}
}
void DeleteChild(BiTree T, BiTree p, int LR) {
if(LR==0) {
DestroyBiTree(p->lc);
} else {
DestroyBiTree(p->rc);
}
}
void PreOrderTraverse(BiTree T) {
if(!T) return;
cout << T->data << " ";
PreOrderTraverse(T->lc);
PreOrderTraverse(T->rc);
}
void InOrderTraverse(BiTree T) {
if(!T) return;
InOrderTraverse(T->lc);
cout << T->data << " ";
InOrderTraverse(T->rc);
}
void PostOrderTraverse(BiTree T) {
if(!T) return;
PostOrderTraverse(T->lc);
PostOrderTraverse(T->rc);
cout << T->data << " ";
}
void LevelOrderTraverse(BiTree T) {
queue<BiTree> q;
q.push(T);
for(;!q.empty();) {
BiTree t=q.front(); q.pop();
if(!t) continue;
q.push(t->lc); q.push(t->rc);
cout << t->data << " ";
}
}
void PrePrderTraverseWithNonRecursive(BiTree T) { // 前序遍历非递归实现
stack<BiTree> q;
q.push(T);
for(;!q.empty();) {
BiTree x=q.top(); q.pop();
cout << x->data << " ";
if(x->rc) q.push(x->rc);
if(x->lc) q.push(x->lc);
}
}
void InPrderTraverseWithNonRecursive(BiTree T) { // 中序遍历非递归实现
stack<BiTree> q; map<BiTree, bool> mp;
q.push(T);
for(;!q.empty();) {
BiTree x=q.top(); q.pop();
if(mp.count(x)) { cout << x->data << " "; continue; }
mp[x]=1;
if(x->rc) q.push(x->rc);
q.push(x);
if(x->lc) q.push(x->lc);
}
}
void PostPrderTraverseWithNonRecursive(BiTree T) { // 后序遍历非递归实现
stack<BiTree> q; map<BiTree, bool> mp;
q.push(T);
for(;!q.empty();) {
BiTree x=q.top(); q.pop();
if(mp.count(x)) { cout << x->data << " "; continue; }
mp[x]=1;
q.push(x);
if(x->rc) q.push(x->rc);
if(x->lc) q.push(x->lc);
}
}
void test() {
BiTree T=NULL;
InitBiTree(T);
CreateBiTree(T); // Enter: 1 2 0 0 3 4 0 0 5 0 0
/* BiTree
1
| \
2 3
| \
4 5
*/
PreOrderTraverse(T); cout << endl;
InOrderTraverse(T); cout << endl;
PostOrderTraverse(T); cout << endl;
LevelOrderTraverse(T); cout << endl;
PrePrderTraverseWithNonRecursive(T); cout << endl;
InPrderTraverseWithNonRecursive(T); cout << endl;
PostPrderTraverseWithNonRecursive(T); cout << endl;
}
int main() {
test();
return 0;
}
二叉线索树
简单来说就是利用左右为空的节点,建立线索树,也就是中序遍历的前驱和后继1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using namespace std;
typedef int TElemType;
enum PointerTag { Link, Thread };
struct BiThrNode {
TElemType data;
BiThrNode *lchild, *rchild;
PointerTag LTag, RTag;
};
typedef BiThrNode* BiThrTree;
int InOrderTraverse_Thr(BiThrTree T) {
BiThrTree p=T->lchild;
for(; p!=T; p=p->rchild) {
while(p->LTag == Link) p=p->lchild;
cout << p->data << endl;
while(p->RTag == Thread && p->rchild!=T) {
p=p->rchild; cout << p->data << endl;
} p=p->rchild;
} return 0;
}
BiThrTree pre;
void InThreading(BiThrTree p) {
if(!p) return;
InThreading(p->lchild);
if(!p->lchild) { p->LTag=Thread; p->lchild=pre; }
if(!pre->rchild) { pre->RTag = Thread; pre->rchild = p; }
pre = p;
InThreading(p->rchild);
}
int InOrderThreading(BiThrTree &Thrt, BiThrTree T) {
if(!(Thrt = (BiThrTree)malloc(sizeof(BiThrNode)))) exit(1);
Thrt->LTag = Link; Thrt->RTag=Thread;
Thrt->rchild = Thrt;
if(!T) Thrt->lchild = Thrt;
else {
Thrt->lchild = T;
pre = Thrt;
InThreading(T);
pre->rchild = Thrt; pre->RTag = Thread;
Thrt->rchild = pre;
} return 0;
}
int main() {
return 0;
}
哈夫曼树和哈夫曼编码
贪心合并1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using namespace std;
struct HTNode {
int weight;
int parent, lchild, rchild;
};
typedef HTNode *HuffmanTree;
typedef char **HuffmanCode;
void Select(HuffmanTree HT, int n, int &s1, int &s2) {
s1=0;
for(int i=1; i<=n; i++) if(HT[i].parent==0) {
if(s1==0 || HT[i].weight<HT[s1].weight) s1=i;
}
s2=0;
for(int i=1; i<=n; i++) if(HT[i].parent==0 && i!=s1) {
if(s2==0 || HT[i].weight<HT[s2].weight) s2=i;
}
}
void HuffmanCoding(HuffmanTree &HT, HuffmanCode &HC, int *w, int n) {
if(n<=1) return;
int m = 2*n-1;
HT = (HuffmanTree)malloc((m+1)*sizeof(HTNode));
// cout << " init ok " << endl;
HuffmanTree p=HT+1;
int i;
for(i=1; i<=n; i++, p++, ++w) *p={*w, 0, 0, 0};
for(;i<=m; i++, p++) *p={0, 0, 0, 0};
// for(int i=1; i<=m; i++) cout << HT[i].weight << " " << HT[i].parent << " " << HT[i].lchild << " " << HT[i].rchild << endl;
// cout << " init ok " << endl;
for(i=n+1; i<=m; i++) {
int s1, s2;
Select(HT, i-1, s1, s2);
// cout << s1 << " " << s2 << endl;
HT[s1].parent = HT[s2].parent = i;
HT[i].lchild = s1; HT[i].rchild = s2;
HT[i].weight = HT[s1].weight + HT[s2].weight;
}
// cout << "HuffmanTree ok" << endl;
// for(int i=1; i<=m; i++) cout << HT[i].weight << " " << HT[i].parent << " " << HT[i].lchild << " " << HT[i].rchild << endl;
HC = (HuffmanCode)malloc((n+1)*sizeof(char*));
char *cd = (char *)malloc(n*sizeof(char));
cd[n-1]='\0';
for(i=1; i<=n; i++) {
int start = n-1;
for(int c=i, f=HT[i].parent; f!=0; c=f, f=HT[f].parent)
if(HT[f].lchild == c) cd[--start]='0';
else cd[--start]='1';
HC[i] = (char *)malloc((n-start)*sizeof(char));
strcpy(HC[i], &cd[start]);
} free(cd);
// cout << "HuffmanCode ok" << endl;
}
void Traverse(HuffmanTree HT, int rt) {
queue<int> q;
q.push(rt);
for(;!q.empty();) {
int x; x=q.front(); q.pop();
if(HT[x].lchild) q.push(HT[x].lchild), q.push(HT[x].rchild);
else cout << HT[x].weight << endl;
}
}
void test() { // test
int n = 4;
int w[] = {3, 1, 2, 1}; // 'ABACCDA' A-3, B-1, C-2, D-1
HuffmanTree HT;
HuffmanCode HC;
HuffmanCoding(HT, HC, w, n);
cout << "Traverse the tree" << endl;
Traverse(HT, 2*n-1); // traverse the tree by preorder
cout << "HuffmanCode: " << endl;
for(int i=1; i<=n; i++) cout <<(char)('A'+i-1) << ": " << HC[i] << endl; // A-0 C-10 B-110 D-111
}
int main() {
test();
return 0;
}
图
邻接矩阵
1 |
|
邻接表
1 |
|
十字链表
1 |
|