- #include <iostream>
- using namespace std;
- #define MAXSIZE 11
- #define NULLKEY -1
- #define DELKEY -2
- typedef int ElemType;
- typedef struct
- {
- ElemType key;
- int count; //探查次数
- }HashTable[MAXSIZE];
- int SearchHT(HashTable ha, ElemType key)
- {
- int adr = 0 ;
- adr = key % MAXSIZE;
- while(ha[adr].key != NULLKEY && ha[adr].key != key)
- { adr = (adr + 1) % MAXSIZE;} //此处应该对数组的最大长度进行求余
- if(ha[adr].key == key) return adr;
- else return -1;
- }
- int DeleteHT(HashTable ha,ElemType key, int &num)
- {
- int adr = 0;
- adr = SearchHT(ha,key);
- if(adr == -1) return -1;
- else { ha[adr].key = DELKEY; ha[adr].count = 0;--num;}
- return 1;
- }
- void DispHT(HashTable ha);
- void InsertHT(HashTable ha, ElemType key, int &num)
- {
- int adr = 0, count = 1;
- adr = key % MAXSIZE;
- while(ha[adr].key != NULLKEY && ha[adr].key != DELKEY)
- {
- ++count;
- adr = (adr + 1) % MAXSIZE;
- if(count > MAXSIZE) { cout<<"the table is full!!!"<<endl; return ;}
- }
- if(ha[adr].key == NULLKEY || ha[adr].key == DELKEY)
- { ha[adr].key = key;
- ha[adr].count = count;
- ++num;
- }
- }
- void CreateHT(HashTable ha, ElemType keys[], int len, int &num)
- {
- int loop1 = 0;
- for(loop1 = 0; loop1 < MAXSIZE ; ++loop1)
- {
- ha[loop1].key = NULLKEY;
- ha[loop1].count = 0;
- }
- for(loop1 = 0; loop1 < len ; ++loop1)
- InsertHT(ha,keys[loop1],num);
- }
- void DispHT(HashTable ha)
- {
- int loop1 = 0;
- for (loop1 = 0; loop1 < MAXSIZE; ++loop1)
- printf("%5d",loop1);
- printf("\n");
- for(loop1 = 0;loop1 < MAXSIZE ; ++loop1)
- {
- if(ha[loop1].key != NULLKEY && ha[loop1].key != DELKEY)
- printf("%5d",ha[loop1].key);
- else printf(" ");
- }
- printf("\n");
- for(loop1 = 0; loop1 < MAXSIZE; ++loop1)
- {
- if(ha[loop1].count != 0)
- printf("%5d",ha[loop1].count);
- else printf(" ");
- }
- printf("\n");
- }
- void CompASL(HashTable ha, int num)
- {
- int loop1 = 0, total = 0;
- for(loop1 = 0; loop1 < MAXSIZE; ++loop1)
- total += ha[loop1].count;
- cout<<"average find length is :"<<(total * 1.0 / num)<<endl;
- }
- void CompUNASL(HashTable ha)
- {
- int total = 0, loop1 = 0, loop2 = 0;
- for(loop1 = 0; loop1 < MAXSIZE; ++loop1)
- {
- loop2 = loop1;
- while(ha[loop2].count != 0)
- {
- ++total;
- loop2 = (loop2 + 1) % MAXSIZE;
- }
- ++total;
- }
- cout<<"the average unfind length is :"<<(total * 1.0 / MAXSIZE)<<endl;
- }
- int SearchHT2(HashTable ha, ElemType key)
- {
- int flag = 1, var = 1, adr = 0, adr1 = 0;
- adr1 = adr = key % MAXSIZE;
- while(ha[adr].key != key && ha[adr].key != NULLKEY)
- {
- adr = (adr1 + var * var * flag) % MAXSIZE; //采用开放定址法,H(key) = ( H(k) + d ) % MAXSIZE
- if(adr >= MAXSIZE || adr < 0)
- return -1;
- if(flag == 1)
- flag = -1;
- else
- {
- ++var;
- flag = 1;
- }
- }
- if(ha[adr].key == key)
- return adr;
- else return -1;
- }
- int DeleteHT2(HashTable ha, int key, int &num )
- {
- int adr = 0;
- adr = SearchHT2(ha,key);
- if(adr != -1)
- { ha[adr].key = DELKEY; ha[adr].count = 0; --num; return 1;}
- else return -1;
- }
- void InsertHT2(HashTable ha, int key, int &num)
- {
- int adr = 0,adr1 = 0, flag = 1, var = 1, count = 1;
- adr = SearchHT2(ha,key);
- if(adr != -1) { cout<<"The node is already exitence "<<endl; return;}
- adr1 = adr = key % MAXSIZE;
- while(ha[adr].key != NULLKEY && ha[adr].key != DELKEY)
- { ++count;
- adr = (adr1 + var * var * flag) % MAXSIZE;
- if(adr >= MAXSIZE || adr < 0) { cout<<"fail to insert , not so much space !!! "<<endl; return ;}
- if(flag == 1)
- flag = -1;
- else
- {
- ++var;
- flag = 1;
- }
- }
- ++num;
- ha[adr].key = key;
- ha[adr].count = count;
- }
- void CreateHT2(HashTable ha, ElemType keys[], int len, int &num)
- {
- int loop1 = 0;
- for (loop1 = 0; loop1 < MAXSIZE ; ++loop1)
- {
- ha[loop1].count = 0;
- ha[loop1].key = NULLKEY;
- }
- for (loop1 = 0; loop1 < len ; ++loop1)
- InsertHT2(ha,keys[loop1],num);
- }
- void CompASL2(HashTable ha, int num)
- {
- int count = 0;
- int total = 0, loop1 = 0;
- for(loop1 = 0; loop1 < MAXSIZE; ++loop1)
- {
- total += ha[loop1].count;
- if(ha[loop1].count != 0)
- ++count;
- }
- if(count != num) { cout<<"Fail to asl"<<endl; return; }
- cout<<"the find average length is : "<<total * 1.0 / num<<endl;
- }
- typedef struct node1
- {
- ElemType key;
- struct node1 *next;
- }SNode1;
- typedef struct
- {
- struct node1 *next;
- }HNode;
- int SearchHT3(HNode ha[], ElemType key)
- {
- int adr = 0;
- SNode1 *temp = NULL;
- adr = key % MAXSIZE;
- if(ha[adr].next == NULL) return -1;
- else
- {
- temp = ha[adr].next;
- while(temp != NULL && temp->key != key)
- temp = temp->next;
- if(temp == NULL) return -1;
- else if(temp->key == key) return adr;
- }
- }
- int DeleteHT3(HNode ha[], int key)
- {
- int adr = 0;
- SNode1 *parent = NULL, *desc = NULL;
- adr = SearchHT3(ha,key);
- if(adr == -1 ) {cout<<"no the element want to delete "<<endl; return -1;}
- else
- { parent = ha[adr].next;
- desc = ha[adr].next;
- while(desc != NULL && desc->key != key)
- {
- parent = desc;
- desc = desc->next;
- }
- if(desc != NULL && desc->key == key)
- {
- parent->next = desc->next;
- free(desc);
- return 1;
- }
- return -1;
- }
- }
- void InsertHT3(HNode ha[], ElemType key)
- {
- int adr = 0;
- SNode1 *temp = NULL, *temp2 = NULL;
- adr = key % MAXSIZE;
- temp = (SNode1 *)malloc(sizeof(SNode1));
- temp->key = key;
- temp->next = NULL;
- temp2 = ha[adr].next;
- if(temp2 == NULL)
- {ha[adr].next = temp; return ;}
- else
- {
- while(temp2->next != NULL)
- temp2 = temp2->next;
- temp2->next = temp;
- }
- }
- void CreateHT3(HNode ha[], ElemType keys[], int len)
- {
- int loop1 = 0;
- for(loop1 = 0; loop1 < MAXSIZE; ++loop1)
- ha[loop1].next = NULL;
- for(loop1 =0 ; loop1 < len ; ++loop1)
- InsertHT3(ha,keys[loop1]);
- }
- void Disp(HNode ha[])
- {
- int loop1 = 0;
- SNode1 *temp = NULL;
- for(loop1 =0; loop1 < MAXSIZE; ++loop1)
- {
- if(ha[loop1].next != NULL)
- {
- temp = ha[loop1].next;
- while(temp != NULL)
- {
- cout<<temp->key<<' ';
- temp = temp->next;
- }
- cout<<endl;
- }
- }
- }
- void CompASL3(HNode ha[])
- {
- int num = 0,total = 0,loop1 = 0,var = 0;
- SNode1 *temp = NULL;
- for(loop1 = 0; loop1 < MAXSIZE; ++loop1)
- {
- var = 0;
- if(ha[loop1].next != NULL)
- { ++num;
- temp = ha[loop1].next;
- while(temp != NULL)
- {
- ++var;
- total += var;
- temp = temp->next;
- }
- }
- }
- cout<<"num is "<<num<<" the find average length is "<<total * 1.0 / num<<endl;
- }
- void CompUNASL3(HNode ha[])
- {
- int total = 0,loop1 = 0,var = 1;
- SNode1 *temp = NULL;
- for(loop1 = 0; loop1 < MAXSIZE; ++loop1)
- {
- var = 1;
- if(ha[loop1].next != NULL)
- {
- temp = ha[loop1].next;
- while(temp != NULL)
- {
- ++var;
- temp = temp->next;
- }
- }
- total += var;
- }
- cout<<"the find average length is "<<total * 1.0 / MAXSIZE<<endl;
- }
- #include <iostream>
- using namespace std;
- int BinSearch(int iarr[], int len, int key)
- {
- int left = 0, right = len -1 , mid = 0;
- while(left <= right)
- {// cout<<"left is "<<iarr[left]<<" rigth is "<<iarr[right]<<endl;
- mid = (left + right ) / 2;
- if(iarr[mid] == key) return (mid + 1);
- else if(iarr[mid] > key)
- {
- right = mid -1;
- }else
- {
- left = mid + 1;
- }
- }
- return -1;
- }
- typedef int ElemType;
- typedef struct node
- {
- ElemType data;
- struct node *lchild, *rchild;
- }BSTNode;
- BSTNode *BSTSearch(BSTNode *bt, int key)
- {
- BSTNode *temp = NULL;
- if(bt == NULL) return NULL;
- else if(bt->data == key) return bt;
- else if(bt->data > key)
- {
- return BSTSearch(bt->lchild,key);
- }else
- {
- return BSTSearch(bt->rchild,key);
- }
- }
- int BSTInsert(BSTNode *&tree, int key)
- {
- BSTNode *temp = NULL;
- if(tree == NULL)
- {
- temp = (BSTNode *)malloc(sizeof(BSTNode));
- temp->data = key;
- temp->lchild = NULL;
- temp->rchild = NULL;
- tree = temp; return 1;
- }
- else if(tree->data > key)
- {
- return BSTInsert(tree->lchild,key);
- }else if(tree->data < key)
- { return BSTInsert(tree->rchild, key) ;}
- else { return 0; }
- }
- void CreateBST(BSTNode *&tree, int iarr[], int len)
- {
- int loop1 = 0;
- for(loop1 = 0; loop1 < len ; ++loop1)
- BSTInsert(tree,iarr[loop1]);
- }
- int BSTDelete(BSTNode *&tree, int key) //删除排序树中的节点
- {
- BSTNode *parent = NULL, *desc = NULL, *parent2 = NULL,*temp = NULL;
- parent = tree; desc = tree;
- while(desc != NULL && desc->data != key) //找到要删除的节点
- { parent = desc;
- if(desc->data > key)
- desc = desc->lchild;
- else if(desc->data < key)
- desc = desc->rchild;
- }
- if(desc == NULL) //表示没有要找的节点
- return 0;
- else if(desc->lchild == NULL && desc->rchild == NULL) //找到的节点是叶子节点,即没有左右子树
- {
- if(parent->lchild == desc) //直接把它释放即可
- parent->lchild = NULL;
- else if(parent->rchild == desc)
- parent->rchild = NULL;
- free(desc);
- return 1;
- }else if(desc->lchild != NULL && desc->rchild == NULL) //此处表示找到的没有右节点只有左节点,把此节点的左节点接到其双亲节点的左节点上即可
- { //此处不用考虑其是不是双亲节点的右节点, 因为其有左节点,无右节点证明,它就是双亲节点的左节点, 因为这是一颗排序树
- parent->lchild = desc->lchild;
- free(desc);
- return 1;
- }else if(desc->lchild == NULL && desc->rchild != NULL)
- {
- parent->rchild = desc->rchild;
- free(desc);
- return 1;
- }else //此处表示其左右节点都存在,处理这种情况,两种方法,1 :把其左子树的最右下节点代替它, 2 :把其右子树最左下节点代替它。 此处用的是第一种方法
- {
- parent2 = desc;
- temp = desc->lchild;
- while(temp != NULL && temp->rchild != NULL)
- {
- parent2 = temp;
- temp = temp->rchild;
- }
- if(parent2->lchild == temp) //此处表示上面的while循环没有进行, 因为按理说temp不应该为左节点
- parent2->lchild = temp->lchild;
- else if(parent2->rchild == temp)
- parent2->rchild = temp->lchild;
- temp->rchild = desc->rchild; //用于代替
- temp->lchild = desc->lchild;
- if(parent == desc) //此处表明找到是根节点的情况
- tree = temp;
- else if(parent->lchild == desc)
- parent->lchild = temp;
- else
- parent->rchild = temp;
- free(desc);
- }
- return 1;
- }
- void BSTDisp(BSTNode *tree)
- {
- if(tree != NULL)
- {
- BSTDisp(tree->lchild);
- cout<<tree->data<<' ';
- BSTDisp(tree->rchild);
- }
- }
- int BSTInsert2(BSTNode *&tree, int key) //此函数是插入节点的非递归算法,关键是用到了非递归找到的节点的思想(while循环)
- { //知道插入的节点一定是作为一个叶子节点插入树中的
- BSTNode *parent = NULL,*desc = NULL, *temp = NULL;
- if(tree != NULL)
- {
- desc = tree;
- while(desc != NULL)
- {
- if(desc->data == key) return 0;
- else if(desc->data > key)
- {
- parent = desc;
- desc = desc->lchild;
- }else
- {
- parent = desc;
- desc = desc->rchild;
- }
- }
- temp = (BSTNode *)malloc(sizeof(BSTNode));
- temp->lchild = temp->rchild = NULL;
- temp->data = key;
- if(parent->data > key)
- parent->lchild = temp;
- else parent->rchild = temp;
- }else return 0;
- return 1;
- }
- int Height(BSTNode *tree) //计算根节点的高度
- {
- int height1 = 0, height2 = 0;
- if(tree == NULL) return 0;
- else if (tree->lchild == NULL && tree->rchild == NULL) return 1;
- else
- {
- height1 = Height(tree->lchild);
- height2 = Height(tree->rchild);
- return height1 > height2 ? height1 + 1 :height2 + 1; //此处加一是为了计算其本身
- }
- }
- void Compbf(BSTNode *tree) //此函数通过递归对每个节点进行判断深度
- {
- int lheight = 0, rheight = 0;
- if(tree != NULL)
- {
- lheight = Height(tree->lchild);
- rheight = Height(tree->rchild);
- cout<<tree->data<<" 's bf is "<<lheight - rheight<<endl;
- Compbf(tree->lchild);
- Compbf(tree->rchild);
- }
- }
- void JudgeAVL(BSTNode *tree, int &balance, int &h)
- { int bl,br,hl,hr;
- if(tree == NULL) { balance = 1; h = 0; }
- else if(tree->lchild == NULL && tree->rchild == NULL) { balance = 1; h = 1;}
- else
- {
- JudgeAVL(tree->lchild,bl,hl);
- JudgeAVL(tree->rchild,br,hr);
- h = (hl > hr ? hl : hr) + 1;
- if(abs(hl - hr) < 2)
- balance = bl & br;
- else
- balance = 0;
- }
- }
- typedef struct tnode1
- {
- char ch;
- int count;
- struct tnode1 *lchild, *rchild;
- }TNode;
- void CreateTree(TNode *&tree, char c[])
- {
- int loop1 = 0; char ch = c[loop1];
- TNode *temp = NULL , *parent = NULL, *desc = NULL;
- while(ch != '\0')
- {
- temp = (TNode *)malloc(sizeof(TNode));
- temp->ch = ch; temp->count = 0; temp->lchild = NULL; temp->rchild = NULL;
- if(tree == NULL)
- { tree = temp; }
- else
- {
- parent = NULL; desc = tree;
- while(desc != NULL && desc->ch != ch) //此处一定要先判断desc是否为空
- {
- if(desc->ch > ch) { parent = desc; desc = desc->lchild;}
- else { parent = desc; desc = desc->rchild; }
- }
- if(desc == NULL && parent != NULL)
- {
- if(parent->ch > ch) parent->lchild = temp;
- else parent->rchild = temp;
- }
- else if(desc->ch == ch) { ++desc->count; }
- }
- ch = c[++loop1];
- }
- }
- void Disp( TNode *tree)
- {
- if(tree != NULL)
- {
- Disp(tree->lchild);
- cout<<tree->ch<<' '<<tree->count<<endl;
- Disp(tree->rchild);
- }
- }
- #include "find.h"
- #include "hash.h"
- void main()
- {
- int iarr[10] = {56,67,78,89,98,100,11,23,31,45};
- char carr[11] = {"dertdacvbs"};
- int num = 0;
- HashTable ha;
- HNode ha2[MAXSIZE];
- // BSTNode *tree = NULL;
- // TNode *ttree = NULL;
- // cout<<"place is : "<<BinSearch(iarr,10,31)<<endl;
- // CreateBST(tree,iarr,10);
- // BSTDisp(tree); cout<<endl;
- // BSTInsert2(tree,65);
- // BSTDisp(tree);cout<<endl;
- // cout<<"root->data "<<tree->data<<endl;
- // cout<<BSTSearch(tree,67)->data<<endl;
- // BSTDelete(tree,45);
- // Compbf(tree);
- // BSTDisp(tree);cout<<endl;
- // CreateTree(ttree,carr);
- // Disp(ttree);
- // CreateHT(ha,iarr,10,num);
- // DispHT(ha);
- /* DeleteHT( ha,56, num) ;
- DispHT(ha);
- DeleteHT( ha,100, num) ;
- DispHT(ha);
- InsertHT(ha, 108,num);
- DispHT(ha); */
- // CompASL(ha,num);
- // CompUNASL(ha);
- /*
- CreateHT2(ha,iarr,10,num);
- DispHT(ha); cout<<"num is "<<num<<endl;
- InsertHT2(ha,14,num);
- DispHT(ha); cout<<"num is "<<num<<endl;
- DeleteHT2(ha,78,num);
- DispHT(ha); cout<<"num is "<<num<<endl;
- CompASL2(ha,num);
- */
- CreateHT3(ha2,iarr,10);
- Disp(ha2); cout<<endl;
- InsertHT3(ha2,16);
- Disp(ha2); cout<<endl;
- DeleteHT3(ha2,100);
- Disp(ha2); cout<<endl;
- CompASL3(ha2);
- CompUNASL3(ha2);
- }
转载于:https://blog.51cto.com/saibro/1183653