一、 实验内容
1、二叉排序树。 任意给定一组数据,设计一个算法,建立一棵二叉排序树,对它进行查找、插入、删除等操作。
2、构造哈希表,key值由随机数获得,自己选择解决冲突的算法。并且计算查找次数及平均查找次数。
一、 源程序
二叉排序树:
1 #include<iostream> 2 #include<string> 3 using namespace std; 4 #define OK 1 5 #define OVERFLOW -1 6 #define ERROR 0 7 #define MAXSIZE 100 8 int typedef Status; 9 typedef struct{ 10 int key; 11 int otherinfo; 12 }ElemType; 13 typedef struct BSTNode{//二叉排序树定义 14 ElemType data; 15 struct BSTNode *lchild, *rchild; 16 17 }BSTNode,*BSTree; 18 BSTree SearchBST(BSTree T, int key){//查找 19 if ((!T) || key == T->data.key) 20 return T; 21 else if (key < T->data.key)return SearchBST(T->lchild, key); 22 else return SearchBST(T->rchild,key); 23 } 24 void InsertBST(BSTree &T, ElemType e){//插入 25 BSTree S; 26 if (!T){ 27 S = new BSTNode; 28 S->data = e; 29 S->lchild = S->rchild = NULL; 30 T = S; 31 } 32 else if (e.key < T->data.key){ 33 InsertBST(T->lchild, e); 34 } 35 else if (e.key>T->data.key){ 36 InsertBST(T->rchild, e); 37 } 38 } 39 void CreatBST(BSTree &T){//创建 40 cout << "请输入结束标志(int)" << endl; 41 int m; 42 cin >> m; 43 T = NULL; 44 cout << "请输入创建排序树" << endl; 45 ElemType e; 46 cin >>e.key; 47 while (e.key != m){ 48 InsertBST(T, e); 49 cin >> e.key; 50 } 51 } 52 void DeleteBST(BSTree &T, int key)//删除 53 { 54 BSTree p,f,q,s; 55 p = T; f = NULL; 56 while (p){ 57 if (p->data.key == key)break; 58 f = p; 59 if (p->data.key > key)p = p->lchild; 60 else p = p->rchild; 61 } 62 if (!p)return; 63 q = p; 64 if ((p->lchild) && (p->rchild)){ 65 s = p->lchild; 66 while (s->rchild){ 67 q = s; s = s->rchild; 68 } 69 p->data = s->data; 70 if (q != p)q->rchild = s->lchild; 71 else q->lchild = s->lchild; 72 delete s; 73 return; 74 } 75 else if (!p->rchild){ 76 p = p->lchild; 77 } 78 else if (!p->lchild){ 79 p = p->rchild; 80 } 81 if (!f)T = p; 82 else if (q == f->lchild)f->lchild = p; 83 else f->rchild = p; 84 delete q; 85 } 86 int main(){ 87 BSTree T,N; 88 ElemType e; 89 e.key = 0; 90 e.otherinfo = 0; 91 T = new BSTNode; 92 N = new BSTNode; 93 //T->data = e; 94 //T->lchild = NULL; 95 //T->rchild = NULL; 96 int n; 97 while(1){ 98 cout << "1,创建" << endl; 99 cout << "2,查找" << endl; 100 cout << "3,插入" << endl; 101 cout << "4,删除" << endl; 102 cout << "0,退出" << endl; 103 cin >> n; 104 if (n == 1)CreatBST(T); 105 if (n == 2){ 106 cout << "请输入想要查找的key值" << endl; 107 int key; 108 cin >> key; 109 N=SearchBST(T, key); 110 cout << N->data.key << endl; 111 } 112 if (n == 3){ 113 InsertBST(T, e); 114 } 115 if (n == 4){ 116 cout << "请输入想要删除的key值" << endl; 117 int key; 118 cin >> key; 119 DeleteBST(T, key); 120 } 121 if (n == 0)break; 122 } 123 }
哈希表:
#include<iostream> #include<string> #include<cstdlib> using namespace std; #define OK 1 #define OVERFLOW -1 #define ERROR 0 #define MAXSIZE 100 int typedef Status; #define m 20 typedef struct{//定义 int key; int data; }HashTable[m]; #define NULLKEY 0 int random(){ return rand() % ((m - 1) - 0 + 1) + 0; } int H(int key){ return random(); } int SearchHash(HashTable HT, int key){// int H0 = H(key); if (HT[H0].key == NULLKEY)return -1; else if (HT[H0].key == key)return H0; else{ for (int i = 1; i < m; i++){ int Hi = (H0 + i) % m; if (HT[Hi].key == NULLKEY)return -1; else if (HT[Hi].key == key)return Hi; } return -1; } } int main(){ HashTable HT; int key;int data; int j = 0; for (int i = 0; i < m; i++){ HT[i].key = 0; HT[i].data = 0; } for (int i = 0; i < m; i++) { cout << "请输入数据" << endl; cin >> data; key = 0; int H0 = H(key); cout <<"key:"<< H0<<endl; if (HT[H0].data == 0){ HT[H0].data = data; cout <<"在第"<< H0 <<"位置上"<< endl; j = j + 1; } else{ for (int i = 1; i < m; i++){ int Hi = (H0 + i) % m; if (HT[Hi].data == 0){ HT[Hi].data = data; cout << "在第a" << Hi << "位置上" << endl; j = j + 1; break; } } } } for (int i = 0; i < m; i++){ cout << HT[i].data << " "; } cout << "查找次数\n" <<j<< endl; cout << "平均查找次数\n" << (j / 20) << endl; /*key=0; cout << "请输入要查找的数" << endl; cin >> key; SearchHash(HT, key);*/ }