当前位置:
文档之家› 安徽工业大学数据结构实验报告
安徽工业大学数据结构实验报告
BSTree T=new BSTNode(); (*T).key=key; (*T).lchild=(*T).rchild=NULL; } else { p=(*T); while(p) {
q=p; if(p->key>key)
p=q->lchild; else if(p->key<key)
p=q->rchild; else {
实验二:查找
1.实验日期
2015 年 6 月 15 日
2.实验目的 了解查找算法
3.实验内容 从键盘随机输入(或随机产生)20 个整数 (1) 用顺序查找算法查找给定的某个数 (2) 对 20 个排序后进行折半查找 (3) 建立这 20 个数的二叉排序树,并进行查找。
4.设计思路 先用一个 20 的 int 数组来保存者 20 个数,然后进行顺序查找,再利用冒泡算法把它们
}
//(2)折半查找
int main()
{
int arr[20];
int i;
for (i=0; i<20; i++) {
cin>>arr[i];
}
int m;
cout<<"按顺序查找数:";
cin>>m;
Search(arr, m);
Bubble_Sort(arr, 20);
cout<<"使用折半查找数:";
cin>>m;
BinarySearch(arr, 20, m);
BSTNode *SearchBST(BSTree T,KeyType key);
void InsertBST(BSTree *Tptr,KeyType key);
BSTree CreateBST();
void ListBinTree(BSTree T);
void push(char str);//入栈 char pop(); //出栈 };
void order_stack :: push(char str) {
if(top>=max) {
cout<<"the stack is full"<<endl; }else
{ top++; temp[top]=str;
} }
void Quicksort( ElementType A[], int Left, int Right ) {
if ( Cutoff <= Right-Left ) { Pivot = Median3( A, Left, Right );
//(3)快速排序
i=Left; j=Right–1; for( ; ; ) {
cout<<"没有找到"<<key<<endl; else
cout<<"找到"<<key<<endl; ListBinTree(p); cout<<endl; return 0; } 6.运行与测试
7.心得或感想 这个题目其实也不难,前面的两个小问,中规中矩的写掉就好了,后面的建立二叉搜索
树是一个难点,建立好了,查找就不算什么问题了。
} }
//使用冒泡算法进行排序
void BinarySearch ( int * Tbl, int n, int K) { int left, right, mid, flag=0; left = 1; right = n; while ( left <= right ) { mid = (left+right)/2; if( K < Tbl[mid]) right = mid-1; else if( K > Tbl[mid]) left = mid+1; else { flag=1; cout<<K<<" index "<<mid; break; } } if (flag==0) cout<<"NotFound "<<K<<endl;
return T; if(key<T->key)
return SearchBST(T->lchild,key); else
return SearchBST(T->rchild,key); }
void InsertBST(BSTree *T,int key) {
BSTNode *p,*q; if((*T)==NULL) {
A[i] = A[i-1]; A[i] = Tmp; } }
void Selection_Sort ( ElementType A[], int N ) //(2)选择排序 {
for(i=0;i<N;i++){ MinPosition = ScanForMin( A, i, N–1 ); /* 从 A[i]到 A[N–1]中找最小元,并将其位置赋给 MinPosition */ Swap( A[i], A[MinPosition] ); /* 将未排序部分的最小元换到有序部分的最后位置 */
/* L = 左边起始位置, R = 右边起始位置, RightEnd = 右边终点位置 */
void Merge( ElementType A[], ElementType TmpA[],int L, int R, int RightEnd )
并排序
{
LeftEnd = R - 1; /* 左边终点位置。假设左右两列挨着 */
cout<<endl<<"该二叉排序树中含有关键字为"<<key<<"的节 点!"<<endl;
return; } } auto p=new BSTNode; p->key=key; p->lchild=p->rchild=NULL; if(q->key>key) q->lchild=p; else q->rchild=p; } }
//用广义表表示二叉树
BSTree T;
BSTNode *p;
int key;
cout<<"请输入关键字(输入 0 为结束标志):";
T=CreateBST();
ListBinTree(T);
cout<<endl;
cout<<"请输入欲查找关键字:"; cin>>key; p=SearchBST(T,key); if(p==Ne <iostream.h> #include <string.h> #define max 10
class order_stack { private :
int top; char temp[max];
public : order_stack() //初始化栈 { top=0; memset(temp,0,sizeof(temp)); }
cout<<"Not Found"<<endl; }
void Bubble_Sort(int *A, int N ) {
for (int P=N-1; P>=0; P-- ){ int flag = 0; for(int i=0; i<P; i++ ) { if ( A[i] > A[i+1] ) { int tmp=A[i]; A[i]=A[i+1]; A[i+1]=tmp; flag = 1; } } if ( flag==0 ) break;
实验三:排序
1.实验日期
2015 年 6 月 15 日
2.实验目的 了解各种排序的算法
3.实验内容 从键盘输入(或随机数产生)20 个数,试用下列算法进行排序 (1)直接插入排序 (2)直接选择排序 (3)快速排序 (4)归并排序 (5)堆排序(可选)
4.设计思路 可以直接用 20 位的 int 数组来保存输入的 20 个数,然后按照题目来进行排序就好了。
} }
void Search(int *a, int m) //(1)按顺序查找算法 {
int i; bool flag=false; for (i=0; i<20; i++) {
if (a[i]==m) { flag=true; cout<<"Found, index "<<i<<endl;
} } if (flag==false)
int ishuiwen(char *str,order_stack &stack) {
while(*str) {
if(*str!=stack.pop()) {
return 0; } str++; } return 1; }
int main() {
order_stack stack; char str[max]; cout<<"please input a string ,Size cannot exceed "<<max<<endl; cin>>str; for(unsigned int i=0;i<strlen(str);i++) {
5.主要程序代码 #include <iostream> using namespace std; void Insertion_Sort(int *A, int N ) { //(1)直接插入排序