Skip to content

Commit e71ccf6

Browse files
author
YuChengKai
committed
并查集
1 parent 9b53893 commit e71ccf6

File tree

4 files changed

+93
-9
lines changed

4 files changed

+93
-9
lines changed

Algorithm/algorithm-ch.md

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
- [树的深度](#%E6%A0%91%E7%9A%84%E6%B7%B1%E5%BA%A6)
2929
- [动态规划](#%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92)
3030
- [斐波那契数列](#%E6%96%90%E6%B3%A2%E9%82%A3%E5%A5%91%E6%95%B0%E5%88%97)
31+
- [最长递增子序列](#%E6%9C%80%E9%95%BF%E9%80%92%E5%A2%9E%E5%AD%90%E5%BA%8F%E5%88%97)
3132

3233
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
3334

DataStruct/dataStruct-zh.md

+68-8
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,33 @@
33
**Table of Contents** *generated with [DocToc](https://github.com/thlorenz/doctoc)*
44

55
- [](#%E6%A0%88)
6-
- [原理](#%E5%8E%9F%E7%90%86)
6+
- [概念](#%E6%A6%82%E5%BF%B5)
77
- [实现](#%E5%AE%9E%E7%8E%B0)
88
- [应用](#%E5%BA%94%E7%94%A8)
99
- [队列](#%E9%98%9F%E5%88%97)
10-
- [原理](#%E5%8E%9F%E7%90%86-1)
10+
- [概念](#%E6%A6%82%E5%BF%B5-1)
1111
- [实现](#%E5%AE%9E%E7%8E%B0-1)
1212
- [单链队列](#%E5%8D%95%E9%93%BE%E9%98%9F%E5%88%97)
1313
- [循环队列](#%E5%BE%AA%E7%8E%AF%E9%98%9F%E5%88%97)
1414
- [链表](#%E9%93%BE%E8%A1%A8)
15-
- [原理](#%E5%8E%9F%E7%90%86-2)
15+
- [概念](#%E6%A6%82%E5%BF%B5-2)
1616
- [实现](#%E5%AE%9E%E7%8E%B0-2)
1717
- [](#%E6%A0%91)
1818
- [二叉树](#%E4%BA%8C%E5%8F%89%E6%A0%91)
1919
- [二分搜索树](#%E4%BA%8C%E5%88%86%E6%90%9C%E7%B4%A2%E6%A0%91)
2020
- [实现](#%E5%AE%9E%E7%8E%B0-3)
21+
- [Trie](#trie)
22+
- [概念](#%E6%A6%82%E5%BF%B5-3)
23+
- [实现](#%E5%AE%9E%E7%8E%B0-4)
24+
- [并查集](#%E5%B9%B6%E6%9F%A5%E9%9B%86)
25+
- [概念](#%E6%A6%82%E5%BF%B5-4)
26+
- [实现](#%E5%AE%9E%E7%8E%B0-5)
2127

2228
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
2329

2430
#
2531

26-
## 原理
32+
## 概念
2733

2834
栈是一个线性结构,在计算机中是一个相当常见的数据结构。
2935

@@ -90,7 +96,7 @@ var isValid = function (s) {
9096

9197
# 队列
9298

93-
## 原理
99+
## 概念
94100

95101
队列一个线性结构,特点是在某一端添加数据,在另一端删除数据,遵循先进先出的原则。
96102

@@ -193,7 +199,7 @@ class SqQueue {
193199

194200
# 链表
195201

196-
## 原理
202+
## 概念
197203

198204
链表是一个线性结构,同时也是一个天然的递归结构。链表结构可以充分利用计算机内存空间,实现灵活的内存动态管理。但是链表失去了数组随机读取的优点,同时链表由于增加了结点的指针域,空间开销比较大。
199205

@@ -557,7 +563,7 @@ _delect(node, v) {
557563

558564
# Trie
559565

560-
## 原理
566+
## 概念
561567

562568
在计算机科学,**trie**,又称**前缀树****字典树**,是一种有序树,用于保存关联数组,其中的键通常是字符串。
563569

@@ -640,9 +646,63 @@ class Trie {
640646

641647
# 并查集
642648

643-
## 原理
649+
## 概念
644650

651+
并查集是一种特殊的树结构,用于处理一些不交集的合并及查询问题。该结构中每个节点都有一个父节点,如果只有当前一个节点,那么该节点的父节点指向自己。
645652

653+
这个结构中有两个重要的操作,分别是:
654+
655+
- Find:确定元素属于哪一个子集。它可以被用来确定两个元素是否属于同一子集。
656+
- Union:将两个子集合并成同一个集合。
657+
658+
![](https://user-gold-cdn.xitu.io/2018/6/9/163e45b56fd25172?w=421&h=209&f=png&s=26545)
646659

647660
## 实现
648661

662+
```js
663+
class DisjointSet {
664+
// 初始化样本
665+
constructor(count) {
666+
// 初始化时,每个节点的父节点都是自己
667+
this.parent = new Array(count)
668+
// 用于记录树的深度,优化搜索复杂度
669+
this.rank = new Array(count)
670+
for (let i = 0; i < count; i++) {
671+
this.parent[i] = i
672+
this.rank[i] = 1
673+
}
674+
}
675+
find(p) {
676+
// 寻找当前节点的父节点是否为自己,不是的话表示还没找到
677+
// 开始进行路径压缩优化
678+
// 假设当前节点父节点为 A
679+
// 将当前节点挂载到 A 节点的父节点上,达到压缩深度的目的
680+
while (p != this.parent[p]) {
681+
this.parent[p] = this.parent[this.parent[p]]
682+
p = this.parent[p]
683+
}
684+
return p
685+
}
686+
isConnected(p, q) {
687+
return this.find(p) === this.find(q)
688+
}
689+
// 合并
690+
union(p, q) {
691+
// 找到两个数字的父节点
692+
let i = this.find(p)
693+
let j = this.find(q)
694+
if (i === j) return
695+
// 判断两棵树的深度,深度小的加到深度大的树下面
696+
// 如果两棵树深度相等,那就无所谓怎么加
697+
if (this.rank[i] < this.rank[j]) {
698+
this.parent[i] = j
699+
} else if (this.rank[i] > this.rank[j]) {
700+
this.parent[j] = i
701+
} else {
702+
this.parent[i] = j
703+
this.rank[j] += 1
704+
}
705+
}
706+
}
707+
```
708+

Framework/framework-zh.md

+17
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
2+
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
3+
**Table of Contents** *generated with [DocToc](https://github.com/thlorenz/doctoc)*
4+
5+
- [Virtual Dom](#virtual-dom)
6+
- [为什么需要 Virtual Dom](#%E4%B8%BA%E4%BB%80%E4%B9%88%E9%9C%80%E8%A6%81-virtual-dom)
7+
- [Virtual Dom 算法简述](#virtual-dom-%E7%AE%97%E6%B3%95%E7%AE%80%E8%BF%B0)
8+
- [Virtual Dom 算法实现](#virtual-dom-%E7%AE%97%E6%B3%95%E5%AE%9E%E7%8E%B0)
9+
- [树的递归](#%E6%A0%91%E7%9A%84%E9%80%92%E5%BD%92)
10+
- [判断属性的更改](#%E5%88%A4%E6%96%AD%E5%B1%9E%E6%80%A7%E7%9A%84%E6%9B%B4%E6%94%B9)
11+
- [判断列表差异算法实现](#%E5%88%A4%E6%96%AD%E5%88%97%E8%A1%A8%E5%B7%AE%E5%BC%82%E7%AE%97%E6%B3%95%E5%AE%9E%E7%8E%B0)
12+
- [遍历子元素打标识](#%E9%81%8D%E5%8E%86%E5%AD%90%E5%85%83%E7%B4%A0%E6%89%93%E6%A0%87%E8%AF%86)
13+
- [渲染差异](#%E6%B8%B2%E6%9F%93%E5%B7%AE%E5%BC%82)
14+
- [最后](#%E6%9C%80%E5%90%8E)
15+
16+
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
17+
118
# Virtual Dom
219

320
[代码地址](https://github.com/KieSun/My-wheels/tree/master/Virtual%20Dom)

JS/JS-en.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,19 @@
33
**Table of Contents** *generated with [DocToc](https://github.com/thlorenz/doctoc)*
44

55
- [Built-in Types](#built-in-types)
6-
- [Typeof](#typeof)
76
- [Type Conversion](#type-conversion)
87
- [Converting to Boolean](#converting-to-boolean)
98
- [Objects to Primitive Types](#objects-to-primitive-types)
109
- [Arithmetic Operators](#arithmetic-operators)
1110
- [`==` operator](#-operator)
1211
- [Comparison Operator](#comparison-operator)
12+
- [Typeof](#typeof)
13+
- [Type Conversion](#type-conversion-1)
14+
- [Converting to Boolean](#converting-to-boolean-1)
15+
- [Objects to Primitive Types](#objects-to-primitive-types-1)
16+
- [Arithmetic Operators](#arithmetic-operators-1)
17+
- [`==` operator](#-operator-1)
18+
- [Comparison Operator](#comparison-operator-1)
1319
- [New](#new)
1420
- [This](#this)
1521
- [Instanceof](#instanceof)

0 commit comments

Comments
 (0)