1 Star 1 Fork 0

CoderMonkey/data-struct-js

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

data-struct-js

About this package

Implement of common data structures using javascript. besides supplied by JS, such as Array/Set(WeakSet)/Map(WeakMap)

source code

Write in ES6, using babel to convert to ES5.

You can download and run npm run compile with customized babel setting to meet your needs.

How to use

more examples see data-struct-js/examples

install the package

npm install --save-dev data-struct-js

or

npm i -D data-struct-js

then use the data-structures supplied simply as below

const {
    Stack,
    Queue,
    PriorityQueue,
    LinkedList,
    DoublyLinkedList,
    CircleLinkedList,
    HashTalbe,
    BinarySearchTree,
    RedBlackTree,
} = require('data-struct-js')
// Or ↓
// import {
//     Stack,
//     Queue,
//     PriorityQueue,
//     LinkedList,
//     DoublyLinkedList,
//     CircleLinkedList,
//     HashTalbe,
//     BinarySearchTree,
//     RedBlackTree,
// } from 'data-struct-js'
// -> may need webpack and bable-loader environment

//----------------------------
// Stack
let stack = new Stack()
stack.push('Element1')
stack.push('Element2')
stack.pop() // Element2
stack.pop() // Element1

//----------------------------
// Queue
let queue = new Queue()
queue.enqueue('First')
queue.enqueue('Second')
queue.dequeue()   // First
queue.dequeue()   // Second

//----------------------------
// PriorityQueue
let pq = new PriorityQueue()
pq.enqueue('1-Memory', 1)
pq.enqueue('2-CPU', 0)
pq.enqueue('3-HardDisk', 2)
pq.enqueue('4-Display', 3)
pq.enqueue('5-OperatingSystem', 5)
pq.enqueue('6-Aplication')
// -->
// 0: QueueElement {__element: "2-CPU", __priority: 0, toString: ƒ}
// 1: QueueElement {__element: "1-Memory", __priority: 1, toString: ƒ}
// 2: QueueElement {__element: "3-HardDisk", __priority: 2, toString: ƒ}
// 3: QueueElement {__element: "4-Display", __priority: 3, toString: ƒ}
// 4: QueueElement {__element: "5-OperatingSystem", __priority: 5, toString: ƒ}
// 5: QueueElement {__element: "6-Aplication", __priority: Infinity, toString: ƒ}

//----------------------------
// LinkedList
let linkedList = new LinkedList()
linkedList.append({name: 'CoderMonkey', age: 18})
linkedList.insert(0, {name: 'CoderMonkie',age: 36})
linkedList.remove({name: 'CoderMonkie',age: 36}, function(x, y){ return x.name === y.name})
//...

//----------------------------
// DoublyLinkedList
let doublyLinkedList = new DoublyLinkedList()
const traverseFunc = function(element){
    console.log(element.name, element,age)
}
doublyLinkedList.append({name: 'CoderMonkey', age: 18})
doublyLinkedList.append({name: 'CoderGorilla',age: 19})
doublyLinkedList.insert(0, {name: 'CoderDog',age: 20})
doublyLinkedList.insert(3, {name: 'CoderSheep',age: 21})
doublyLinkedList.traverse(traverseFunc)
doublyLinkedList.traverse(traverseFunc, true)
//...

//----------------------------
// CircleLinkedList
let circle = new CircleLinkedList()
circle.append("1.Plan")
circle.append("2.Do")
circle.append("3.Check")
circle.append("4.Action")
for (let j = 0; j < 9; j++) {
    const item = circle.getNext()
    console.log(`${j} : ${item}`)
}

//----------------------------
// HashTable
let hashTable = new HashTable()
// Add
hashTable.put('One', 'One eyewitness is better than ten hearsays.')
hashTable.put('Two', 'One hour today is worth two tomorrow.')
hashTable.put('Three', 'One boy is a boy, two boys half a boy, three boys no boy.')
hashTable.put('Four', 'Four eyes see more than two.')
// Update
hashTable.put('One', 'One world, one dream.')
// Delete
hashTable.delete('Three')   // true
hashTable.delete('Five')    // false
//...

//----------------------------
// BinarySearchTree
let bst = new BinarySearchTree()
bst.insert(8, "8: eight")
bst.insert(6, "6: six")
bst.insert(13, "13: thirteen")
bst.insert(4, "4: four")
bst.insert(7, "7: seven")
bst.insert(11, "11: eleven")
bst.insert(3, "3: three")
bst.insert(5, "5: five")
bst.insert(10, "10: ten")
bst.insert(20, "20: twenty")
bst.insert(12, "12: twelve")
bst.insert(16, "16: sixteen")
bst.insert(35, "35: thirty-five")
bst.insert(9, "9: nine")
bst.insert(18, "18: eighteen")
bst.insert(19, "19: nineteen")
/**
 * 
 *              8
 *     ------------------
 *     |                |
 *     6                13
 * ---------     ---------------
 *   |    |       |           |  
 *   4    7       11          20
 * -----       -------    ----------
 * |   |       |     |    |       |
 * 3   5       10    12   16      35
 *           -----      ------ 
 *           |               |
 *           9               18
 *                          -----
 *                              |
 *                              19
 * 
 */

var traverseCallbackFunc = function(value) {
    console.log(`Traversing!\r\n==>current is [${value}]`)
}
bst.inOrderTraverse(traverseCallbackFunc)

bst.remove(13)
// ⬇️
/**
 * 
 *              8
 *     ------------------
 *     |                |
 *     6                16
 * ---------     ---------------
 *   |    |       |           |  
 *   4    7       11          18
 * -----       -------    ----------
 * |   |       |     |            |
 * 3   5       10    12           19
 *           -----              ------ 
 *           |                       |
 *           9                       20
*                                  -----
 *                                     |
 *                                     35
 * 
 */
//...

Data structures

Implement data structures:

Original data structures in JavaScript:

  • Array
  • Set/WeakSet
  • Map/WeakMap

⬆️ not in this library since you could use them directly.


License

MIT

Feel free to use it.

MIT License Copyright (c) 2019 Mao NianYou Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

Implementation of Data Structures in JavaScript. 展开 收起
README
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/coder-monkey/data-struct-js.git
git@gitee.com:coder-monkey/data-struct-js.git
coder-monkey
data-struct-js
data-struct-js
master

搜索帮助