js資料結構--字典(map)

遨翔在知識的海洋裡發表於2019-01-17

字典map

js資料結構--字典(map)

map.js

var Dictionary = function() {
    var items = {};
    // 檢查鍵
    this.has = function(key) {
            return key in items;
        }
        // 新增鍵值對
    this.set = function(key, value) {
            items[key] = value;
        }
        //通過鍵移除元素
    this.delete = function(key) {
            if (this.has(key)) {
                delete items[key];
                return true;
            }
            return false;
        }
        // 由鍵獲取值
    this.get = function(key) {
            return this.has(key) ? items[key] : undefined;
        }
        //以列表返回字典值
    this.values = function() {
            var values = [];
            for (var k in items) {
                if (this.has(k)) {
                    values.push(items[k])
                }
            }
            return values;
        }
        //獲取全部鍵名
    this.keys = function() {
            return Object.keys(items);
        }
        //獲取私有變數items
    this.getItems = function() {
        return items;
    }
}
複製程式碼

例項化

js資料結構--字典(map)

雜湊表(雜湊表)

js資料結構--字典(map)

charCodeAt()

把字元傳為數字

js資料結構--字典(map)

實現雜湊表

hash.js

var HashTable = function() {
    var items = [];
    //雜湊函式
    var loseloseHashCode = function(key) {
        var hash = 0;
        for (var i = 0; i < key.length; i++) {
            hash += key[i].charCodeAt();
        }
        return hash % 37
    }

    this.put = function(key, value) {
        var position = loseloseHashCode(key)
        console.log(position + '-' + value);
        items[position] = value;
    }

    this.remove = function(key) {
        items[loseloseHashCode(key)] = undefined
    }

    this.get = function(key){
        return items[loseloseHashCode(key)]
    }
    this.getItems = function() {
        return items;
    }
}
複製程式碼

例項化

js資料結構--字典(map)

雜湊表衝突解決方案:分離連結法

hash2.js

var LikedList = function() {

    //連結串列頭
    var head = null
        // 連結串列長度
    var length = 0

    // 輔助類:節點
    var Node = function(element) {
        this.element = element
        this.next = null
    }

    //連結串列尾新增元素
    this.append = function(element) {
        var node = new Node(element)
            //node={
            //   element : element
            //   next : null
            // }

        if (head == null) {
            head = node
        } else {
            var current = head
            while (current.next) {
                current = current.next
            }
            // while迴圈執行完之後 ,current已經是連結串列最後一項了
            current.next = node
        }
        length++
    }

    //連結串列某一個位置新增元素
    this.insert = function(position, element) {
            // 越界
            if (position > -1 && position < length) {
                var node = new Node(element)
                if (position == 0) {
                    var current = head
                    head = node
                    head.next = current
                } else {
                    var index = 0
                    var current = head
                    var previous = null
                    while (index < position) {
                        previous = current
                        current = current.next
                        index++
                    }

                    previous.next = node
                    node.next = current

                }
                length++
            }
        }
        // splice(1,10)
        // 沒有移除 ??? 拿出來用一下
    this.removeAt = function(position) {
        if (position > -1 && position < length) {
            if (position == 0) {
                var current = head
                head = current.next
            } else {
                var current = head
                var previous = null
                var index = 0
                while (index < position) {
                    previous = current
                    current = current.next
                    index++
                }
                // 跳出迴圈的時候 index == position
                previous.next = current.next
            }

            length--
            return current
        }
        return null
    }

    this.indexOf = function(element) {
        var current = head
        var index = 0
        while (current) {
            if (current.element == element) {
                return index
            }
            current = current.next
            index++
        }
        return -1
    }

    //removeAt(position)   刪除某個位置的元素
    //indexOf(element)     獲取某個元素的位置
    this.remove = function(element) {
        // length --
        return this.removeAt(this.indexOf(element))
    }

    this.isEmpty = function() {
        return length == 0
    }
    this.size = function() {
        return length
    }

    this.getHead = function() {
        return head
    }
}

//分離連結
var HashTable_L = function() {
    var table = []
        // 雜湊函式 key 》 雜湊值 》重複率太高
    var loseloseHashCode = function(key) { //Jobs
        var hash = 0
        for (var i = 0; i < key.length; i++) {
            hash += key[i].charCodeAt()
        }
        return hash % 37
    }
    var Node = function(key, value) {
        this.key = key
        this.value = value
    }
    this.put = function(key, value) {
        var position = loseloseHashCode(key)
        if (table[position]) {
            table[position].append(new Node(key, value))
        } else {
            var l = new LikedList
            table[position] = l
            table[position].append(new Node(key, value))
        }
    }
    this.get = function(key) {
        var position = loseloseHashCode(key)
        if (table[position]) {
            // 連結串列線性查詢
            var current = table[position].getHead()
            while (current) {
                if (current.element.key == key) {
                    return current.element.value
                }
                current = current.next
            }

        } else {
            return undefined
        }
    }
    this.remove = function(key) {
        var position = loseloseHashCode(key)
        if (table[position]) {
            // 刪除
            // remove(element)
            var current = table[position].getHead()
            while (current) {
                if (current.element.key == key) {
                    // 查詢到對應的key了
                    table[position].remove(current.element /*Node*/ )
                    if (table[position].isEmpty()) {
                        table[position] = undefined
                    }
                    return true
                }
                current = current.next
            }
        } else {
            return false
        }
    }
    this.getTable = function() {
        return table
    }
}
複製程式碼

例項化

js資料結構--字典(map)

雜湊表衝突解決方案:線性探查法

    this.put = function(key, value) {
        var position = loseloseHashCode(key)
        if (table[position] == undefined) {
            table[position] = new Node(key, value)
        } else {
            var index = position + 1;
            while (table[index] !== undefined) {
                index++
            }
            table[index] = new Node(key, value)
        }
    }
複製程式碼

js資料結構--字典(map)

雜湊表衝突解決方案: 利用數學方法生成不重複的雜湊函式

hash4.js

var HashTable = function() {
    var items = [];
    //不重雜湊函式
    var djb2HashCode = function(key) {
        var hash = 5381
        for (var i = 0; i < key.length; i++) {
            hash = hash * 33 + key[i].charCodeAt()
        }
        return hash % 1013
    }

    this.put = function(key, value) {
        var position = djb2HashCode(key)
        console.log(position + '-' + value);
        items[position] = value;
    }

    this.remove = function(key) {
        items[djb2HashCode(key)] = undefined
    }

    this.get = function(key) {
        return items[djb2HashCode(key)]
    }
    this.getItems = function() {
        return items;
    }
}
複製程式碼

例項化

js資料結構--字典(map)

相關文章