链表
class Node {
constructor(item) {
this.item = item;
this.next = null;
}
}
栈
const stack = [];
stack.push(1,2,3);
stack.pop();
class Stack {
constructor() {
this.first = null;
this.length = 0;
}
isEmpty(){
return this.length === 0;
}
size(){
return this.length;
}
push(item) {
const oldFirst = this.first;
this.first = new Node(item);
this.first.next = oldFirst;
this.length++;
}
pop() {
let item = this.first.item;
this.first = this.first.next;
this.length--;
return item;
}
}
队列
const stack = [];
stack.push(1,2,3);
stack.shift();
class Queue {
constructor() {
this.first = null;
this.last = null;
this.length = 0;
}
isEmpty(){
return this.length === 0;
}
size(){
return this.length;
}
push(item) {
let oldLast = this.last;
this.last = new Node(item);
if(this.isEmpty()){
this.first = this.last;
}else{
oldLast.next = this.last;
}
this.length++;
}
pop() {
let item = this.first.item;
this.first = this.first.next;
if(this.isEmpty) this.last = null;
this.length--;
return item;
}
}
背包
const bag= [];
bag.push(1,2,3);
class Bag{
constructor() {
this.first = null;
}
isEmpty(){
return this.first === null;
}
add(item) {
const oldFirst = this.first;
this.first = new Node(item);
this.first.next = oldFirst;
}
}
图
class Graph{
constructor(){
this.edgeLength = 0;
this.vertexLength = 0;
this.adj = {};
}
addVertex(v){
this.adj[v] = [];
}
addEdge(v,w){
this.adj[v].push(w);
this.adj[w].push(v);
}
}
深度遍历
class DepthSearch{
constructor(graph,s){
this.marked = {};
this.edgeTo = {};
this.count = 0;
this.dfs(graph,s);
}
dfs(graph,v){
this.marked[v] = true;
this.count++ ;
let bag = graph.adj[v];
bag.forEach(w=>{
if(!this.marked[w]){
this.edgeTo[w] = v;
this.dfs(graph,w);
}
})
}
hasPathTo(v){
return this.marked[v];
}
}
广度遍历
class BreadthSearch{
constructor(graph,s){
this.marked = {};
this.edgeTo = {};
this.count = 0;
this.bfs(graph,s);
}
bfs(graph,s){
let queue = [s],adj = graph.adj;
let {marked,edgeTo} = this;
marked[s] = true;
while(queue.length){
let v = queue.shift();
let bag = adj[v];
bag.forEach(w=>{
if(!marked[w]){
marked[w] = true;
edgeTo[w] = v;
queue.push(w);
}
})
}
}
hasPathTo(v){
return this.marked[v];
}
}
连通分量
class CC{
constructor(graph){
this.ids = {};
this.count = 0;
this.marked = {};
this.run(graph);
}
run(graph){
let vertices = graph.vertices;
let length = graph.vertices.length;
let {marked,count} = this;
for(let i=0;i<length;i++) {
let s = vertices[i];
if (!marked[s]) {
this.dfs(graph, s);
count++;
}
}
}
dfs(graph,v){
this.marked[v] = true;
this.ids[v] = this.count;
let bag = graph.adj[v];
bag.forEach(w=>{
if(!this.marked[w]){
this.dfs(graph,w);
}
})
}
connected(v,w){
return this.ids[v] = this.ids[w];
}
}
二叉树
class Node{
constructor(key,value,N){
this.left = null;
this.right = null;
this.value = value;
this.key = key;
this.N = N;
}
}
class BST{
constructor(){
this.root = null;
}
getFromRoot(key){
return this.get(this.root,key);
}
get(node,key){
if(node === null) return null;
if(key > node.key) return this.get(node.right,key);
else if(key < node.key) return this.get(node.left,key);
else return node.value;
}
putFromRoot(key,value){
this.root = this.put(this.root,key,value);
}
put(node,key,value){
if(!node) return new Node(key,value,1);
let nodeKey = node.key;
if(key < nodeKey) node.left = this.put(node.left,key,value);
else if(key > nodeKey) node.right = this.put(node.right,key,value);
else node.value = value;
node.N = this.size(node.left) + this.size(node.right) + 1;
return node;
}
size(node){
return node?node.N:0;
}
}
最大公约数
function gcd(p,q){
if(q===0)return p;
const r = p % q;
return gcd(q , r);
}
最小公倍数
二分查找
function BinarySearch(items,value){
let start = 0;
let end = items.length - 1;
while(start<=end){
let mid = start + parseInt((end - start)/2);
let midV = items[mid];
if(value < midV)end = mid - 1;
else if(value > midV) start = mid + 1;
else return midV;
}
return null;
}
const array = [2,31,51,23,15,1,2,5];
const items = array.slice().sort((c,n)=>c-n);
BinarySearch(items,51);