1、二叉排序树。 任意给定一组数据,设计一个算法,建立一棵二叉排序树,对它进行查找、插入、删除等操作。
2、构造哈希表,key值由随机数获得,自己选择解决冲突的算法。并且计算查找次数及平均查找次数。
二叉排序数
#include<iostream> #include<string> using namespace std; #define OK 1 #define OVERFLOW -1 #define ERROR 0 #define MAXSIZE 100 int typedef Status; typedef struct{ int key; int otherinfo; }ElemType; typedef struct BSTNode{//二叉排序树定义 ElemType data; struct BSTNode *lchild, *rchild; }BSTNode,*BSTree; BSTree SearchBST(BSTree T, int key){//查找 if ((!T) || key == T->data.key) return T; else if (key < T->data.key)return SearchBST(T->lchild, key); else return SearchBST(T->rchild,key); } void InsertBST(BSTree &T, ElemType e){//插入 BSTree S; if (!T){ S = new BSTNode; S->data = e; S->lchild = S->rchild = NULL; T = S; } else if (e.key < T->data.key){ InsertBST(T->lchild, e); } else if (e.key>T->data.key){ InsertBST(T->rchild, e); } } void CreatBST(BSTree &T){//创建 cout << "请输入结束标志(int)" << endl; int m; cin >> m; T = NULL; cout << "请输入创建排序树" << endl; ElemType e; cin >>e.key; while (e.key != m){ InsertBST(T, e); cin >> e.key; } } void DeleteBST(BSTree &T, int key)//删除 { BSTree p,f,q,s; p = T; f = NULL; while (p){ if (p->data.key == key)break; f = p; if (p->data.key > key)p = p->lchild; else p = p->rchild; } if (!p)return; q = p; if ((p->lchild) && (p->rchild)){ s = p->lchild; while (s->rchild){ q = s; s = s->rchild; } p->data = s->data; if (q != p)q->rchild = s->lchild; else q->lchild = s->lchild; delete s; return; } else if (!p->rchild){ p = p->lchild; } else if (!p->lchild){ p = p->rchild; } if (!f)T = p; else if (q == f->lchild)f->lchild = p; else f->rchild = p; delete q; } int main(){ BSTree T,N; ElemType e; e.key = 0; e.otherinfo = 0; T = new BSTNode; N = new BSTNode; //T->data = e; //T->lchild = NULL; //T->rchild = NULL; int n; while(1){ cout << "1,创建" << endl; cout << "2,查找" << endl; cout << "3,插入" << endl; cout << "4,删除" << endl; cout << "0,退出" << endl; cin >> n; if (n == 1)CreatBST(T); if (n == 2){ cout << "请输入想要查找的key值" << endl; int key; cin >> key; N=SearchBST(T, key); cout << N->data.key << endl; } if (n == 3){ InsertBST(T, e); } if (n == 4){ cout << "请输入想要删除的key值" << endl; int key; cin >> key; DeleteBST(T, key); } if (n == 0)break; } }
哈希表
#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 << "查找次数 " <<j<< endl; cout << "平均查找次数 " << (j / 20) << endl; /*key=0; cout << "请输入要查找的数" << endl; cin >> key; SearchHash(HT, key);*/ }