您的位置:首页 > 其它

堆排序、归并、快排、BSTree

2015-08-18 08:36 218 查看
//////////////////////////QuickSort////////////////////////////////////////

void QuickSort(int* a, int low, int high)

{

int mid = low;

if(low < high)

{

mid = Partition(a, low, high);

QuickSort(a, low, mid-1);

QuickSort(a, mid+1, high);

}

}

int Partition(int* a, int low, int high)

{

int base = a[low];

while(low < high)

{

while(a[high] > base && low < high )

{

--high;

}

swap(&a[high], &a[low]);

while(a[low]<base && low < high)

{

++low;

}

swap(&a[high], &a[low]);

}

return low;

}

void swap(int* a, int* b)

{

int temp = *a;

*a = *b;

*b = temp;

}

////////////////////////////////////////////////////////////////////////

//////////////////////////HeapSort////////////////////////////////

void HeapSort(int a[], int len)

{

for(int i=len/2-1;i>=0;--i) //i为数组中的序号

{

HeapAdjust(a, i, len-1);

}

swap(&a[0], &a[len-1]);

for(int i=len-1;i>0;--i)

{

HeapAdjust(a, 0, i-1);

swap(&a[0], &a[i-1]);

}

}

void HeapAdjust(int* a, int beg, int end)

{

if(!a || beg<0 || end<0 || beg>end)

{

return;

}

int nLeft = beg*2;

int nRight = beg*2+1;

int temp = 0;

for(int i=beg+1;2*i<=end+1;) //i nLeft nRight 为树中序号

{

nLeft = i*2;

nRight = i*2+1;

if(nLeft-1<=end && a[i-1] < a[nLeft-1])

{

swap(&a[i-1], &a[nLeft-1]);

i = nLeft;

}

if(nRight-1<=end && a[i-1] < a[nRight-1])

{

swap(&a[i-1], &a[nRight-1]);

i = nRight;

}

if(2*i < nRight)

{

i *=2;

}

}

}

////////////////////////////////////////////////////////////////////////

///////////////////////MergeSort///////////////////////////////////////////////

void MergeSort(int* b, int* a, int i, int n)

{

if(a==NULL || i-n>0)

return;

int temp[MAX_SIZE] = {0};

if(i==n)

{

a[i] = b[i];

}

else

{

int m = (i+n)/2;

MergeSort(b, temp, i, m);

MergeSort(b, temp, m+1, n);

Merge(temp, a, i,m, n);

}

}

void Merge(int* b, int* a, int i, int m, int n)

{

int k, j, s;

k = j = s = 0;

for(k = i,j=m+1;i<=m && j<=n;k++)

{

if(b[i]<b[j])

{

a[k] = b[i];

i++;

}

else

{

a[k] = b[j];

j++;

}

}

if(i<=m)

{

while(i<=m)

{

a[k] = b[i++];

k++;

}

}

if(j<=n)

{

while(j<=n)

{

a[k] = b[j++];

k++;

}

}

}

//////////////////////////////////////////////////////////////////////////////

///////////////////////////BSTree////////////////////////////////////

void InitBSTree(PBSTree &root)

{

root = NULL;

cout << "successfully initial"<<endl;

}

bool SearchBSTree(PBSTree &t, int key , PBSTree parent, PBSTree &k)

{

if(!t)

{

k = parent;

return false;

}

if(key == t->data)

{

k = t;

return true;

}

else if(key < t->data)

{

return SearchBSTree(t->left, key, t,k);

}

else

{

return SearchBSTree(t->right, key, t,k);

}

}

bool InsertBSTree(PBSTree &root, int key)

{

PBSTree p = NULL;

PBSTree pNode = NULL;

if(!SearchBSTree(root, key, NULL,p))

{

pNode = new BSTreeNode;

pNode->data = key;

pNode->left = pNode->right = NULL;

if(!p)

{

root = pNode;

}

else

{

if(key < p->data)

{

p->left = pNode;

}

else

{

p->right = pNode;

}

}

return true;

}

else

{

return false;

}

}

void InOrderTraverse(PBSTree t)

{

if(!t)

{

return;

}

InOrderTraverse(t->left);

cout << t->data << endl;

InOrderTraverse(t->right);

}

void PreOrderTraverse(PBSTree t)

{

if(!t)

{

return;

}

cout << t->data << endl;

PreOrderTraverse(t->left);

PreOrderTraverse(t->right);

}

void PostOrderTraverse(PBSTree t)

{

if(!t)

{

return;

}

PostOrderTraverse(t->left);

PostOrderTraverse(t->right);

cout << t->data << endl;

}

bool DeleteBSTreeNode(PBSTree &t, int key)

{

if(!t)

{

return false;

}

else

{

if(t->data == key)

{

PBSTree p=NULL;

PBSTree q=NULL;

if(NULL ==t->left)

{

p = t;

t = t->right;

delete p;

}

else if(NULL == t->right)

{

p = t;

t = t->left;

delete p;

}

else

{

p=q=NULL;

p = t;

q = t->left;

while(q->right)

{

p = q;

q = q->right;

}

t->data = q->data;

if(p != t)

{

p ->right = q->left;

}

else

{

p->left = q->left;

}

delete q;

}

return true;

}

else if(t->data > key)

{

return DeleteBSTreeNode(t->left , key);

}

else

{

return DeleteBSTreeNode(t->right, key);

}

}

}

////////////////////////////////////////////////////////////////////////

复制去Google翻译翻译结果
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: