日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
常見數(shù)據(jù)結(jié)構(gòu)和Javascript實現(xiàn)總結(jié)

做前端的同學(xué)不少都是自學(xué)成才或者半路出家,計算機基礎(chǔ)的知識比較薄弱,尤其是數(shù)據(jù)結(jié)構(gòu)和算法這塊,所以今天整理了一下常見的數(shù)據(jù)結(jié)構(gòu)和對應(yīng)的Javascript的實現(xiàn),希望能幫助大家完善這方面的知識體系。

創(chuàng)新互聯(lián)"三網(wǎng)合一"的企業(yè)建站思路。企業(yè)可建設(shè)擁有電腦版、微信版、手機版的企業(yè)網(wǎng)站。實現(xiàn)跨屏營銷,產(chǎn)品發(fā)布一步更新,電腦網(wǎng)絡(luò)+移動網(wǎng)絡(luò)一網(wǎng)打盡,滿足企業(yè)的營銷需求!創(chuàng)新互聯(lián)具備承接各種類型的成都網(wǎng)站制作、成都做網(wǎng)站、外貿(mào)營銷網(wǎng)站建設(shè)項目的能力。經(jīng)過十多年的努力的開拓,為不同行業(yè)的企事業(yè)單位提供了優(yōu)質(zhì)的服務(wù),并獲得了客戶的一致好評。

1. Stack(棧)

Stack的特點是后進先出(last in first out)。生活中常見的Stack的例子比如一摞書,你最后放上去的那本你之后會最先拿走;又比如瀏覽器的訪問歷史,當(dāng)點擊返回按鈕,最后訪問的網(wǎng)站最先從歷史記錄中彈出。

  1. Stack一般具備以下方法:
  2. push:將一個元素推入棧頂
  3. pop:移除棧頂元素,并返回被移除的元素
  4. peek:返回棧頂元素
  5. length:返回棧中元素的個數(shù)

Javascript的Array天生具備了Stack的特性,但我們也可以從頭實現(xiàn)一個 Stack類:

 
 
 
 
  1. function Stack() {
  2.   this.count = 0;
  3.   this.storage = {};
  4.   this.push = function (value) {
  5.     this.storage[this.count] = value;
  6.     this.count++;
  7.   }
  8.   this.pop = function () {
  9.     if (this.count === 0) {
  10.       return undefined;
  11.     }
  12.     this.count--;
  13.     var result = this.storage[this.count];
  14.     delete this.storage[this.count];
  15.     return result;
  16.   }
  17.   this.peek = function () {
  18.     return this.storage[this.count - 1];
  19.   }
  20.   this.size = function () {
  21.     return this.count;
  22.   }
  23. }

2. Queue(隊列)

Queue和Stack有一些類似,不同的是Stack是先進后出,而Queue是先進先出。Queue在生活中的例子比如排隊上公交,排在第一個的總是最先上車;又比如打印機的打印隊列,排在前面的最先打印。

  • Queue一般具有以下常見方法:
  • enqueue:入列,向隊列尾部增加一個元素
  • dequeue:出列,移除隊列頭部的一個元素并返回被移除的元素
  • front:獲取隊列的第一個元素
  • isEmpty:判斷隊列是否為空
  • size:獲取隊列中元素的個數(shù)

Javascript中的Array已經(jīng)具備了Queue的一些特性,所以我們可以借助Array實現(xiàn)一個Queue類型:

 
 
 
 
  1. function Queue() {
  2.   var collection = [];
  3.   this.print = function () {
  4.     console.log(collection);
  5.   }
  6.   this.enqueue = function (element) {
  7.     collection.push(element);
  8.   }
  9.   this.dequeue = function () {
  10.     return collection.shift();
  11.   }
  12.   this.front = function () {
  13.     return collection[0];
  14.   }
  15.   this.isEmpty = function () {
  16.     return collection.length === 0;
  17.   }
  18.   this.size = function () {
  19.     return collection.length;
  20.   }
  21. }

Priority Queue(優(yōu)先隊列)

Queue還有個升級版本,給每個元素賦予優(yōu)先級,優(yōu)先級高的元素入列時將排到低優(yōu)先級元素之前。區(qū)別主要是enqueue方法的實現(xiàn):

 
 
 
 
  1. function PriorityQueue() {
  2.   ...
  3.   this.enqueue = function (element) {
  4.     if (this.isEmpty()) {
  5.       collection.push(element);
  6.     } else {
  7.       var added = false;
  8.       for (var i = 0; i < collection.length; i++) {
  9.         if (element[1] < collection[i][1]) {
  10.           collection.splice(i, 0, element);
  11.           added = true;
  12.           break;
  13.         }
  14.       }
  15.       if (!added) {
  16.         collection.push(element);
  17.       }
  18.     }
  19.   }
  20. }

測試一下:

 
 
 
 
  1. var pQ = new PriorityQueue();
  2. pQ.enqueue(['gannicus', 3]);
  3. pQ.enqueue(['spartacus', 1]);
  4. pQ.enqueue(['crixus', 2]);
  5. pQ.enqueue(['oenomaus', 4]);
  6. pQ.print();

結(jié)果:

 
 
 
 
  1. [
  2.   [ 'spartacus', 1 ],
  3.   [ 'crixus', 2 ],
  4.   [ 'gannicus', 3 ],
  5.   [ 'oenomaus', 4 ]
  6. ]

3. Linked List(鏈表)

顧名思義,鏈表是一種鏈式數(shù)據(jù)結(jié)構(gòu),鏈上的每個節(jié)點包含兩種信息:節(jié)點本身的數(shù)據(jù)和指向下一個節(jié)點的指針。鏈表和傳統(tǒng)的數(shù)組都是線性的數(shù)據(jù)結(jié)構(gòu),存儲的都是一個序列的數(shù)據(jù),但也有很多區(qū)別,如下表:

一個單向鏈表通常具有以下方法:

  1. size:返回鏈表中節(jié)點的個數(shù)
  2. head:返回鏈表中的頭部元素
  3. add:向鏈表尾部增加一個節(jié)點
  4. remove:刪除某個節(jié)點
  5. indexOf:返回某個節(jié)點的index
  6. elementAt:返回某個index處的節(jié)點
  7. addAt:在某個index處插入一個節(jié)點
  8. removeAt:刪除某個index處的節(jié)點

單向鏈表的Javascript實現(xiàn):

 
 
 
 
  1. /**
  2.  * 鏈表中的節(jié)點 
  3.  */
  4. function Node(element) {
  5.   // 節(jié)點中的數(shù)據(jù)
  6.   this.element = element;
  7.   // 指向下一個節(jié)點的指針
  8.   this.next = null;
  9. }
  10. function LinkedList() {
  11.   var length = 0;
  12.   var head = null;
  13.   this.size = function () {
  14.     return length;
  15.   }
  16.   this.head = function () {
  17.     return head;
  18.   }
  19.   this.add = function (element) {
  20.     var node = new Node(element);
  21.     if (head == null) {
  22.       head = node;
  23.     } else {
  24.       var currentNode = head;
  25.       while (currentNode.next) {
  26.         currentNode = currentNode.next;
  27.       }
  28.       currentNode.next = node;
  29.     }
  30.     length++;
  31.   }
  32.   this.remove = function (element) {
  33.     var currentNode = head;
  34.     var previousNode;
  35.     if (currentNode.element === element) {
  36.       head = currentNode.next;
  37.     } else {
  38.       while (currentNode.element !== element) {
  39.         previousNode = currentNode;
  40.         currentNode = currentNode.next;
  41.       }
  42.       previousNode.next = currentNode.next;
  43.     }
  44.     length--;
  45.   }
  46.   this.isEmpty = function () {
  47.     return length === 0;
  48.   }
  49.   this.indexOf = function (element) {
  50.     var currentNode = head;
  51.     var index = -1;
  52.     while (currentNode) {
  53.       index++;
  54.       if (currentNode.element === element) {
  55.         return index;
  56.       }
  57.       currentNode = currentNode.next;
  58.     }
  59.     return -1;
  60.   }
  61.   this.elementAt = function (index) {
  62.     var currentNode = head;
  63.     var count = 0;
  64.     while (count < index) {
  65.       count++;
  66.       currentNode = currentNode.next;
  67.     }
  68.     return currentNode.element;
  69.   }
  70.   this.addAt = function (index, element) {
  71.     var node = new Node(element);
  72.     var currentNode = head;
  73.     var previousNode;
  74.     var currentIndex = 0;
  75.     if (index > length) {
  76.       return false;
  77.     }
  78.     if (index === 0) {
  79.       node.next = currentNode;
  80.       head = node;
  81.     } else {
  82.       while (currentIndex < index) {
  83.         currentIndex++;
  84.         previousNode = currentNode;
  85.         currentNode = currentNode.next;
  86.       }
  87.       node.next = currentNode;
  88.       previousNode.next = node;
  89.     }
  90.     length++;
  91.   }
  92.   this.removeAt = function (index) {
  93.     var currentNode = head;
  94.     var previousNode;
  95.     var currentIndex = 0;
  96.     if (index < 0 || index >= length) {
  97.       return null;
  98.     }
  99.     if (index === 0) {
  100.       head = currentIndex.next;
  101.     } else {
  102.       while (currentIndex < index) {
  103.         currentIndex++;
  104.         previousNode = currentNode;
  105.         currentNode = currentNode.next;
  106.       }
  107.       previousNode.next = currentNode.next;
  108.     }
  109.     length--;
  110.     return currentNode.element;
  111.   }
  112. }

4. Set(集合)

集合是數(shù)學(xué)中的一個基本概念,表示具有某種特性的對象匯總成的集體。在ES6中也引入了集合類型Set,Set和Array有一定程度的相似,不同的是Set中不允許出現(xiàn)重復(fù)的元素而且是無序的。

一個典型的Set應(yīng)該具有以下方法:

  1. values:返回集合中的所有元素
  2. size:返回集合中元素的個數(shù)
  3. has:判斷集合中是否存在某個元素
  4. add:向集合中添加元素
  5. remove:從集合中移除某個元素
  6. union:返回兩個集合的并集
  7. intersection:返回兩個集合的交集
  8. difference:返回兩個集合的差集
  9. subset:判斷一個集合是否為另一個集合的子集

使用Javascript可以將Set進行如下實現(xiàn),為了區(qū)別于ES6中的Set命名為MySet:

 
 
 
 
  1. function MySet() {
  2.   var collection = [];
  3.   this.has = function (element) {
  4.     return (collection.indexOf(element) !== -1);
  5.   }
  6.   this.values = function () {
  7.     return collection;
  8.   }
  9.   this.size = function () {
  10.     return collection.length;
  11.   }
  12.   this.add = function (element) {
  13.     if (!this.has(element)) {
  14.       collection.push(element);
  15.       return true;
  16.     }
  17.     return false;
  18.   }
  19.   this.remove = function (element) {
  20.     if (this.has(element)) {
  21.       index = collection.indexOf(element);
  22.       collection.splice(index, 1);
  23.       return true;
  24.     }
  25.     return false;
  26.   }
  27.   this.union = function (otherSet) {
  28.     var unionSet = new MySet();
  29.     var firstSet = this.values();
  30.     var secondSet = otherSet.values();
  31.     firstSet.forEach(function (e) {
  32.       unionSet.add(e);
  33.     });
  34.     secondSet.forEach(function (e) {
  35.       unionSet.add(e);
  36.     });
  37.     return unionSet;
  38.   }
  39.   this.intersection = function (otherSet) {
  40.     var intersectionSet = new MySet();
  41.     var firstSet = this.values();
  42.     firstSet.forEach(function (e) {
  43.       if (otherSet.has(e)) {
  44.         intersectionSet.add(e);
  45.       }
  46.     });
  47.     return intersectionSet;
  48.   }
  49.   this.difference = function (otherSet) {
  50.     var differenceSet = new MySet();
  51.     var firstSet = this.values();
  52.     firstSet.forEach(function (e) {
  53.       if (!otherSet.has(e)) {
  54.         differenceSet.add(e);
  55.       }
  56.     });
  57.     return differenceSet;
  58.   }
  59.   this.subset = function (otherSet) {
  60.     var firstSet = this.values();
  61.     return firstSet.every(function (value) {
  62.       return otherSet.has(value);
  63.     });
  64.   }
  65. }

5. Hash Table(哈希表/散列表)

Hash Table是一種用于存儲鍵值對(key value pair)的數(shù)據(jù)結(jié)構(gòu),因為Hash Table根據(jù)key查詢value的速度很快,所以它常用于實現(xiàn)Map、Dictinary、Object等數(shù)據(jù)結(jié)構(gòu)。如上圖所示,Hash Table內(nèi)部使用一個hash函數(shù)將傳入的鍵轉(zhuǎn)換成一串?dāng)?shù)字,而這串?dāng)?shù)字將作為鍵值對實際的key,通過這個key查詢對應(yīng)的value非常快,時間復(fù)雜度將達到O(1)。Hash函數(shù)要求相同輸入對應(yīng)的輸出必須相等,而不同輸入對應(yīng)的輸出必須不等,相當(dāng)于對每對數(shù)據(jù)打上唯一的指紋。

一個Hash Table通常具有下列方法:

  1. add:增加一組鍵值對
  2. remove:刪除一組鍵值對
  3. lookup:查找一個鍵對應(yīng)的值

一個簡易版本的Hash Table的Javascript實現(xiàn):

 
 
 
 
  1. function hash(string, max) {
  2.   var hash = 0;
  3.   for (var i = 0; i < string.length; i++) {
  4.     hash += string.charCodeAt(i);
  5.   }
  6.   return hash % max;
  7. }
  8. function HashTable() {
  9.   let storage = [];
  10.   const storageLimit = 4;
  11.   this.add = function (key, value) {
  12.     var index = hash(key, storageLimit);
  13.     if (storage[index] === undefined) {
  14.       storage[index] = [
  15.         [key, value]
  16.       ];
  17.     } else {
  18.       var inserted = false;
  19.       for (var i = 0; i < storage[index].length; i++) {
  20.         if (storage[index][i][0] === key) {
  21.           storage[index][i][1] = value;
  22.           inserted = true;
  23.         }
  24.       }
  25.       if (inserted === false) {
  26.         storage[index].push([key, value]);
  27.       }
  28.     }
  29.   }
  30.   this.remove = function (key) {
  31.     var index = hash(key, storageLimit);
  32.     if (storage[index].length === 1 && storage[index][0][0] === key) {
  33.       delete storage[index];
  34.     } else {
  35.       for (var i = 0; i < storage[index]; i++) {
  36.         if (storage[index][i][0] === key) {
  37.           delete storage[index][i];
  38.         }
  39.       }
  40.     }
  41.   }
  42.   this.lookup = function (key) {
  43.     var index = hash(key, storageLimit);
  44.     if (storage[index] === undefined) {
  45.       return undefined;
  46.     } else {
  47.       for (var i = 0; i < storage[index].length; i++) {
  48.         if (storage[index][i][0] === key) {
  49.           return storage[index][i][1];
  50.         }
  51.       }
  52.     }
  53.   }
  54. }

6. Tree(樹)

顧名思義,Tree的數(shù)據(jù)結(jié)構(gòu)和自然界中的樹極其相似,有根、樹枝、葉子,如上圖所示。Tree是一種多層數(shù)據(jù)結(jié)構(gòu),與Array、Stack、Queue相比是一種非線性的數(shù)據(jù)結(jié)構(gòu),在進行插入和搜索操作時很高效。在描述一個Tree時經(jīng)常會用到下列概念:

  1. Root(根):代表樹的根節(jié)點,根節(jié)點沒有父節(jié)點
  2. Parent Node(父節(jié)點):一個節(jié)點的直接上級節(jié)點,只有一個
  3. Child Node(子節(jié)點):一個節(jié)點的直接下級節(jié)點,可能有多個
  4. Siblings(兄弟節(jié)點):具有相同父節(jié)點的節(jié)點
  5. Leaf(葉節(jié)點):沒有子節(jié)點的節(jié)點
  6. Edge(邊):兩個節(jié)點之間的連接線
  7. Path(路徑):從源節(jié)點到目標節(jié)點的連續(xù)邊
  8. Height of Node(節(jié)點的高度):表示節(jié)點與葉節(jié)點之間的最長路徑上邊的個數(shù)
  9. Height of Tree(樹的高度):即根節(jié)點的高度
  10. Depth of Node(節(jié)點的深度):表示從根節(jié)點到該節(jié)點的邊的個數(shù)
  11. Degree of Node(節(jié)點的度):表示子節(jié)點的個數(shù)

我們以二叉查找樹為例,展示樹在Javascript中的實現(xiàn)。在二叉查找樹中,即每個節(jié)點最多只有兩個子節(jié)點,而左側(cè)子節(jié)點小于當(dāng)前節(jié)點,而右側(cè)子節(jié)點大于當(dāng)前節(jié)點,如圖所示:

一個二叉查找樹應(yīng)該具有以下常用方法:

  1. add:向樹中插入一個節(jié)點
  2. findMin:查找樹中最小的節(jié)點
  3. findMax:查找樹中最大的節(jié)點
  4. find:查找樹中的某個節(jié)點
  5. isPresent:判斷某個節(jié)點在樹中是否存在
  6. remove:移除樹中的某個節(jié)點

以下是二叉查找樹的Javascript實現(xiàn):

 
 
 
 
  1. class Node {
  2.   constructor(data, left = null, right = null) {
  3.     this.data = data;
  4.     this.left = left;
  5.     this.right = right;
  6.   }
  7. }
  8. class BST {
  9.   constructor() {
  10.     this.root = null;
  11.   }
  12.   add(data) {
  13.     const node = this.root;
  14.     if (node === null) {
  15.       this.root = new Node(data);
  16.       return;
  17.     } else {
  18.       const searchTree = function (node) {
  19.         if (data < node.data) {
  20.           if (node.left === null) {
  21.             node.left = new Node(data);
  22.             return;
  23.           } else if (node.left !== null) {
  24.             return searchTree(node.left);
  25.           }
  26.         } else if (data > node.data) {
  27.           if (node.right === null) {
  28.             node.right = new Node(data);
  29.             return;
  30.           } else if (node.right !== null) {
  31.             return searchTree(node.right);
  32.           }
  33.         } else {
  34.           return null;
  35.         }
  36.       };
  37.       return searchTree(node);
  38.     }
  39.   }
  40.   findMin() {
  41.     let current = this.root;
  42.     while (current.left !== null) {
  43.       current = current.left;
  44.     }
  45.     return current.data;
  46.   }
  47.   findMax() {
  48.     let current = this.root;
  49.     while (current.right !== null) {
  50.       current = current.right;
  51.     }
  52.     return current.data;
  53.   }
  54.   find(data) {
  55.     let current = this.root;
  56.     while (current.data !== data) {
  57.       if (data < current.data) {
  58.         current = current.left
  59.       } else {
  60.         current = current.right;
  61.       }
  62.       if (current === null) {
  63.         return null;
  64.       }
  65.     }
  66.     return current;
  67.   }
  68.   isPresent(data) {
  69.     let current = this.root;
  70.     while (current) {
  71.       if (data === current.data) {
  72.         return true;
  73.       }
  74.       if (data < current.data) {
  75.         current = current.left;
  76.       } else {
  77.         current = current.right;
  78.       }
  79.     }
  80.     return false;
  81.   }
  82.   remove(data) {
  83.     const removeNode = function (node, data) {
  84.       if (node == null) {
  85.         return null;
  86.       }
  87.       if (data == node.data) {
  88.         // node沒有子節(jié)點
  89.         if (node.left == null && node.right == null) {
  90.           return null;
  91.         }
  92.         // node沒有左側(cè)子節(jié)點
  93.         if (node.left == null) {
  94.           return node.right;
  95.         }
  96.         // node沒有右側(cè)子節(jié)點
  97.         if (node.right == null) {
  98.           return node.left;
  99.         }
  100.         // node有兩個子節(jié)點
  101.         var tempNode = node.right;
  102.         while (tempNode.left !== null) {
  103.           tempNode = tempNode.left;
  104.         }
  105.         node.data = tempNode.data;
  106.         node.right = removeNode(node.right, tempNode.data);
  107.         return node;
  108.       } else if (data < node.data) {
  109.         node.left = removeNode(node.left, data);
  110.         return node;
  111.       } else {
  112.         node.right = removeNode(node.right, data);
  113.         return node;
  114.       }
  115.     }
  116.     this.root = removeNode(this.root, data);
  117.   }
  118. }

測試一下:

 
 
 
 
  1. const bst = new BST();
  2. bst.add(4);
  3. bst.add(2);
  4. bst.add(6);
  5. bst.add(1);
  6. bst.add(3);
  7. bst.add(5);
  8. bst.add(7);
  9. bst.remove(4);
  10. console.log(bst.findMin());
  11. console.log(bst.findMax());
  12. bst.remove(7);
  13. console.log(bst.findMax());
  14. console.log(bst.isPresent(4));

打印結(jié)果:

 
 
 
 
  1. 1
  2. 7
  3. 6
  4. false

7. Trie(字典樹,讀音同try)

Trie也可以叫做Prefix Tree(前綴樹),也是一種搜索樹。Trie分步驟存儲數(shù)據(jù),樹中的每個節(jié)點代表一個步驟,trie常用于存儲單詞以便快速查找,比如實現(xiàn)單詞的自動完成功能。 Trie中的每個節(jié)點都包含一個單詞的字母,跟著樹的分支可以可以拼寫出一個完整的單詞,每個節(jié)點還包含一個布爾值表示該節(jié)點是否是單詞的最后一個字母。

Trie一般有以下方法:

  • add:向字典樹中增加一個單詞
  • isWord:判斷字典樹中是否包含某個單詞
  • print:返回字典樹中的所有單詞
 
 
 
 
  1. /**
  2.  * Trie的節(jié)點
  3.  */
  4. function Node() {
  5.   this.keys = new Map();
  6.   this.end = false;
  7.   this.setEnd = function () {
  8.     this.end = true;
  9.   };
  10.   this.isEnd = function () {
  11.     return this.end;
  12.   }
  13. }
  14. function Trie() {
  15.   this.root = new Node();
  16.   this.add = function (input, node = this.root) {
  17.     if (input.length === 0) {
  18.       node.setEnd();
  19.       return;
  20.     } else if (!node.keys.has(input[0])) {
  21.       node.keys.set(input[0], new Node());
  22.       return this.add(input.substr(1), node.keys.get(input[0]));
  23.     } else {
  24.       return this.add(input.substr(1), node.keys.get(input[0]));
  25.     }
  26.   }
  27.   this.isWord = function (word) {
  28.     let node = this.root;
  29.     while (word.length > 1) {
  30.       if (!node.keys.has(word[0])) {
  31.         return false;
  32.       } else {
  33.         node = node.keys.get(word[0]);
  34.         word = word.substr(1);
  35.       }
  36.     }
  37.     return (node.keys.has(word) && node.keys.get(word).isEnd()) ? true : false;
  38.   }
  39.   this.print = function () {
  40.     let words = new Array();
  41.     let search = function (node = this.root, string) {
  42.       if (node.keys.size != 0) {
  43.         for (let letter of node.keys.keys()) {
  44.           search(node.keys.get(letter), string.concat(letter));
  45.         }
  46.         if (node.isEnd()) {
  47.           words.push(string);
  48.    &nbs
    分享名稱:常見數(shù)據(jù)結(jié)構(gòu)和Javascript實現(xiàn)總結(jié)
    當(dāng)前URL:http://www.5511xx.com/article/dpehepo.html