数据结构:查找与排序

来源:互联网 发布:java画图程序源代码 编辑:程序博客网 时间:2024/06/04 19:32
#include <iostream>using namespace std;void ShellSort( int a[], int n )//希尔排序的非递归实现 {int gab = n / 2;while( gab >= 1 ){int iSubCnt = 0; //子列的起始索引while ( iSubCnt < gab ){//对子列进行插入排序for ( int i = iSubCnt + gab; i < n; i += gab ){int iTemp = a[i];for ( int j = i - gab; j >= iSubCnt; j -= gab ){if ( iTemp < a[j] )//当前元素比前面小{a[j+gab] = a[j];}else{break;}}a[j+gab] = iTemp;}++iSubCnt;}gab /= 2;}}int Search_Seq( int* a, int n, int key )//顺序查找{a[0] = key;int i = n;while( a[i] != key ){--i;}return i;}int Binary_Search( int* a, int n, int key )//二分查找{int low = 1;int high = n;int mid = 0;while( low <= high ){mid = ( low + high )/2;if ( key < a[mid] ){high = mid - 1;}else if ( key > a[mid] ){low = mid + 1;}else{return mid;}}return 0;}int Insertpolation_Search( int* a, int n, int key )//插值查找{int mid = 0; int high = n;int low = 1;while ( low <= high ){mid = low + (key - a[low])/(a[high] - a[low])*(high - low);if ( a[mid] > key ){high = mid - 1;}else if ( a[mid] < key ){low = mid + 1;}else{return mid;}}return 0;}typedef struct Node{Node():data(0),lchild(0),rchild(0){}int data;struct Node* lchild;struct Node* rchild;}BiTNode, *BiTree;//查找成功返回ture,并且p指向记录,否则返回false,p指向返回前最后访问的结点bool SearchBST( BiTree T, int key, BiTree f, BiTree& p ){if ( !T ){p = f;return false;}else if ( key == T->data ){p = T;return true;}else if ( key < T->data ){return SearchBST( T->lchild, key, T, p );}else {return SearchBST( T->rchild, key, T, p );}}bool InsertBST( BiTree& T, int key ){BiTree p = NULL;if ( !SearchBST( T, key, NULL, p) ){BiTree q = NULL;q = new BiTNode;memset( q, 0, sizeof(BiTNode) );q->data = key;if ( !p ){T = q;}else if ( key > p->data ){p->rchild = q;}else{p->lchild = q;}return true;}return false;}bool SearchBSTExt( BiTree T, int key, BiTree f, BiTree& p )//二叉排序树的非递归查找{BiTree q = T;while ( q ){if ( q->data == key ){p = q;return true;}else {f = q;q = (key < q->data) ? q->lchild : q->rchild;}}p = f;return false;}bool InsertBSTExt( BiTree& T, int key ){BiTree p = NULL;if ( !SearchBSTExt( T, key, NULL, p) ){BiTNode* s = new BiTNode;memset( s, 0, sizeof(BiTNode) );s->data = key;if ( !p ){T = s;}else if ( key < p->data ){p->lchild = s;}else{p->rchild = s;}return true;}else{return false;}}//二叉排序树的递归删除bool DeleteTNode( BiTNode*& p );bool DeleteBST( BiTree &T, int key ){if ( !T ){return false;}else{if ( key == T->data ){return DeleteTNode( T );}else if ( key < T->data ){return DeleteBST( T->lchild, key );}else{return DeleteBST( T->rchild, key );}}}bool DeleteTNode( BiTNode*& p ){if ( !p ){return false;}BiTNode* f = NULL;BiTNode* q = NULL;if ( p->lchild == NULL ) //左子树为空,只需重接右子树{q = p;p = p->rchild;delete q;}else if ( p->rchild == NULL )//右子树为空,只需重接左子树{q = p;p = p->lchild;delete q;}else //左右子树均不空{f = p;q = p->lchild;  while ( q->rchild )//转左,向右走到尽头{f = q;q = q->rchild;}p->data = q->data;if ( p != q ){f->rchild = q->lchild;}else{f->lchild = q->lchild;}delete q;}return true;}void printArray( int a[], int n ){for ( int i = 0; i < n; ++i ){cout << a[i] << " ";}cout << endl;}void main(){int a[15] = { 5, 3, 6, 2, 7, 9, 67, 12, 34, 6, 9, 0, -1, -5, 80};BiTree T = NULL;for ( int i = 0; i < 15; ++i ){if ( !InsertBST( T, a[i] ) ){cout << a[i] << " exist in the BST!" << endl;}}BiTree T2 = NULL;for ( int j = 0; j < 15; ++j ){if ( !InsertBSTExt( T2, a[j] ) ){cout << a[j] << " exist in the BST!" << endl;}}if ( DeleteBST( T2, 3 ) ){cout << "3 is delete successfully" << endl;if ( InsertBSTExt( T2, 3 ) ){cout << "3 is insert successfully!" << endl;}if ( !InsertBST( T2, 3 ) ){cout << "3 exist in the BST!" << endl;}}// ShellSort( a, 15 );// printArray( a, 15 );// // int b[6] = { 0, 1, 2, 3, 4, 6 };// int iResult = 0;// ShellSort( b, 6 );// // iResult = Search_Seq( b, 5, 6 );// cout << iResult << endl;// // // iResult = Binary_Search( b, 5, 6 );// cout << iResult << endl;// // iResult = Insertpolation_Search( b, 5, 0 );// // cout << iResult << endl;}

0 0