這是第五週的練習題,上週忘記發啦,這周是複習 Dictionary 和 HashTable。前端
下面是以前分享的連接:git
本週練習內容:數據結構與算法 —— Dictionary 和 HashTable算法
這些都是數據結構與算法,一部分方法是團隊其餘成員實現的,一部分我本身作的,有什麼其餘實現方法或錯誤,歡迎各位大佬指點,感謝。數組
字典是什麼?微信
字典和集合有什麼異同?數據結構
什麼是散列表和散列函數?app
散列表的特色是什麼?函數
解析:post
字典是一種以 鍵-值對 形式存儲數據的數據格式,其中鍵名用來查詢特定元素。
相同:都是用來存儲不一樣元素的數據格式;
區別:集合是以 值-值 的數據格式存儲,而字典是以 鍵-值 的數據格式存儲。
哈希表(Hash table
,也叫散列表),是根據關鍵碼值(·Key value·)而直接進行訪問的數據結構。也就是說,它經過把關鍵碼值映射到表中一個位置來訪問記錄,以加快查找的速度。這個映射函數叫作散列函數,存放記錄的數組叫作散列表。
特色:數組和連接優勢的結合,查詢速度很是的快,幾乎是O(1)的時間複雜度,而且插入和刪除也容易。
set(key,value)
:向字典中添加新元素。
delete(key)
:經過使用鍵值從字典中移除鍵值對應的值。
has(key)
:若是某個鍵值存在於這個字典中,則返回 true,不然返回 false。
get(key)
:使用鍵值查找對應的值並返回。
clear()
:刪除字典中的全部元素。
size()
:返回字典包含的元素數量,與數組的 length 屬性相似。
keys()
:將字典的全部鍵名以數組的形式返回。
values()
:將字典包含的全部數值以數組形式返回。
使用示例:
let dictionary = new Dictionary();
dictionary.set("Gandalf", "gandalf@email.com");
dictionary.set("John", "johnsnow@email.com");
dictionary.set("Tyrion", "tyrion@email.com");
console.log(dictionary.has("Gandalf"));
console.log(dictionary.size());
console.log(dictionary.keys());
console.log(dictionary.values());
console.log(dictionary.get("Tyrion"));
dictionary.delete("John");
console.log(dictionary.keys());
console.log(dictionary.values());
複製代碼
提示:Web 端優先使用 ES6 以上的語法實現。
解析:
// 2、請實現一個字典
class Dictionary {
constructor(){
this.items = []
}
/** * 向字典中添加新元素 * @param {*} key 添加的鍵名 * @param {*} value 添加的值 */
set (key, value) {
if ( !key ) return new Error('請指定插入的key')
this.items[key] = value
}
/** * 查詢某個鍵值存在於這個字典 * @param {*} key 查詢的鍵名 * @return {Boolean} 是否存在 */
has (key) {
return key in this.items
}
/** * 經過使用鍵值從字典中移除鍵值對應的值 * @param {*} key 移除的鍵名 * @return {Boolean} 是否移除成功 */
delete (key) {
if(!key || !this.has(key)) return false
delete this.items[key]
return true
}
/** * 使用鍵值查找對應的值並返回 * @param {*} key 查找的鍵名 * @return {*} 查找的結果 */
get (key) {
return this.has(key) ? this.items[key] : undefined
}
/** * 刪除字典中的全部元素 */
clear () {
this.items = {}
}
/** * 將字典的全部鍵名以數組的形式返回 * @return {Array} 全部鍵名的數組 */
keys () {
return Object.keys(this.items)
}
/** * 將字典的全部鍵值以數組的形式返回 * @return {Array} 全部鍵值的數組 */
values () {
let result = []
for(let k in this.items){
if(this.has[k]){
result.push(this.items[k])
}
}
return result
}
/** * 返回字典包含的元素數量 * @return {Number} 元素數量 */
size () {
const values = this.values()
return values.length
}
}
複製代碼
put(key,value)
:向散列表增長/更新一個新的項。
remove(key)
:根據鍵值從散列表中移除值。
get(key)
:根據鍵值檢索到特定的值。
print()
:打印散列表中已保存的值。
散列表內部的散列算法:
function hashCode(key) {
let hash = 0;
for (let i = 0; i < key.length; i++) {
hash += key.charCodeAt(i);
}
return hash % 37;
}
複製代碼
使用示例:
const hashTable = new HashTable();
hashTable.put("Gandalf", "gandalf@email.com");
hashTable.put("John", "johnsnow@email.com");
hashTable.put("Tyrion", "tyrion@email.com");
hashTable.print();
複製代碼
解析:
// 3、請實現一個散列表
class HashTable {
constructor(){
this.table = []
}
/** * 散列函數 * @param {*} key 鍵名 */
hashCode(key) {
let hash = 0;
for (let i = 0; i < key.length; i++) {
hash += key.charCodeAt(i);
}
return hash % 37;
}
/** * 向散列表增長/更新一個新的項 * @param {*} key 添加的鍵名 * @param {*} value 添加的值 */
put (key, value) {
let position = this.hashCode(key)
this.table[position] = value
}
/** * 根據鍵值從散列表中移除值 * @param {*} key 移除的鍵名 * @return {Boolean} 是否成功移除 */
remove (key) {
if ( !key ) return false
let position = this.hashCode(key)
this.table[position] = undefined
return true
}
/** * 根據鍵值檢索到特定的值 * @param {*} key 查找的鍵名 * @return {*} 查找的值 */
get (key) {
let position = this.hashCode(key)
return this.table[position]
}
/** * 打印散列表中已保存的值 * @return {*} 散列表的值 */
print () {
return this.table
}
}
複製代碼
分離連接是爲散列表的每個位置建立一個鏈表儲存元素的方式來處理散列表中的衝突:
請實現新的散列表方法:
put(key,value)
:將 key 和
value存在一個
ValuePair對象中(便可定義一個包含
key和
value屬性的
ValuePair` 類),並將其加入對應位置的鏈表中。
get(key)
:返回鍵值對應的值,沒有則返回 undefined
。
remove(key)
:從散列表中移除鍵值對應的元素。
print()
:打印散列表中已保存的值。
提示:先找到元素儲存位置所對應的鏈表,再找到對應的值。
const hashTable = new HashTable();
hashTable.put("Gandalf", "gandalf@email.com");
hashTable.put("Tyrion", "tyrion@email.com");
hashTable.put("Aaron", "aaron@email.com");
hashTable.put("Ana", "ana@email.com");
hashTable.put("Mindy", "mindy@email.com");
hashTable.put("Paul", "paul@email.com");
hashTable.print();
console.log(hashTable.get("Tyrion"));
console.log(hashTable.get("Aaron"));
hashTable.remove("Tyrion");
hashTable.print();
複製代碼
解析:
// 鏈表的實現代碼省略 能夠查看以前的代碼
let ValuePair = function (key, value){
this.key = key
this.value = value
this.toString = function(){
return `[${this.key} - ${this.value}]`
}
}
class HashTable {
constructor(){
this.table = []
}
/** * 散列函數 * @param {*} key 鍵名 */
hashCode(key) {
let hash = 0;
for (let i = 0; i < key.length; i++) {
hash += key.charCodeAt(i);
}
return hash % 37;
}
/** * 向散列表增長/更新一個新的項 * @param {*} key 添加的鍵名 * @param {*} value 添加的值 */
put (key, value) {
let position = this.hashCode(key)
if(this.table[position] == undefined){
this.table[position] = new LinkedList()
}
this.table[position].append(new ValuePair(key, value))
}
/** * 根據鍵值從散列表中移除值 * @param {*} key 移除的鍵名 * @return {Boolean} 是否成功移除 */
remove (key) {
let position = this.hashCode(key)
if ( !key || this.table[position] === undefined ) return false
let current = this.table[position].getHead()
while(current.next){
if(current.element.key === key){
this.table[position].remove(current.element)
if(this.table[position].isEmpty){
this.table[position] = undefined
}
return true
}
current = current.next
}
}
/** * 根據鍵值檢索到特定的值 * @param {*} key 查找的鍵名 * @return {*} 查找的值 */
get (key) {
let position = this.hashCode(key)
if(!key || this.table[position] === undefined) return undefined
let current = this.table[position].getHead()
while(current.next()){
if(current.element.key === key){
return current.element.value
}
current = current.next
}
}
/** * 打印散列表中已保存的值 * @return {*} 散列表的值 */
print () {
return this.table
}
}
複製代碼
線性探查是解決散列表中衝突的另外一種方法,當向表中某一個位置加入新元素的時候,若是索引爲 index
的位置已經被佔據了,就嘗試 index+1
的位置。若是 index+1
的位置也被佔據,就嘗試 index+2
,以此類推。
請實現散列表:
put(key,value)
:將 key
和 value
存在一個 ValuePair
對象中(便可定義一個包含 key
和 value
屬性的 ValuePair
類)並分配到散列表。
get(key)
:返回鍵值對應的值,沒有則返回 undefined
。
remove(key)
:從散列表中移除鍵值對應的元素。
提示:移除一個元素,只須要將其賦值爲 undefined。
使用示例:
const hashTable = new HashTable();
hashTable.put("Gandalf", "gandalf@email.com");
hashTable.put("Tyrion", "tyrion@email.com");
hashTable.put("Aaron", "aaron@email.com");
hashTable.put("Ana", "ana@email.com");
hashTable.put("Mindy", "mindy@email.com");
hashTable.put("Paul", "paul@email.com");
hashTable.print();
console.log(hashTable.get("Tyrion"));
console.log(hashTable.get("Aaron"));
hashTable.remove("Tyrion");
hashTable.print();
複製代碼
解析:
let ValuePair = function (key, value){
this.key = key
this.value = value
this.toString = function(){
return `[${this.key} - ${this.value}]`
}
}
class HashTable {
constructor(){
this.table = []
}
/** * 散列函數 * @param {*} key 鍵名 */
hashCode(key) {
let hash = 0;
for (let i = 0; i < key.length; i++) {
hash += key.charCodeAt(i);
}
return hash % 37;
}
/** * 向散列表增長/更新一個新的項 * @param {*} key 添加的鍵名 * @param {*} value 添加的值 */
put (key, value) {
let position = this.hashCode(key)
if(this.table[position] == undefined){
this.table[position] = new ValuePair(key, value)
}else{
let index = ++position
while(this.table[index] !== undefined){
index ++
}
this.table[index] = new ValuePair(key, value)
}
}
/** * 根據鍵值從散列表中移除值 * @param {*} key 移除的鍵名 * @return {Boolean} 是否成功移除 */
remove (key) {
let position = this.hashCode(key)
if( !key || this.table[position] === undefined ) return undefined
if(this.table[position].key === key){
this.table[index] = undefined
}else{
let index = ++position
while(
this.table[index] === undefined ||
this.table[index].key !== key
){
index ++
}
if(this.table[index].key === key){
this.table[index] = undefined
}
}
}
/** * 根據鍵值檢索到特定的值 * @param {*} key 查找的鍵名 * @return {*} 查找的值 */
get (key) {
let position = this.hashCode(key)
if( !key || this.table[position] === undefined ) return undefined
if(this.table[position].key === key){
return this.table[position].value
}else{
let index = ++position
while(
this.table[index] === undefined ||
this.table[index].key !== key
){
index ++
}
if(this.table[index].key === key){
return this.table[index].value
}
}
}
/** * 打印散列表中已保存的值 * @return {*} 散列表的值 */
print () {
return this.table
}
}
複製代碼
下週將練習 Tree 的題目。