随笔分类 - 学习JavaScript数据结构与算法
摘要:const Comepare = { LESS_THAN: -1, BIGGER_THAN: 1, EQUALS: 0 } function defaultCompare(a, b) { if (a b) { return Comepare.EQUALS; } return a < b ? Come
阅读全文
摘要:const map = new Map(); map.set('Gandalf', 'gandalf@email.com'); map.set('John', 'johnsnow@email.com'); map.set('Tyrion', 'tyrion@email.com'); console.
阅读全文
摘要:一个良好的散列函数的构成有:插入和检索的时间(即性能),以及较低的冲突可能性。 djb2HashCode(key){ const tableKey = this.toStrFn(key); let hash = 5381; for (let i = 0; i < tableKey.length; i
阅读全文
摘要:线性探查:当想表中的某个位置添加一个元素的时候,如果索引为position的位置已经被占据了,就尝试position+1的位置。如果position的位置也被占据了,就尝试position+2的位置,以此类推,直到在散列表中找到一个空闲的位置。 put(key, value) { if (key !
阅读全文
摘要:处理散列表中冲突的方法有:分离链接、线性探查和双散列法。 分离链接法:为散列表的每一个位置创建一个链表并将元素存储到里面。 相对于之前的散列表只需重写put、get和remove方法。 put(key, value) { if (key == null || value == null) { //键
阅读全文
摘要:散列算法的作用是尽可能快地在数据结构中找到一个值。 散列函数的作用是给定一个键值,然后返回值在表中的地址。 class ValuePair { //用于存放一对键值对 constructor(key, value) { this.key = key; this.value = value; } to
阅读全文
摘要:字典也称为映射、符号表或关联数组,在计算机科学中经常用来保存对象的引用地址。 集合以【值,值】的形式存放元素,字典是以【键,值】的形式存放元素。 class ValuePair { //存放一对键值的对象 constructor(key, value) { this.key = key; this.
阅读全文
摘要:let set = new Set(); function union(set1, set2) { //set1与set2的并集 let unionSet = new Set(); set1.forEach(element => { unionSet.add(element); }); set2.f
阅读全文
摘要:class Set { constructor() { this.items = {}; } add(element) { if (this.has(element)) { return false; } else { this.items[element] = element; return tr
阅读全文
摘要:class Node { //结点内部一个next指针一个元素数据 constructor(element) { this.next = null; this.element = element; } } class LinkedList { constructor() { //链表类属性为一个长度
阅读全文
摘要:class Deque { constructor() { this.count = 0; this.lowestCount = 0; this.items = {}; } addFront(element) { if (this.isEmpty()) { this.addBack(element)
阅读全文
摘要:class Queue { constructor() { this.count = 0; this.lowestCount = 0; this.items = {}; } enqueue(element) { this.items[this.count] = element; this.count
阅读全文
摘要:class Stack { constructor() { this.count = 0; this.items = {}; } push(element) { this.items[this.count] = element; this.count++; } pop() { if (this.is
阅读全文
摘要:class StackArray { constructor() { this.items = []; } push(element) { this.items.push(element); } pop() { return this.items.pop(); } peek() { return t
阅读全文

浙公网安备 33010602011771号