青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

Problem Solving using C++

Algorithm Study using C++

2009年7月26日 #

博客移至http://libiao.appspot.com

博客移至http://libiao.appspot.com,專(zhuān)注于專(zhuān)注于FreeBSD,C/C++, Python,算法和服務(wù)器開(kāi)發(fā)

posted @ 2009-07-26 22:12 Kingoal Lee's Alogrithm Study using cplusplus 閱讀(387) | 評(píng)論 (0)編輯 收藏

2007年9月5日 #

單鏈表的翻轉(zhuǎn)

非常簡(jiǎn)單的實(shí)現(xiàn),設(shè)置3個(gè)指針,分別指向當(dāng)前節(jié)點(diǎn)、前一個(gè)節(jié)點(diǎn)、后一個(gè)節(jié)點(diǎn),然后依次處理所有的節(jié)點(diǎn)就OK了。
具體代碼為:
#include <iostream>

using namespace std;

#ifndef 
null
#define 
null (void*)0
#endif

typedef struct node
{
    struct node
* next;
    
int data;
}node;

node
* head=(node*)null;

void reverse(node* root)
{
    node 
*cur,*pre,*next;
    
    pre
=(node*)null;
    cur
=root;
    next
=cur->next;
    
    
while(next)
    {
        cur
->next=pre;
        pre
=cur;
        cur
=next;
        next
=cur->next;
    }
    
    head
=cur;
    cur
->next=pre;
}

void insert(node* p)
{
    p
->next=head;
    head
=p;
}

void del(node* p)
{
    node 
*cur,*next;
    cur
=p;
    next
=p->next;
    
    
while(next)
    {
        delete cur;
        cur
=next;
        next
=cur->next;
    }
    
    delete cur;
}

void print(node* p)
{
    
while(p)
    {
        cout
<<p->data<<" ";
        p
=p->next;
    }
    
    cout
<<endl;
}

int main(int argc,char** argv)
{
    
for(int i=0;i<10;i++)
    {
        node
* p=new node;
        p
->next=(node*)null;
        p
->data=i;
        
        insert(p);
    }
    print(head);
    cout
<<"reverse order:"<<endl;
    
    reverse(head);
    print(head);
    
    del(head);
    
    
    system(
"pause");
    
return 0;
}


posted @ 2007-09-05 14:41 Kingoal Lee's Alogrithm Study using cplusplus 閱讀(2034) | 評(píng)論 (0)編輯 收藏

2007年8月31日 #

求兩排序數(shù)組的中位數(shù)

#include <iostream>
#include 
<string>

using namespace std;

int main(int argc,char* argv[])
{
    
int A[]={1,3,5,7,8,9};
    
int B[]={2,4,6,10,11,12,13,14};

    
int sizeA = sizeof(A)/sizeof(int);
    
int sizeB = sizeof(B)/sizeof(int);

    
int ma=0,na=sizeA-1;
    
int mb=0,nb=sizeB-1;

    
while(1)
    {
        
int ka=(na+ma+1)/2;
        
int kb=(nb+mb+1)/2;

        
if(na<ma)
        {
            cout
<<B[kb]<<endl;
            
break;
        }
        
if(nb<mb)
        {
            cout
<<A[ka]<<endl;
            
break;
        }
        
if(A[ka]==B[kb])//find the value
        {
            cout
<<A[ka]<<endl;
            
break;
        }

        
if((ma==na)&&((nb-mb+1)%2==0))//there is only one element at A[]
        {
            
if((A[na]<B[kb])&&(A[na]>=B[kb-1]))
            {
                cout
<<A[na]<<endl;
                
break;
            }
        }
        
if((ma==na)&&((nb-mb+1)%2))
        {
            
if((A[na]>B[kb])&&(A[na]<=B[kb+1]))
            {
                cout
<<A[na]<<endl;
                
break;
            }
        }

        
if((mb==nb)&&((na-ma+1)%2==0))//there is only one element at B[]
        {
            
if((B[nb]<A[ka])&&(B[nb]>=A[ka-1]))
            {
                cout
<<B[nb]<<endl;
                
break;
            }
        }
        
if((mb==nb)&&((na-ma+1)%2))
        {
            
if((B[nb]>A[ka])&&(B[nb]<=A[ka+1]))
            {
                cout
<<B[nb]<<endl;
                
break;
            }
        }

        
int offset=ka-ma;
        
if(offset>(kb-mb))
            offset
=kb-mb;
        
if(offset==0)
            offset
++;
        
//int offset=((ka>=kb)?ka:kb);

        
if(A[ka]<B[kb])
        {
            ma
+=offset;
            nb
-=offset;
        }

        
if(A[ka]>B[kb])
        {
            na
-=offset;
            mb
+=offset;
        }
    }

    system(
"pause");
    
return 0;
}

posted @ 2007-08-31 10:32 Kingoal Lee's Alogrithm Study using cplusplus 閱讀(925) | 評(píng)論 (0)編輯 收藏

2007年8月29日 #

常用字符串string操作--find

#include <iostream>
#include 
<string>
#include 
<cctype>
#include 
<vector>
#include 
<algorithm>
#include 
<iterator>

using namespace std;

int main(int argc,char** argv[])
{
    string line1
="We were her pride of 10 she named us:";
    string line2
="Benjamin, Phoenix, the Prodigal";
    string line3
="and perspicacious pacific Suzanne";
    string sentence 
= line1+' '+line2+' '+line3;
    
    string separator(
" \n\t:,\r\v\f");
    vector
<string> longest,shortest;
    
int num = 0;
    string::size_type startpos
=0,endpos=0;
    string word;
    
int longLen=0,shortLen=-1,wordlen;
    
    
while((startpos=sentence.find_first_not_of(separator,endpos))!=string::npos)
    {
        
++num;
        
        endpos
=sentence.find_first_of(separator,startpos);
        
if(endpos==string::npos)
        {
            wordlen 
= sentence.size()-startpos;
        }
        
else
        {
            wordlen 
= endpos-startpos;
        }
        
        word.assign(sentence.begin()
+startpos,sentence.begin()+wordlen+startpos);
        
        startpos 
= sentence.find_first_not_of(separator,endpos);
        
        
if(shortLen==-1)
        {
            shortLen
=longLen=wordlen;
            shortest.push_back(word);
            longest.push_back(word);
            
            
continue;
        }
        
if(shortLen==wordlen)
        {
            shortest.push_back(word);
        }
        
if(shortLen>wordlen)
        {
            shortest.clear();
            shortest.push_back(word);
            shortLen 
= wordlen;
        }
        
if(wordlen==longLen)
        {
            longest.push_back(word);
        }
        
if(wordlen>longLen)
        {
            longest.clear();
            longest.push_back(word);
            longLen
=wordlen;
        }    
    }
    
    cout
<<"Words:"<<num<<endl;
    cout
<<"Shortest:"<<shortLen<<endl;
    copy(shortest.begin(),shortest.end(),ostream_iterator
<string>(cout," "));
    cout
<<endl;
    cout
<<"Longest:"<<longLen<<endl;
    copy(longest.begin(),longest.end(),ostream_iterator
<string>(cout," "));
    cout
<<endl;
    
    system(
"pause");
    
return 0;
}
#include <iostream>
#include 
<string>
#include 
<cctype>
#include 
<vector>
#include 
<algorithm>
#include 
<iterator>

using namespace std;

void str_replace(string& str,const string& src,const string& dst)
{
    string::size_type pos 
= 0;
    
int srclen = src.size();
    
int dstlen = dst.size();
    
    
while((pos = str.find(src,pos))!=string::npos)
    {
        
//str.replace(pos,srclen,dst);
        str.erase(pos,srclen);
        str.insert(pos,dst);
        pos
+=dstlen;
    }
}

int main(int argc,char** argv[])
{
    
//replace/erase/insert
    string str("I like apple,what about you? apple tastes great!");
    cout
<<str<<endl;
    str_replace(str,
"apple","banana");
    cout
<<str<<endl;
    
    
//assign/append
    string q1("When lilacs last in the dooryard bloom'd");
    string q2(
"The child is father of the man");
    string sentence;
    
    sentence.assign(q2.begin(),q2.begin()
+13);
    sentence.append(q1.substr(q1.find(
"in"),15));
    cout
<<sentence<<endl;
    
    system(
"pause");
    
return 0;
}

posted @ 2007-08-29 11:12 Kingoal Lee's Alogrithm Study using cplusplus 閱讀(1192) | 評(píng)論 (0)編輯 收藏

2007年8月28日 #

STL常見(jiàn)操作(1)--排序

vector<int> vec;
generate_n(back_inserter(vec),100,rand);

sort(vec.begin(),vec.end());//or sort(vec.begin(),vec.end(),greater<int>());

copy(vec.begin(),vec.end(),ostream_iterator<int>(cout," "));

#include <iostream>
#include 
<functional>
#include 
<algorithm>
#include 
<iterator>
#include 
<vector>
#include 
<cstdlib>

using namespace std;

int main(int argc,char** argv)
{
    vector
<int> vec;
    generate_n(back_inserter(vec),
100,rand);
    
    copy(vec.begin(),vec.end(),ostream_iterator
<int>(cout," "));
    cout
<<endl;
    
    sort(vec.begin(),vec.end());
    
    copy(vec.begin(),vec.end(),ostream_iterator
<int>(cout," "));
    cout
<<endl;
    
    system(
"pause");
    
return 0;
}


posted @ 2007-08-28 10:54 Kingoal Lee's Alogrithm Study using cplusplus 閱讀(227) | 評(píng)論 (0)編輯 收藏

2007年8月27日 #

partial_sort--基于堆排序

經(jīng)常會(huì)出現(xiàn)求n個(gè)數(shù)中的前k個(gè)最小值(最大值),可以采取的策略為:

看n和k的大小,數(shù)組的規(guī)律。即便看情況也不一定有絕對(duì)的“最優(yōu)"
如果是基本有序的,那么就用Hoare的算法,每次選中間位置的數(shù)當(dāng)軸。
如果k或(n-k)是小常數(shù),那么部分排序算法,比如用堆。
如果要抵抗算法復(fù)雜度攻擊,那么可以考慮隨機(jī)Hoare或者linear time selection.
在k為小值的時(shí)候,可以采用基于堆排序的部分排序方法:
代碼如下:
#include <iostream>
#include 
<cstdlib>
#include 
<algorithm>
#include 
<iterator>

using namespace std;

void min_heapify(int arr[],int i,int size)
{
    
int left = 2*i+1;
    
int right = left+1;

    
int largest;

    
if((left<=size)&&(arr[left]>arr[i]))
    {
        largest 
= left;
    }
    
else
    {
        largest 
= i;
    }

    
if((right<=size)&&(arr[right]>arr[largest]))
    {
        largest 
= right;
    }

    
if(largest!=i)
    {
        
int temp = arr[i];
        arr[i]
=arr[largest];
        arr[largest]
=temp;

        min_heapify(arr,largest,size);
    }
}

void fillarray(int arr[],int size)
{
    
for(int i=0;i<size;i++)
    {
        arr[i]
=rand()%size;
    }
}

void print(const int arr[],int size)
{
    copy(arr,arr
+size,ostream_iterator<int>(cout," "));
    cout
<<endl;
}

int main(int argc,char* argv[])
{
    
int size,k;

    
while(cin>>size)
    {
        
int* buff=new int[size];

        fillarray(buff,size);
        print(buff,size);

        cout
<<"Please input the top k value:"<<endl;
        cin
>>k;

        
for(int i=(k-1)/2;i>=0;i--)
            min_heapify(buff,i,k
-1);

        print(buff,size);
        cout
<<endl<<endl;
        
for(int i=size-1;i>=k;i--)
        {
            
if(buff[i]<buff[0])
            {
                
int temp = buff[0];
                buff[
0]=buff[i];
                buff[i]
=temp;
    
                min_heapify(buff,
0,k-1);
            }
        }

        print(buff,size);

        delete [] buff;
    }
}


posted @ 2007-08-27 22:49 Kingoal Lee's Alogrithm Study using cplusplus 閱讀(427) | 評(píng)論 (0)編輯 收藏

2007年8月24日 #

二叉搜索樹(shù)操作(3)----非遞歸遍歷

應(yīng)用隊(duì)列(queue)和棧(stack)對(duì)二叉搜索樹(shù)進(jìn)行非遞歸遍歷
應(yīng)用隊(duì)列queue的是BFS,棧stack的是DFS
代碼為:
#include <iostream>
#include 
<cstdlib>
#include 
<queue>
#include 
<stack>
#include 
<algorithm>

using namespace std;

#ifndef NULL
#define NULL 
0
#endif

#ifndef MAXSIZE
#define MAXSIZE    
10
#endif

typedef struct BST
//Binary Search Tree
{
    
int key;
    
//maybe there are some other satellites
    
    struct BST
* left;
    struct BST
* right;
    struct BST
* parent;
} BST;

BST
* root=NULL;

void BST_Insert(int key)//add value key to the Binary Search Tree
{
    BST
* y=NULL;//y records the parent node
    BST* x=root;//x records the current node
    
    BST
* node = new BST();
    node
->key = key;
    node
->left = NULL;
    node
->right = NULL;
    node
->parent = NULL;
    
    
while(x!=NULL)
    {
        y
=x;
        
        
if(key<x->key)
            x
=x->left;
        
else
            x
=x->right;
    }
    
    node
->parent=y;
    
    
if(y==NULL)
        root 
= node;
    
else
    {
        
if(key<y->key)
            y
->left=node;
        
else
            y
->right=node;
    }
}

void BST_Delete(BST* p)
{
    
if(p)
    {
        BST_Delete(p
->left);
        BST_Delete(p
->right);
        delete p;
    }
}

void BST_Build()
{
    
int temp;
    
    cout
<<"Original Input:"<<endl;
    
for(int i=0;i<MAXSIZE;i++)
    {
        temp
=rand()%MAXSIZE;
        cout
<<temp<<" ";
        BST_Insert(temp);
    }
    cout
<<endl;
}

void BST_Inorder_Walk(BST* p)
{
    
    
if(p)
    {
        BST_Inorder_Walk(p
->left);
        cout
<<p->key<<" ";
        BST_Inorder_Walk(p
->right);
    }
}

void BST_Preorder_Walk(BST* p)
{
    
    
if(p)
    {
        cout
<<p->key<<" ";
        BST_Preorder_Walk(p
->left);
        BST_Preorder_Walk(p
->right);
    }
}

void BST_Postorder_Walk(BST* p)
{
    
if(p)
    {
        BST_Postorder_Walk(p
->left);
        BST_Postorder_Walk(p
->right);
        cout
<<p->key<<" ";
    }
}

void BST_Layer_Walk()
{
    queue
<BST*> queue;
    BST
* p;
    queue.push(root);
    
    
while(!queue.empty())
    {
        p
=queue.front();
        
        queue.pop();
        
if(p->left)
            queue.push(p
->left);
        
if(p->right)
            queue.push(p
->right);
        
        cout
<<p->key<<" ";
    }
    
    cout
<<endl;
}

void Inorder_Walk(BST* node=root)
{
    stack
<BST*> stk;
    
    
while(!stk.empty()||node)
    {
        
if(node)
        {
            stk.push(node);
            node
=node->left;
        }
        
else
        {
            node
=stk.top();
            cout
<<node->key<<" ";
            stk.pop();
            node
=node->right;
        }
    }
}

void Preorder_Walk(BST* node=root)
{
    stack
<BST*> stk;
    
    
while(!stk.empty()||node)
    {
        
if(node)
        {
            cout
<<node->key<<" ";
            stk.push(node);
            node
=node->left;
        }
        
else
        {
            node
=stk.top();
            stk.pop();
            node
=node->right;
        }
    }
}

void Postorder_Walk(BST* node=root)
{
    stack
<BST*> stk;
    BST
* p;
    
int flag = 0;//0 stands for left, 1 stands for right

    
do
    {
        
while(node)
        {
            stk.push(node);
            node
=node->left;
        }
        
        p
=NULL;
        flag
=0;
        
        
while(!stk.empty()&&(flag==0))
        {
            node
=stk.top();
            
if(node->right==p)
            {
                stk.pop();
                p
=node;
                cout
<<node->key<<" ";
            }
            
else
            {
                node
= node->right;
                flag
=1;
            }
        }
    }
while(!stk.empty());
}

int main(int argc,char* argv[])
{
    
int input;
    
    BST_Build();
    
    cout
<<"Inorder Walk:"<<endl;
    
//BST_Inorder_Walk(root);
    Inorder_Walk();
    cout
<<endl;
    
    cout
<<"Preorder Walk:"<<endl;
    BST_Preorder_Walk(root);
    cout
<<endl;
    
    cout
<<"Stack Preorder Walk:"<<endl;
    Preorder_Walk(root);
    cout
<<endl;
    
    cout
<<"Postorder Walk:"<<endl;
    BST_Postorder_Walk(root);
    cout
<<endl;
    
    cout
<<"Stack Postorder Walk:"<<endl;
    Postorder_Walk(root);
    cout
<<endl;
    
    cout
<<"Layer Walk:"<<endl;
    BST_Layer_Walk();
    
    BST_Delete(root);
    
    cout
<<endl;
    system(
"pause");
    
return 0;
}


posted @ 2007-08-24 16:16 Kingoal Lee's Alogrithm Study using cplusplus 閱讀(320) | 評(píng)論 (0)編輯 收藏

二叉搜索樹(shù)操作(2)--其他

#include <iostream>
#include 
<cstdlib>
using namespace std;

#ifndef NULL
#define NULL 
0
#endif

#ifndef MAXSIZE
#define MAXSIZE    
10
#endif

typedef struct BST
//Binary Search Tree
{
    
int key;
    
//maybe there are some other satellites
    
    struct BST
* left;
    struct BST
* right;
    struct BST
* parent;
} BST;

BST
* root=NULL;

void BST_Insert(int key)//add value key to the Binary Search Tree
{
    BST
* y=NULL;//y records the parent node
    BST* x=root;//x records the current node
    
    BST
* node = new BST();
    node
->key = key;
    node
->left = NULL;
    node
->right = NULL;
    node
->parent = NULL;
    
    
while(x!=NULL)
    {
        y
=x;
        
        
if(key<x->key)
            x
=x->left;
        
else
            x
=x->right;
    }
    
    node
->parent=y;
    
    
if(y==NULL)
        root 
= node;
    
else
    {
        
if(key<y->key)
            y
->left=node;
        
else
            y
->right=node;
    }
}

void BST_Delete(BST* p)
{
    
if(p)
    {
        BST_Delete(p
->left);
        BST_Delete(p
->right);
        delete p;
    }
}

void BST_Build()
{
    
int temp;
    
    cout
<<"Original Input:"<<endl;
    
for(int i=0;i<MAXSIZE;i++)
    {
        temp
=rand()%MAXSIZE;
        cout
<<temp<<" ";
        BST_Insert(temp);
    }
    cout
<<endl;
}

void BST_Inorder_Walk(BST* p)
{
    
    
if(p)
    {
        BST_Inorder_Walk(p
->left);
        cout
<<p->key<<" ";
        BST_Inorder_Walk(p
->right);
    }
}

void BST_Preorder_Walk(BST* p)
{
    
    
if(p)
    {
        cout
<<p->key<<" ";
        BST_Preorder_Walk(p
->left);
        BST_Preorder_Walk(p
->right);
    }
}

void BST_Postorder_Walk(BST* p)
{
    
if(p)
    {
        BST_Postorder_Walk(p
->left);
        BST_Postorder_Walk(p
->right);
        cout
<<p->key<<" ";
    }
}

BST
* BST_Search(int key)
{
    BST
* x=root;
    
    
while(x)
    {
        
if(x->key==key)
            
return x;
        
else
            
if(x->key>key)
                x
=x->left;
            
else
                x
=x->right;
    }
    
    
return NULL;    
}

BST
* BST_Min(BST* p=root)
{
    
//BST* p = root;
    
    
while(p->left)
    {
        p
=p->left;
    }
    
    
return p;
}

BST
* BST_Max(BST* p=root)
{
    
//BST* p = root;
    
    
while(p->right)
    {
        p
=p->right;
    }
    
    
return p;
}

BST
* BST_Successor(int key)
{
    BST
* p = BST_Search(key);
    BST
* y;
    
    
if(p)
    {
        
if(p->right)
        {
            
return BST_Min(p->right);
        }
        
        y
=p->parent;
        
while(y&&(y->right==p))
        {
            p
=y;
            y
=y->parent;
        }
        
        
return y;
    }
    
    
return NULL;
}

BST
* BST_Predecessor(int key)
{
    BST
* p = BST_Search(key);
    BST
* y;
    
    
if(p)
    {
        
if(p->left)
            
return BST_Max(p->left);
        
        y
=p->parent;
        
while(y&&(y->left==p))
        {
            p
=y;
            y
=y->parent;
        }
        
        
return y;
    }
    
    
return p;
}

BST
* BST_Delete(int key)
{
    BST
* p = BST_Search(key);
    BST
* y,*x;
    
    
if(p)
    {
        
if(!p->left||!p->right)
        {
            y
=p;
        }
        
else
            y
=BST_Successor(key);
            
        
if(y->left)
            x
=y->left;
        
else
            x
=y->right;
        
        
if(x!=NULL)
            x
->parent=y->parent;
            
        
if(!y->parent)
            root
=x;
        
else
        {
            
if(y==y->parent->left)
                y
->parent->left=x;
            
else
                y
->parent->right=x;
        }
        
        
if(y!=p)
            p
->key=y->key;
        
        
return y;
    }
    
    
return p;
}

int main(int argc,char* argv[])
{
    
int input;
    
    BST_Build();
    
    BST_Delete(
7);
    
    cout
<<"Inorder Walk:"<<endl;
    BST_Inorder_Walk(root);
    cout
<<endl;
    
    cout
<<"Preorder Walk:"<<endl;
    BST_Preorder_Walk(root);
    cout
<<endl;
    
    cout
<<"Postorder Walk:"<<endl;
    BST_Postorder_Walk(root);
    cout
<<endl;
    
    cout
<<"Min: "<<BST_Min()->key<<endl;
    cout
<<"Max: "<<BST_Max()->key<<endl;
    
    
while(1)
    {
        cin
>>input;
        
        
if(input==-1)
            
break;
        
        BST
* p;
        
if(p=BST_Search(input))
        {
            cout
<<"Parent="<<(p->parent)->key<<endl;
            
if(p->left)
                cout
<<"Left:"<<p->left->key<<endl;
            
if(p->right)
                cout
<<"Right:"<<p->right->key<<endl;
        }
        
else
        {
            cout
<<"Not found!"<<endl;
            
break;
        }
        
        
if(p=BST_Predecessor(input))
        {
            cout
<<"Predecessor:"<<p->key<<endl;
        }
        
        
if(p=BST_Successor(input))
        {
            cout
<<"Successor:"<<p->key<<endl;
        }
    }
    
    BST_Delete(root);
    
    cout
<<endl;
    system(
"pause");
    
return 0;
}

posted @ 2007-08-24 12:35 Kingoal Lee's Alogrithm Study using cplusplus 閱讀(191) | 評(píng)論 (0)編輯 收藏

二叉搜索樹(shù)操作(1)----生成

二叉搜索樹(shù)是專(zhuān)門(mén)有利于搜索(時(shí)間復(fù)雜度為logn)的二叉樹(shù)。
生成一棵二叉搜索樹(shù)關(guān)鍵是不斷地插入數(shù)據(jù)到該樹(shù)中,若當(dāng)前的root為NULL,則當(dāng)前插入的節(jié)點(diǎn)為root,否則比較左右樹(shù)插入,直到為NULL為之。
二叉搜索樹(shù)的插入代碼為:
void BST_Insert(int key)
{
    構(gòu)建當(dāng)前節(jié)點(diǎn)current,初始化current,設(shè)置其value=key,左右父節(jié)點(diǎn)都為NULL;
   //用x,y兩個(gè)指針來(lái)跟蹤current放置的位置
   y=NULL;
  x=root;

  while(x)//x有下面的節(jié)點(diǎn)時(shí)
    {  
         y=x;
         x的key和當(dāng)前參數(shù)里面的key做對(duì)比,若大于當(dāng)前key,則x=left[x],否則x=right[x];
    }

  比較y和NULL的關(guān)系,若y==NULL,則root=NULL,有root=current;
  設(shè)置parent[current]=y;
  比較current的key和y的key的大小,若大,則left[y]=current,否則right[y]=current;
//插入完畢
}
使用代碼表示為:
#include <iostream>
#include 
<cstdlib>
using namespace std;

#ifndef NULL
#define NULL 
0
#endif

#ifndef MAXSIZE
#define MAXSIZE    
10
#endif

typedef struct BST
//Binary Search Tree
{
    
int key;
    
//maybe there are some other satellites
    
    struct BST
* left;
    struct BST
* right;
    struct BST
* parent;
} BST;

BST
* root=NULL;

void BST_Insert(int key)//add value key to the Binary Search Tree
{
    BST
* y=NULL;//y records the parent node
    BST* x=root;//x records the current node
    
    BST
* node = new BST();
    node
->key = key;
    node
->left = NULL;
    node
->right = NULL;
    node
->parent = NULL;
    
    
while(x!=NULL)
    {
        y
=x;
        
        
if(key<x->key)
            x
=x->left;
        
else
            x
=x->right;
    }
    
    node
->parent=y;
    
    
if(y==NULL)
        root 
= node;
    
else
    {
        
if(key<y->key)
            y
->left=node;
        
else
            y
->right=node;
    }
}

void BST_Delete(BST* p)
{
    
if(p)
    {
        BST_Delete(p
->left);
        BST_Delete(p
->right);
        delete p;
    }
}

void BST_Build()
{
    
int temp;
    
    
for(int i=0;i<MAXSIZE;i++)
    {
        temp
=rand()%MAXSIZE;
        cout
<<temp<<" ";
        BST_Insert(temp);
    }
    cout
<<endl;
}

void BST_Inorder_Walk(BST* p)
{
    
if(p)
    {
        BST_Inorder_Walk(p
->left);
        cout
<<p->key<<" ";
        BST_Inorder_Walk(p
->right);
    }
}

int main(int argc,char* argv[])
{
    BST_Build();
    BST_Inorder_Walk(root);
    BST_Delete(root);
    
    cout
<<endl;
    system(
"pause");
    
return 0;
}


posted @ 2007-08-24 09:19 Kingoal Lee's Alogrithm Study using cplusplus 閱讀(548) | 評(píng)論 (0)編輯 收藏

2007年8月23日 #

選擇算法--Random Select

從一個(gè)亂序數(shù)組A[1....n]中選擇排第i的數(shù),就是經(jīng)典的選擇select問(wèn)題
最Naive的辦法就是先對(duì)整個(gè)數(shù)組進(jìn)行排序(可以使用quicksort/merge sort/heap sort)但是其算法復(fù)雜度為O(nlogn),如果使用quicksort的partition的變種的話(huà),其最差復(fù)雜度為O(n^2),但是平均復(fù)雜度為O(n).其核心算法如下:
int randselect(int p,int r,int i)
{
    if(p==r)
          return arr[p];
   int q = partition(p,r);//調(diào)用quicksort里面的partition
   int k = q-p+1;
   if(i==k)
           return arr[q];
   if(i<k)
           return randselect(p,q-1,i);
   if(i>k)
           return randselect(q+1,r,i-k);
}

代碼如下:

#include <iostream>
#include 
<iterator>
#include 
<algorithm>
#include 
<cstdlib>

using namespace std;

#define MAXSIZE    
100
int arr[MAXSIZE];

void fillarray()
{
    
for(int i=0;i<MAXSIZE;i++)
    {
        arr[i]
=rand()%MAXSIZE;
    }
}

void print(bool flag=true)
{
    
if(flag)
    {
        copy(arr,arr
+MAXSIZE,ostream_iterator<int>(cout," "));
        cout
<<endl;
    }
}

int partition(int p,int r)
{
    
int pivot = arr[r];
   
    
int i=p-1;
    
for(int j=p;j<r;j++)
    {
        
if(arr[j]<pivot)
        {
            i
++;
           
            
int temp = arr[j];
            arr[j]
=arr[i];
            arr[i]
=temp;
        }
    }
   
    arr[r]
=arr[i+1];
    arr[i
+1]=pivot;
   
    
return i+1;
}

void quicksort(int p,int r)
{
    
if(p<r)
    {
        
int q=partition(p,r);
        quicksort(p,q
-1);
        quicksort(q
+1,r);
    }
}

int randselect(int p,int r,int i)
{
    
if(p==r)
        
return arr[p];
       
    
int q = partition(p,r);
    
int k = q-p+1;
    
if(i==k)
        
return arr[q];
    
if(i<k)
        
return randselect(p,q-1,i);
    
if(i>k)
        
return randselect(q+1,r,i-k);
}

int main(int argc,char* argv[])
{
    fillarray();
    print();
   
    
//quicksort(0,MAXSIZE-1);
    
//print();
    for(int i=0;i<MAXSIZE;i++)
        cout
<<randselect(0,MAXSIZE-1,i+1)<<" ";
   
    cout
<<endl;
   
    system(
"pause");
    
return 0;
}

posted @ 2007-08-23 10:35 Kingoal Lee's Alogrithm Study using cplusplus 閱讀(890) | 評(píng)論 (0)編輯 收藏

僅列出標(biāo)題  下一頁(yè)

My Links

Blog Stats

常用鏈接

留言簿(1)

隨筆檔案

搜索

最新評(píng)論

閱讀排行榜

評(píng)論排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            久久这里只有| 在线不卡中文字幕| 伊人久久大香线| 久久网站热最新地址| 亚洲大片精品永久免费| 亚洲精品视频一区| 欧美日韩福利在线观看| 亚洲一区网站| 国产精品亚洲欧美| 亚洲欧美一区二区在线观看| 久久精品一区二区国产| 欧美~级网站不卡| 亚洲精品国产精品国自产在线 | 麻豆精品视频在线| 国产精品嫩草99av在线| 久久综合久色欧美综合狠狠 | 亚洲一区二区三区激情| 亚洲一区二区动漫| 久久国产精品久久国产精品| 麻豆精品在线视频| 狼狼综合久久久久综合网| 亚洲第一毛片| 在线视频精品一| 欧美在线免费视频| 欧美精品成人一区二区在线观看| 国产精品久久久久久久久免费樱桃| 国产欧美一区二区三区沐欲| 国产三级欧美三级| 亚洲人午夜精品免费| 亚洲综合999| 欧美成人自拍视频| 亚洲一区二区影院| 欧美成人精品1314www| 欧美视频官网| 亚洲高清激情| 欧美一级视频一区二区| 欧美不卡福利| 翔田千里一区二区| 欧美精品免费看| 精品盗摄一区二区三区| 在线视频你懂得一区| 久久精品国产综合精品| 亚洲毛片网站| 蜜桃久久av一区| 国产日韩综合| 亚洲欧美国产精品va在线观看| 久久综合伊人77777麻豆| 中文av字幕一区| 欧美国产视频在线| 在线观看日产精品| 欧美影院在线| 日韩香蕉视频| 久久精品成人| 国产精品乱人伦一区二区 | 一区二区三区产品免费精品久久75| 久久成人在线| 91久久午夜| 欧美中文字幕在线视频| 亚洲三级影院| 欧美精品日韩精品| 国产一区二区三区在线观看网站| 99国产精品久久久久久久| 欧美电影电视剧在线观看| 久久国产欧美日韩精品| 久久久欧美精品| 亚洲精品国产日韩| 女人香蕉久久**毛片精品| 亚洲一区精品在线| 国产精品qvod| 亚洲午夜精品一区二区| 亚洲国产精品悠悠久久琪琪| 久久久一本精品99久久精品66| 国产日韩欧美在线播放| 欧美综合国产| 久久福利视频导航| 国产一区二区剧情av在线| 久久精品国产99精品国产亚洲性色 | 午夜影视日本亚洲欧洲精品| aa级大片欧美| 国产精品你懂得| 亚洲精品在线免费| 亚洲精品一区二区网址| 国产精品国产福利国产秒拍| 性欧美激情精品| 久久精品水蜜桃av综合天堂| 在线观看日韩av电影| 亚洲级视频在线观看免费1级| 欧美va日韩va| 欧美久久一级| 欧美尤物巨大精品爽| 一区二区三区精密机械公司| 国产麻豆91精品| 免费在线观看一区二区| 欧美国产亚洲另类动漫| 亚洲视频在线观看网站| 亚洲小视频在线| 伊人久久大香线蕉av超碰演员| 欧美激情91| 一区二区三区高清在线观看| 久久精品人人做人人综合 | 一区二区三区免费观看| 亚洲欧美日韩在线一区| 亚洲美女在线视频| 久久这里只有精品视频首页| 久久精彩视频| 一本色道久久加勒比88综合| 美国三级日本三级久久99| 久久精品一二三| 欲色影视综合吧| 亚洲精品久久久久久久久久久| 欧美日韩中文| 久久视频在线视频| 欧美视频日韩视频| 久久这里有精品15一区二区三区| 欧美国产欧美亚洲国产日韩mv天天看完整| 亚洲一线二线三线久久久| 久久久综合激的五月天| 亚洲午夜免费福利视频| 蜜桃久久精品乱码一区二区| 欧美一级久久久| 欧美日韩中文| 亚洲精品一区二区在线| 亚洲国产成人精品视频| 香蕉久久一区二区不卡无毒影院 | 欧美精品午夜| 欧美www视频| 国内精品视频一区| 亚洲一区二区三区影院| 99国产精品国产精品久久| 久久亚洲精品视频| 久久久国产精彩视频美女艺术照福利| 欧美日韩免费一区二区三区| 欧美96在线丨欧| 国内揄拍国内精品少妇国语| 99热这里只有成人精品国产| 亚洲免费观看| 欧美护士18xxxxhd| 欧美成人午夜免费视在线看片| 国产一区二区在线观看免费播放| 亚洲一区在线看| 亚洲欧美精品中文字幕在线| 欧美三级电影精品| 亚洲人屁股眼子交8| 91久久综合亚洲鲁鲁五月天| 日韩一二三区视频| 亚洲国产成人久久| 一区二区三区在线免费观看| 亚洲女同同性videoxma| 日韩亚洲欧美一区| 亚洲视频在线观看| 国产自产在线视频一区| 国产精品高潮视频| 欧美大片免费观看| 这里是久久伊人| 久久色在线观看| 欧美日韩成人在线观看| 亚洲日本免费电影| 99视频在线精品国自产拍免费观看| 欧美人成免费网站| 亚洲四色影视在线观看| 中文欧美在线视频| 国产九色精品成人porny| 午夜精品美女自拍福到在线| 久久久久国产精品一区三寸| 亚洲成在线观看| 欧美破处大片在线视频| 一本色道久久| 蜜桃视频一区| 亚洲午夜精品福利| 国产一区二区三区丝袜| 久久中文在线| 一本色道久久综合狠狠躁的推荐| 欧美一级在线播放| 一区二区视频在线观看| 欧美激情在线免费观看| 亚洲午夜电影| 免费成人在线观看视频| 亚洲看片网站| 国产深夜精品| 裸体女人亚洲精品一区| 99精品视频免费观看| 久久久久久久精| 一二三区精品| 精品91视频| 国产毛片一区二区| 欧美大胆a视频| 欧美在线免费观看| 中文久久乱码一区二区| 亚洲第一页自拍| 久久精品夜夜夜夜久久| 99国产精品99久久久久久粉嫩| 黑人操亚洲美女惩罚| 国产精品久久久久久久app| 免费精品视频| 欧美成人一品| 国产一区二区高清| 久久精品国产99国产精品澳门| 欧美高清在线观看| 欧美大片免费久久精品三p| 国产真实久久|