A. 如何学习数据结构与算法
1、记住数据结构,记住算法思想(是什么)记住数据结构最直观的东西;记忆该数据结构的定义、性质、特点等。很多东西的理解和创新都是以记忆为前提的。
2、进行大量相关编程练习,用编程语言去实现某一数据结构上的算法(怎么办)
很多时候,理解一个算法很容易,很容易在纸上去模拟一个算法的实现过程。但具体实现,则是另一回事。一定得先自己思考,然后再去看书中给的编程语言实现。
3、“记住”特定情景下,利用某一特定的数据结构,去解决问题 (为什么+怎么办)
每介绍一种数据结构,浙大数据结构与算法的MOOC课程都会有一个实际问题来作为“引子”,回答了“这种数据结构为什么会出现”。有的是为了实现特定的操作,有的是为了时间和空间上(大部分考虑的是时间复杂性)效率的更高(所以,没事的时候,分析一下算法的时间复杂性)。这些东西,我们也须理解记忆。每一数据结构都有其特性,去解决某一类问题,我们需要去记忆,去感悟。
4、形成一个属于自己的知识体系
如何去“记住”(记好笔记,多多复习);在学习过程中,遇到挫折,产生挫败感该如何处理(这个是必然会发生的,总有难以理解不会的地方);如何进行心态方面的调整(欲速则不达,不过也有”敏捷学习“的概念)。
B. 数据结构中串模拟匹配中的KMP算法能用简单通俗的话解释一下吗谢谢啦!
错位移动模式串,找出失配位置之前
能与模式串以最大长度配对的串的一部分
例子:模式串
a
b
c
a
b
d,
(d处失配,错位移动模式串)
a
b
c
a
b
d
本例中模式串以最大长度配对的串的一部分为a
b
C. 数据结构与算法题需要回答
《数据结构与算法》模拟题
一、填空题:(共15分)(每空一分)
按照排序时,存放数据的设备,排序可分为<1> 排序和<2> 排序。内部排序和外部排序
图的常用的两种存储结构是<3> 和<4> 。邻接矩阵和邻接表
数据结构中的三种基本的结构形式是<5> 线性结构 和<6> 树型结构 、图型结构<7> 。
一个高度为6的二元树,最多有<8> 63 个结点。
线性查找的时间复杂度为:<9> O(n^2) ,折半查找的时间复杂度为:<10> O(nlogn) 、堆分类的时间复杂度为:<11> O(nlogn) 。
在采用散列法进行查找时,为了减少冲突的机会,散列函数必须具有较好的随机性,在我们介绍的几种散列函数构造法中,随机性最好的是<12> 随机数 法、最简单的构造方法是除留余数法<13> 。
线性表的三种存储结构是:数组、<14> 链表 、<15> 静态链表 。
二、回答下列问题:(共30分)
现有如右图的树,回答如下问题:看不见图
根结点有:
叶结点有:
具有最大度的结点:
结点的祖先是:
结点的后代是:
栈存放在数组A[m]中,栈底位置是m-1。试问:
栈空的条件是什么?top=m-1
栈满的条件是什么?top=-1
数据结构和抽象数据型的区别与联系:
数据结构(data structure)—是相互之间存在一种或多种特定关系的数据元素的集合。数据元素相互之间的关系称为结构。
抽象数据类型(ADT):是指一个数学模型(数据结构)以及定义在该模型(数据结构)上的一组操作。
D. 数据结构经典算法有哪些
二叉树遍历:
status initqueue(Queue &Q)
{//初始化一个空队列
Q.base=(QElemtype *)malloc(MAXSIZE*sizeof(QElemtype));
if(!Q.base)
exit(OVERFLOW);
Q.front=Q.rear=0;
return OK;
}
status inqueue(Queue &Q,BiTree e)
{//将元素e入队
if((Q.rear+1)%MAXSIZE==Q.front)
return ERROR;
Q.base[Q.rear]=e;
Q.rear=(Q.rear+1)%MAXSIZE;
return OK;
}
status outqueue(Queue &Q,BiTree &e)
{//删除队头元素,并用e返回其值
if(Q.front==Q.rear)
return ERROR;
e=Q.base[Q.front];
Q.front=(Q.front+1)%MAXSIZE;
return OK;
}
status emptyqueue(Queue Q)
{//若队列空,返回TRUE,否则返回FALSE
if(Q.front==Q.rear)
return TRUE;
return FALSE;
}
//以下是二叉树的算法
void creattree(BiTree &t)
{//先序顺序建立二叉树t
char ch;
ch=getchar();
if(ch==' ')
{
t=NULL;
return;
}
t=(BiTree)malloc(sizeof(BiNode));
if(!t) exit(OVERFLOW);
t->data=ch;
creattree(t->lchild);
creattree(t->rchild);
}
void print(TElemtype e)
{
printf("%c",e);
}
void pretraverse(BiTree t, void (*visit)(TElemtype e))
{//先序遍历二叉树t
if(t)
{
(*visit)(t->data);
pretraverse(t->lchild,visit);
pretraverse(t->rchild,visit);
}
}
void intraverse(BiTree t, void (*visit)(TElemtype e))
{//中序遍历二叉树t
if(t)
{
intraverse(t->lchild,visit);
(*visit)(t->data);
intraverse(t->rchild,visit);
}
}
void posttraverse(BiTree t, void (*visit)(TElemtype e))
{//后序遍历二叉树t
if(t)
{
posttraverse(t->lchild,visit);
posttraverse(t->rchild,visit);
(*visit)(t->data);
}
}
void leveltraverse(BiTree t, void (*visit)(TElemtype e))
{//层次遍历二叉树t
BiNode *p;
Queue Q;
//if(!t) return;
initqueue(Q);
p=t;
inqueue(Q,p);
while(!emptyqueue(Q))
{
outqueue(Q,p);
if(p)
{
(*visit)(p->data);
inqueue(Q,p->lchild);
inqueue(Q,p->rchild);
}
}
}
void destroytree(BiTree &t)
{
if(t==NULL) return;
else if(t->lchild==NULL&&t->rchild==NULL)
{
free(t);
return;
}
else{
destroytree(t->lchild);
destroytree(t->rchild);
free(t);
return;
}
}
E. PYTHON的数据结构和算法介绍
当你听到数据结构时,你会想到什么?
数据结构是根据类型组织和分组数据的容器。它们基于可变性和顺序而不同。可变性是指创建后改变对象的能力。我们有两种类型的数据结构,内置数据结构和用户定义的数据结构。
什么是数据算法-是由计算机执行的一系列步骤,接受输入并将其转换为目标输出。
列表是用方括号定义的,包含用逗号分隔的数据。该列表是可变的和有序的。它可以包含不同数据类型的混合。
months=['january','february','march','april','may','june','july','august','september','october','november','december']
print(months[0])#print the element with index 0
print(months[0:7])#all the elements from index 0 to 6
months[0]='birthday #exchange the value in index 0 with the word birthday
print(months)
元组是另一种容器。它是不可变有序元素序列的数据类型。不可变的,因为你不能从元组中添加和删除元素,或者就地排序。
length, width, height =9,3,1 #We can assign multiple variables in one shot
print("The dimensions are {} * {} * {}".format(length, width, height))
一组
集合是唯一元素的可变且无序的集合。它可以让我们快速地从列表中删除重复项。
numbers=[1,2,3,4,6,3,3]
unique_nums = set(numbers)
print(unique_nums)
models ={'declan','gift','jabali','viola','kinya','nick',betty' }
print('davis' in models)#check if there is turner in the set models
models.add('davis')
print(model.pop())remove the last item#
字典
字典是可变和无序的数据结构。它允许存储一对项目(即键和值)
下面的例子显示了将容器包含到其他容器中来创建复合数据结构的可能性。
* 用户定义的数据结构*
使用数组的堆栈堆栈是一种线性数据结构,其中元素按顺序排列。它遵循L.I.F.O的机制,意思是后进先出。因此,最后插入的元素将作为第一个元素被删除。这些操作是:
溢出情况——当我们试图在一个已经有最大元素的堆栈中再放一个元素时,就会出现这种情况。
下溢情况——当我们试图从一个空堆栈中删除一个元素时,就会出现这种情况。
队列是一种线性数据结构,其中的元素按顺序排列。它遵循先进先出的F.I.F.O机制。
描述队列特征的方面
两端:
前端-指向起始元素。
指向最后一个元素。
有两种操作:
树用于定义层次结构。它从根节点开始,再往下,最后的节点称为子节点。
链表
它是具有一系列连接节点的线性数据。每个节点存储数据并显示到下一个节点的路由。它们用来实现撤销功能和动态内存分配。
图表
这是一种数据结构,它收集了具有连接到其他节点的数据的节点。
它包括:
算法
在算法方面,我不会讲得太深,只是陈述方法和类型:
原文:https://www.tuicool.com/articles/hit/VRRvYr3
F. 遗传算法的模拟 数据结构题目
我这里给出了一个简单的模板如果需要编代码填代码的地方已经有提示了
/*package otherFile;
import java.util.Random;
import tGraph.TdcppGraph;
import shuffP.*;
*/
/**************
*
* @author vaqeteart
* 这里是遗传算法的核心框架遗传算法的步骤:
* 遗传算法核心部分的算法描述
* 算法步骤:
* 1、初始化
* 1.1、生成初始种群编码
* 1.2、计算每个个体的适配值。
* 1.3、记录当前最优适配值和最优个体
* 2、选择和遗传,
* 2.0、若当前最优适配值多次小于已有的最优适配值(或相差不大)很多次,或者进化的次数超过设定的限制,转4。
* 2.1、按照与每个个体的适配值成正比的概率选择个体并复制,复制之后个体的数目和原始种群数目一样。
* 2.2、(最好先打乱复制后种群的个体次序)对复制后个体进行两两配对交叉,生成相同数目的的下一代种群。
* 2.3、对下一代种群按照一定的概率进行变异
* 2.4、计算每个个体的适配值。
* 2.5、记录当前最优适配值和最优个体
* 2.6、转2
* 3、返回当前最优适配值以及其对应的编码,结束。
*
* 注意:
* 1.这里的内容相当于一个模板,编写具体的遗传算法的时候,可以按照这个模板的形式编写。
* 2.应该填写代码的地方都有提示的标记。
*/
public class GAKernel
{
//number of population
int popNum;//set the number to 20 in constructor
//current evolution times
int evolutionTim;
//limit of the evolution times
int evolutionLim;//set the number to 20 in constructor
//unaccepted times
//int eliminTim;
//limit of unaccepted times
//int eliminLim;
//current best euler code
//int curBestCode[];
//current best fitness
int curBestFitness;
//fitness of every indivial
int iFitness[];
//fator of compute the fitness
int factor;
//..................other members.............................................
//the graph
//public TdcppGraph tpGraph;
//the eula code group
//int codes[][];
//every population
//
//constructor
GAKernel(TdcppGraph tG,int eulerCode[])
{
popNum = 32;//2*2*2*2*2
//factor = Integer.MAX_VALUE / popNum;//to avoid overflow when select,for every fitness
evolutionTim = 0;/////
evolutionLim = 15;///////
//this.tpGraph=new TdcppGraph(tG);
//eliminTim = 0;
//eliminLim
curBestFitness = 0;
//curBestCode = new int[eulerCode.length];
//for(int i = 0; i < curBestCode.length; ++i)
//{
// curBestCode[i] = eulerCode[i];
//}
//??curBestFitness
iFitness = new int[popNum];
//codes = new int[popNum][];//lines
for(int i = 0; i < popNum; ++i)
{
//codes[i] = new int[eulerCode.length];
iFitness[i] = 0;
}
System.out.println("构造函数,需要填入代码");
}
//initialize the originalpopulation
void initPopulation()
{
//.......................初始化种群........................................
//int tmpCode[] = new int[curBestCode.length];
//get the initial indivial
//for(int i = 0; i < curBestCode.length; ++i)
//{
// tmpCode[i] = curBestCode[i];
// codes[0][i] = tmpCode[i];
//}
//ShuffEP s = new ShuffEP(this.tpGraph);
//for(int i = 1; i < popNum; ++i)
//{
// s.shuff(tmpCode);
// for(int j = 0; j < tmpCode.length; ++j)
// {
// codes[i][j] = tmpCode[j];
// }
//}
System.out.println("初始化种群,需要填入代码");
//get the initial fitness to the member iFitness
computeFitness();
//get the initial best indivial and fitness
recordBest();
}
//compute the fitness of every indivial in current population
void computeFitness()
{
//........................计算每个个体适应度.......................
//int time = 0;
//for(int i = 0; i < popNum; ++i)
//{
// time = 0;
// for(int j = 0; j < codes[i].length - 1; ++j)
// {
// time += tpGraph.Edge(codes[i][j], codes[i][j + 1]).getCost(time);
// }
// iFitness[i] = factor - time;
// if(iFitness[i] < 0)
// {
// System.out.println("错误,某个个体适应度过小使得适配值出现负数");//lkdebug
// System.exit(1);
// }
//}
System.out.println("计算每个个体适应度,需要填入代码");
}
//record the current best fitness and the according indivial
void recordBest()
{
int bestIndex = -1;
for(int i = 0; i < popNum; ++i)
{
if(curBestFitness < iFitness[i])
{
curBestFitness = iFitness[i];
bestIndex = i;
}
}
//............................记录最优个体.............................
if(bestIndex > -1)
{
// for(int i = 0; i < curBestCode.length; ++i)
// {
// curBestCode[i] = codes[bestIndex][i];
// }
}
System.out.println("记录最优个体,需要填入代码");
}
//selection and reproce indivial in population
void selIndivial()
{
int tmpiFitness[] = new int[iFitness.length];
tmpiFitness[0] = iFitness[0];
//建立临时群体用于选择交换
//.................................复制个体...............................
//清除原来的群体
//int tmpCode[][] = new int[popNum][];
//for(int i = 0; i < codes.length; ++i)
//{
// tmpCode[i] = new int[codes[i].length];//???
// for(int j = 0; j < codes[i].length; ++j)
// {// to tmpCode and reset codes
// tmpCode[i][j] = codes[i][j];
// codes[i][j] = -1;
// }
//}
System.out.println("复制个体,需要填入代码");
for(int i = 1; i < tmpiFitness.length; ++i)
{
tmpiFitness[i] = tmpiFitness[i - 1] + iFitness[i];
//iFitness[i] = 0;
}
//轮盘赌选择个体
for(int i = 0; i < popNum; ++i)
{
int rFit = new Random().nextInt(tmpiFitness[tmpiFitness.length - 1]);
for(int j = 0; j < tmpiFitness.length; ++j)
{
if(rFit < tmpiFitness[j])
{
rFit = j;//record the index of the indivial
break;
}
}
if(rFit == 0)
{
iFitness[i] = tmpiFitness[rFit];
}
else
{
iFitness[i] = tmpiFitness[rFit] - tmpiFitness[rFit - 1];// fitness
}
//....................................选择个体...........................
//for(int j = 0; j < tmpCode[rFit].length; ++j)
//{
// codes[i][j] =tmpCode[rFit][j];
//}
System.out.println("选择个体,需要填入代码");
}
//get the copied fitness in iFitness
}
//match every two indivial and cross the code
void matchCross()
{
//........................需要填入代码................................
System.out.println("配对交叉,需要填入代码");
}
//mutate by a specifical probability
void mutate()
{
//........................按照一定的概率进行变异.......................
System.out.println("按照一定的概率进行变异,需要填入代码");
}
//evolve current population
void evolve()
{
selIndivial();
matchCross();
mutate();
}
//compute the approximative best value by GA
//find approximative best solution by GA
public void compute()
{
initPopulation();
//while((evolutionTim < evolutionLim) && (eliminTim < eliminLim))
while(evolutionTim < evolutionLim)
{
evolve();
//get the initial fitness to the member iFitness
computeFitness();
//get the initial best indivial and fitness
recordBest();
++evolutionTim;
}
}
}