【演算法導論】十一章散列表11.1-4大數組實現直接定址方式的字典操作,導論11.1-4

來源:互聯網
上載者:User

【演算法導論】十一章散列表11.1-4大數組實現直接定址方式的字典操作,導論11.1-4
11.1-4題目:


        我們希望在一個非常大的數組上,通過利用直接定址的方式來實現一個字典。開始時,該數組中可能包含一些無用資訊,但要對整個數組進行初始化是不太實際的,因為該數組的規模太大。請給出在大數組上實現直接定址字典的方式。每個儲存物件佔用O(1)空間;SEARCH、INSEART、DELETE操作的時間均為O(1);並且對資料結構初始化的時間為O(1)。(提示:可以利用一個附加數組,處理方式類似於棧,其大小等於實際儲存在字典中的關鍵字數目,以協助確定大數組中某個給定的項是否有效)。



想法:


        由於大數組太大,不能初始化,我們也就等於不知道到底哪裡有真正的資料,於是乎資料不能儲存在大數組中,因為你根本不知道到底哪裡才是資料。


        這裡方式是:將資料存放區到棧上,棧上的增刪查都可以實現O(1),然後在大數組上,對應key的位置的元素,存放棧上對應的下標,這樣根據key到大數組中找到棧的下標,然後根據棧的下標又可以找到那個key值對應的資料元素了。


        然後,還需要解決如何判斷資料是否有效問題,這個也很簡單,經過上面的尋找過程,不難發現,如果該資料是有效,需要滿足以下幾個條件:


        1、key值對應到大數組中位置的值,必須位於[0,棧的棧頂位置]之間,否則肯定不是資料        2、滿足第1條之後,我們到棧上對應的位置,找到那個元素資料,它的key值要反過來等於我們原始的key值,否則表示這個資料也是不存在的。可以參見下面代碼中的isExist函數的寫法;



資料元素類型:_node.h


class node {public:int key;node(int key) :key(key) {}node() :key(-1) {}};



棧類,存放真實資料:_stack.h


#include <iostream>#include "_node.h"using namespace std;/* * 用於存放資料的棧,使用單數組實現,這裡的資料為node類型,裡麵包含一個key值 */class Stack {int size;public:int top;node* array;Stack(int size) :size(size), top(-1) {array = new node[size];}~Stack() {//做一些記憶體回收工作delete[] array;}//入棧void push(int* hash, int key) {if (top == size - 1) {cout << "error:stackoverflow" << endl;return;}top++;array[top].key = key;//讓hash數組中對應key位置的元素與棧上top位置元素掛鈎hash[key] = top;}//出棧int pop(int* hash, int key) {if (top == -1) {cout << "error:stackunderflow" << endl;return -1;}int tmp = array[top].key;top--;//更新hash表,讓其等於-1,因為棧數組下標不可能為-1,方便以後判斷hash[key] = -1;return tmp;}void travel() {if (top < 0) {return;}int tmp = top;while (tmp >= 0) {cout << array[tmp--].key << ' ';}cout << endl;}/* * 將pos位置的值與棧頂的值交換 */void swapTop(int* hash, int key) {int pos = (hash)[key];//更新散列表hash[array[top].key] = pos;hash[array[pos].key] = top;//交換操作,更新棧node tmp = array[top];array[top] = array[pos];array[pos] = tmp;}};



demo.cpp,包含hash類:


#include <iostream>#include "_stack.h"using namespace std;class Hash {public:int* hashArray; //用於存放棧中位置的數組,該數組下標對應於key值Stack* s; //存放真實資料的棧//構造Hash(int hashSize, int stackSize) :hashArray(), s() {hashArray = new int[hashSize];s = new Stack(stackSize);}//析構~Hash() {delete[] hashArray;delete s;}//判斷key值是否已經存在的函數bool isExist(int* hash, int key) {if (hash[key] <= s->top && hash[key] >= 0&& key == s->array[hash[key]].key) {return true;}cout << "key does not exist!" << endl;return false;}//插入一個資料void insert(int key) {s->push(this->hashArray, key);}//刪除一個資料void delete_(int key) {//判斷是否存在if (!isExist(this->hashArray, key)) {return;}//將對應的棧上的位置的資料與棧頂資料交換,同時重新整理hash數組中的值,使其指向正確的棧數組元素s->swapTop(this->hashArray, key);//出棧,同時重新整理hash數組中的值s->pop(this->hashArray, key);}//尋找是否已經包含key值node* search(int key) {if (!isExist(this->hashArray, key)) {return NULL;} else {return s->array + hashArray[key];}}//遍曆所包含的元素void travel() {int tmp = s->top;while (tmp >= 0) {cout << s->array[tmp--].key << ' ';}cout << endl;}};int main() {//測試使用hash數組大小為1000,存放資料的棧大小為100Hash* hash = new Hash(1000, 100);cout << hash->search(555) << endl;hash->insert(555);hash->insert(444);hash->insert(333);hash->travel();hash->delete_(555);hash->travel();cout << hash->search(333)->key << endl;return 0;}







聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.