算法导论 4

链表

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();

//基于链表 
//Array.prototype.push Array.prototype.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);
}

最小公倍数

//p*q/gcd(p,q)

二分查找

//第K次查询,剩余待查询个数N/(2^K)
//最坏情况,N/(2^K)=1 时间复杂度K=logN
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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值