![](https://img-blog.csdnimg.cn/20201014180756913.png?x-oss-process=image/resize,m_fixed,h_224,w_224)
数据结构与算法
数据结构与算法
·Q·
努力努力
展开
-
实现LRU缓存淘汰算法
LRU:缓存淘汰算法,最近最少使用淘汰队列class LRUCache{ constructor(size){ this.capacity = size; this.cache = new Map(); } // 队列 先前先出 对头进 队尾出 get(key){ if(this.cache.has(key)){ let temp = this.cache.get(key); .原创 2022-01-03 00:50:25 · 804 阅读 · 0 评论 -
二叉搜索树
构建二叉树function Node(value) { this.value = value; this.left = null; this.right = null; }function addNode(root, value) { if (root == null || root.value == value) return; if (value < root.value) { if (root.left == null) root.le.原创 2021-02-25 21:05:09 · 71 阅读 · 0 评论 -
二叉树的深度优先遍历与广度优先遍历
//深度优先搜索 类似前序遍历function deepSearch(root,target){ if(root == null) return false; //如果当前节点等于目标返回true if(root.value == target) return true; var left = deepSearch(root.leftTree,target); if(left) return true; var right = deepSearch(root....原创 2021-02-24 20:05:41 · 111 阅读 · 0 评论 -
二叉树还原
跟据前序中序还原二叉树var qian = ['a', 'c', 'f', 'g', 'b', 'd', 'e'];var zhong = ['f', 'c', 'g', 'a', 'd', 'b', 'e'];var hou = ['f', 'g', 'c', 'd', 'e', 'b', 'a']; function Node(value) { this.value = value; this.leftTree = null; this.rightTree原创 2021-02-22 12:41:43 · 140 阅读 · 0 评论 -
[数据结构与算法]选择排序
//比较方式 function compare(a, b) { if (a > b) //正序 a < b逆序 return true; else return false; } //交换 function swap(arr, a, b) { var temp = arr[a]; arr[a] = arr[b]; arr[b] = temp; } //选择排序 function selectS.原创 2021-02-05 00:27:48 · 128 阅读 · 0 评论 -
[数据算法与结构]快速排序
快排的步骤找基准 建立两个序列,一个存放比基准数小的,一个存放比基准数大的 继续对两个序列进行快排.直到序列长度为0或1。简单快排 //比较方式 function compare(a, b) { if (a > b) //正序 a < b逆序 return true; else return false; } //交换 function swap(arr, a, b) { var temp原创 2021-02-05 20:53:10 · 67 阅读 · 0 评论 -
[数据结构与算法]冒泡排序
//比较方式 function compare(a, b) { if (a > b) //正序 a < b逆序 return true; else return false; } //交换 function swap(arr, a, b) { var temp = arr[a]; arr[a] = arr[b]; arr[b] = temp; } function bullerSort(arr) {.原创 2021-02-04 23:24:34 · 58 阅读 · 0 评论 -
[数据结构与算法]链表逆置与遍历
function Node(value){ this.value = value; this.next = null; } var node1 = new Node(1); var node2 = new Node(2); var node3 = new Node(3); var node4 = new Node(4); var node5 = new Node(5); node1.next=node2; node2.next=node3;.原创 2021-02-03 19:49:57 · 169 阅读 · 0 评论 -
[数据结构与算法]二分查找法
在数组中查找一个数var arr = [1, 3, 5, 7, 9]普通方法// 普通方法 function binarySearch(arr , val){ for(var i=0;i<arr.length;i++) if(arr[i]==val) return i; }//ES6方法 funct...原创 2021-01-25 22:26:39 · 88 阅读 · 1 评论