-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
415 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// | ||
// HASH.cpp | ||
// ADT | ||
// | ||
// Created by Jalor on 2020/12/18. | ||
// Copyright © 2020 Jalor. All rights reserved. | ||
// | ||
|
||
#include "HASH.hpp" | ||
Hash::Hash(int* data,int n,const int mode){ | ||
this->mode = mode; | ||
num = 11; | ||
this->data = new int[num]{0}; | ||
|
||
for (int i = 0; i < n; i++) { | ||
int idx = hashFun(data[i]); | ||
|
||
stealFun(idx, 0); | ||
|
||
this->data[idx] = data[i]; | ||
} | ||
|
||
} | ||
|
||
void Hash::outHash(){ | ||
for (int i = 0; i < num; i++) { | ||
cout<<"data["<<i<<"]"<<data[i]<<" idx:"<<hashFun(data[i])<<endl; | ||
} | ||
} | ||
|
||
int Hash::search(int var){ | ||
|
||
int idx = hashFun(var); | ||
di = 0; | ||
|
||
stealFun(idx, var); | ||
|
||
if(idx < num){ | ||
return idx; | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
int Hash::hashFun(int key){ | ||
int idx = key % (num); | ||
return idx; | ||
} | ||
|
||
void Hash::stealFun(int& idx,int standerd){ | ||
switch (mode) { | ||
case LINEAR: | ||
while(this->data[idx] != standerd && idx < num){ | ||
idx++; | ||
di++; | ||
} | ||
break; | ||
case PAIR: | ||
int temp = idx; | ||
int i = 1; | ||
int single = 1; | ||
while(this->data[idx] != standerd && idx < num){ | ||
idx = temp; | ||
if(single % 2 == 1) | ||
idx += i*i; | ||
else { | ||
idx -= i*i; | ||
i++; | ||
} | ||
single++; | ||
di++;//比较次数 | ||
} | ||
break; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// | ||
// HASH.hpp | ||
// ADT | ||
// | ||
// Created by Jalor on 2020/12/18. | ||
// Copyright © 2020 Jalor. All rights reserved. | ||
// | ||
|
||
#ifndef HASH_hpp | ||
#define HASH_hpp | ||
|
||
#include <iostream> | ||
using namespace std; | ||
|
||
const int LINEAR = 0; | ||
const int PAIR = 1; | ||
|
||
class Hash{ | ||
private: | ||
int* data;//散列数组 | ||
int num;//数组长度 | ||
int m;//模 | ||
int hashFun(int);//散列函数 | ||
void stealFun(int &,int );//解决冲突方法 | ||
int mode = 0; | ||
public: | ||
int di; | ||
Hash(int*,int,const int); | ||
int search(int); | ||
void outHash(); | ||
}; | ||
|
||
|
||
#endif /* HASH_hpp */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// | ||
// HUFFUMAN.cpp | ||
// ADT | ||
// | ||
// Created by Jalor on 2020/12/4. | ||
// Copyright © 2020 Jalor. All rights reserved. | ||
// | ||
|
||
#include "HUFFUMAN.hpp" | ||
|
||
/* 构造一颗哈夫曼树 */ | ||
void HuffumanCode::HuffmanTree(HNodeType HuffNode[MAXNODE], int n) | ||
{ | ||
/* i、j: 循环变量,m1、m2:构造哈夫曼树不同过程中两个最小权值结点的权值, | ||
x1、x2:构造哈夫曼树不同过程中两个最小权值结点在数组中的序号。*/ | ||
int i, j, m1, m2, x1, x2; | ||
/* 初始化存放哈夫曼树数组 HuffNode[] 中的结点 */ | ||
for (i = 0; i< 2*n-1 ; i++) | ||
{ | ||
HuffNode[i].weight = -1;//权值 | ||
HuffNode[i].parent = -1; | ||
HuffNode[i].lchild = -1; | ||
HuffNode[i].rchild = -1; | ||
} | ||
|
||
/* 输入 n 个叶子结点的权值 */ | ||
for (i=0; i<n;) | ||
{ | ||
for (j = 0; j < 30;j++) { | ||
if(Text[j]>0){ | ||
HuffNode[i].weight = Text[j]; | ||
i++; | ||
} | ||
} | ||
break; | ||
} | ||
|
||
/* 循环构造 Huffman 树 */ | ||
for (i = n; i < 2*n-1; i++) | ||
{ | ||
|
||
//************************************************************************************************ | ||
m1 = m2 = MAXVALUE;//m1、m2中存放两个无父结点且结点权值最小的两个结点 | ||
x1 = x2 = 0;//选择的节点 | ||
/* 找出所有结点中权值最小、无父结点的两个结点,并合并之为一颗二叉树 */ | ||
for (j = 0; j < 2*n-1; j++) | ||
{ | ||
if (HuffNode[j].weight>0 && HuffNode[j].weight < m1 && HuffNode[j].parent==-1) {//最小 | ||
|
||
m2 = m1; | ||
x2 = x1; | ||
m1 = HuffNode[j].weight; | ||
x1 = j; | ||
|
||
} else if (HuffNode[j].weight>0 && HuffNode[j].weight < m2 && HuffNode[j].parent==-1) {//次小 | ||
|
||
m2 = HuffNode[j].weight; | ||
x2 = j; | ||
|
||
} | ||
} | ||
//************************************************************************************************ | ||
|
||
HuffNode[x1].parent = i; | ||
HuffNode[x2].parent = i; | ||
HuffNode[i].weight = HuffNode[x1].weight + HuffNode[x2].weight; | ||
HuffNode[i].lchild = x1; | ||
HuffNode[i].rchild = x2; | ||
} | ||
} | ||
|
||
//解码 | ||
void decodeing(char string[],HNodeType Buf[],int Num) | ||
{ | ||
int i,tmp=0,code[1024]; | ||
int m=2*Num-1; | ||
char *nump; | ||
char num[1024]; | ||
for(i=0;i<strlen(string);i++) | ||
{ | ||
if(string[i]=='0') | ||
num[i]=0; | ||
else | ||
num[i]=1; | ||
} | ||
i=0; | ||
nump=&num[0]; | ||
|
||
while(nump<(&num[strlen(string)])) | ||
{tmp=m-1; | ||
while((Buf[tmp].lchild!=-1)&&(Buf[tmp].rchild!=-1)) | ||
{ | ||
|
||
if(*nump==0) | ||
{ | ||
tmp=Buf[tmp].lchild ; | ||
} | ||
else tmp=Buf[tmp].rchild; | ||
nump++; | ||
|
||
} | ||
|
||
printf("%d",Buf[tmp].value); | ||
} | ||
} | ||
|
||
HuffumanCode HuffumanCode::setContent(string s) { | ||
this->content = s; | ||
for (int i = 0; i< content.length(); i++) { | ||
if(Text[content[i] - 'a'] == 0){//若还没有录取进字库,统计字符个数 | ||
TextSize++; | ||
} | ||
Text[content[i] -'a']++; | ||
} | ||
return *this; | ||
} | ||
|
||
string HuffumanCode::getZipContent() { | ||
return this->content; | ||
} | ||
|
||
void HuffumanCode::doZip(){ | ||
HuffmanTree(HuffNode, TextSize); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// | ||
// HUFFUMAN.hpp | ||
// ADT | ||
// | ||
// Created by Jalor on 2020/12/4. | ||
// Copyright © 2020 Jalor. All rights reserved. | ||
// | ||
|
||
#ifndef HUFFUMAN_hpp | ||
#define HUFFUMAN_hpp | ||
|
||
#include <iostream> | ||
#include <cstdio> | ||
|
||
#define MAXBIT 100 | ||
#define MAXVALUE 10000 | ||
#define MAXLEAF 30 | ||
#define MAXNODE MAXLEAF*2 -1 | ||
using namespace std; | ||
|
||
typedef struct | ||
{ | ||
int weight;//权值 | ||
int parent;//父节点 | ||
int lchild;//左孩子 | ||
int rchild;//右孩子 | ||
int value;//值 | ||
}HNodeType; | ||
/* 结点结构体 */ | ||
|
||
class HuffumanCode { | ||
private: | ||
string content;//内容 | ||
string zip_content;//压缩内容 | ||
HNodeType HuffNode[MAXNODE]; | ||
int TextSize = 0;//文字包含每个字母数 | ||
int Text[30] = {0};//各字母权值 | ||
void HuffmanTree (HNodeType HuffNode[MAXNODE], int n); | ||
public: | ||
HuffumanCode setContent(string);//写入内容 | ||
void doZip();//压缩 | ||
string getZipContent();//获得压缩内容 | ||
}; | ||
|
||
#endif /* HUFFUMAN_hpp */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// | ||
// TSP.cpp | ||
// ADT | ||
// | ||
// Created by Jalor on 2020/12/11. | ||
// Copyright © 2020 Jalor. All rights reserved. | ||
// | ||
|
||
#include "TSP.hpp" | ||
TSP::TSP(int a[],int n,int e){ | ||
for (int i = 0; i < n; i++) { | ||
vert[i] = a[i]; | ||
} | ||
vertNum = n; | ||
edgeNum = e; | ||
} | ||
|
||
void TSP::setMap(int* a,int n){ | ||
for (int i = 0 ; i < TSP_MAX; i++) { | ||
for (int j = i; j <TSP_MAX; j++) { | ||
if(i == j){ | ||
edge[i][j] = 0; | ||
continue; | ||
} | ||
edge[i][j] = *(a+i*n+j); | ||
edge[j][i] = edge[i][j]; | ||
} | ||
} | ||
} | ||
|
||
void TSP::outSolution(int s,int f[]){ | ||
|
||
int flag[TSP_MAX] = {0};//标志位 | ||
|
||
bool allArrive = true;//是否全部读取完毕 | ||
|
||
for (int i = 0; i<TSP_MAX; i++) { | ||
flag[i] = f[i]; | ||
if(!flag[i]){ | ||
allArrive = false; | ||
} | ||
} | ||
|
||
if(allArrive){ | ||
ans += edge[s][start]; | ||
cout<<ans<<endl; | ||
found = true; | ||
return; | ||
} | ||
|
||
int max = 1000; | ||
int c = s; | ||
|
||
int cant[TSP_MAX] = {0}; | ||
|
||
while(!found){ | ||
|
||
for (int i = 0; i<TSP_MAX; i++) { | ||
if(edge[s][i]<max && edge[s][i] && !cant[i] && !flag[i]){ | ||
c = i; | ||
max = edge[s][i]; | ||
} | ||
} | ||
|
||
if(c == s){ | ||
return; | ||
} | ||
|
||
flag[c] = 1; | ||
ans += edge[s][c]; | ||
|
||
outSolution(c, flag); | ||
|
||
cant[c] = 1; | ||
ans -= edge[s][c]; | ||
flag[c] = 0; | ||
} | ||
} | ||
|
||
void TSP::setStart(int s){ | ||
start = s; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// | ||
// TSP.hpp | ||
// ADT | ||
// | ||
// Created by Jalor on 2020/12/11. | ||
// Copyright © 2020 Jalor. All rights reserved. | ||
// | ||
|
||
#ifndef TSP_hpp | ||
#define TSP_hpp | ||
|
||
#include <iostream> | ||
using namespace std; | ||
const int TSP_MAX = 1; | ||
class TSP{ | ||
private: | ||
int vert[TSP_MAX];//顶点集合 | ||
int edge[TSP_MAX][TSP_MAX];//对应地图 | ||
int vertNum = 0,edgeNum = 0; | ||
int ans = 0; | ||
bool found = false; | ||
int start = 0; | ||
public: | ||
TSP(int[],int,int); | ||
void setMap(int* ,int); | ||
void setStart(int s); | ||
void outSolution(int s,int f[]); | ||
}; | ||
|
||
#endif /* TSP_hpp */ |
Oops, something went wrong.