Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit b44b282

Browse files
committed
code: 新增结构
1 parent b27e784 commit b44b282

File tree

28 files changed

+2356
-23
lines changed

28 files changed

+2356
-23
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# No.110 平衡二叉树
2+
3+
给定一个二叉树,判断它是否是高度平衡的二叉树。
4+
5+
本题中,一棵高度平衡二叉树定义为:
6+
7+
一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1。
8+
9+
## 示例
10+
11+
示例 1:
12+
13+
给定二叉树 [3,9,20,null,null,15,7]
14+
15+
3
16+
/ \
17+
9 20
18+
/ \
19+
15 7
20+
返回 true 。
21+
22+
示例 2:
23+
24+
给定二叉树 [1,2,2,3,3,null,null,4,4]
25+
26+
1
27+
/ \
28+
2 2
29+
/ \
30+
3 3
31+
/ \
32+
4 4
33+
返回 false 。
34+
35+
## 解题思路
36+
37+
### 1、自顶向下发
38+
39+
遍历每一个节点,计算节点的左右子树高度。
40+
41+
```javascript
42+
var isBalanced = function(root) {
43+
if (root == null) return true;
44+
return (Math.abs(leftHeight(root) - rightHeight(root)) <= 1) && isBalanced(root.left) && isBalanced(root.right)
45+
};
46+
47+
var leftHeight = function(node) {
48+
if (!node) return 0;
49+
return height(node.left) + 1;
50+
}
51+
52+
var rightHeight = function(node) {
53+
if (!node) return 0;
54+
return height(node.right) + 1;
55+
}
56+
57+
var height = function(node) {
58+
return Math.max(leftHeight(node), rightHeight(node));
59+
}
60+
```
61+
62+
### 2、自底向上
63+
64+
对二叉树做深度优先遍历DFS,递归过程中,当我们发现有一例 左/右子树高度差 > 1 的情况时,代表此树不是平衡树,返回-1;
65+
当发现不是平衡树时,后面的高度计算都没有意义了,因此一路返回-1,避免后续多余计算。
66+
最差情况是对树做一遍完整DFS,时间复杂度为 O(N)。
67+
68+
```javascript
69+
var isBalanced = function(root) {
70+
return depth(root) != -1;
71+
};
72+
73+
var depth = function(root) {
74+
if (!root) return 0;
75+
let left = depth(root.left);
76+
if (left == -1) return -1;
77+
let right = depth(root.right);
78+
if (right == -1) return -1;
79+
return Math.abs(left - right) < 2 ? Math.max(left, right) + 1 : -1;
80+
}
81+
```
Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
1-
module.exports = {
2-
env: {
3-
browser: true,
4-
es6: true,
5-
},
6-
extends: [
7-
'airbnb-base',
8-
],
9-
globals: {
10-
Atomics: 'readonly',
11-
SharedArrayBuffer: 'readonly',
12-
},
13-
parserOptions: {
14-
ecmaVersion: 2018,
15-
sourceType: 'module',
16-
},
17-
rules: {
18-
},
19-
};
1+
{
2+
"root": true,
3+
"extends": "airbnb",
4+
"plugins": ["jest"],
5+
"env": {
6+
"jest/globals": true
7+
},
8+
"rules": {
9+
"no-bitwise": "off",
10+
"no-lonely-if": "off",
11+
"class-methods-use-this": "off",
12+
"arrow-body-style": "off",
13+
"no-loop-func": "off"
14+
}
15+
}

算法与数据结构学习/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
- [堆栈](data-structures/stack)
99
- [哈希表](data-structures/hash-table)
1010
- [](data-structures/heap)
11-
- [有限队列](data-structures/priority-queue)
11+
- [优先队列](data-structures/priority-queue)
12+
- [字典树](data-structures/trie)
1213

1314
## 算法
1415

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function shellSort(arr) {
2+
var len = arr.length,
3+
temp,
4+
gap = Math.floor(len / 2);
5+
while (gap > 0) {
6+
for (var i = gap; i < len; i++) {
7+
temp = arr[i];
8+
for (var j = i-gap; j >= 0 && arr[j] > temp; j -= gap) {
9+
arr[j+gap] = arr[j];
10+
}
11+
arr[j+gap] = temp;
12+
}
13+
gap = Math.floor(gap / 2);
14+
}
15+
return arr;
16+
}
17+
18+
console.log(shellSort([5,2,4,1,3]))
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export default class InsertionSort {
2+
sort(originalArray) {
3+
const array = [...originalArray];
4+
5+
for (let i = 1; i < array.length; i++) {
6+
7+
let currentIdx = i;
8+
9+
while ((array[currentIdx] < array[currentIdx - 1]) && currentIdx > 0) {
10+
const tmp = array[currentIdx - 1];
11+
array[currentIdx - 1] = array[currentIdx];
12+
array[currentIdx] = tmp;
13+
currentIdx--;
14+
}
15+
}
16+
return array;
17+
}
18+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# 插入排序
2+
3+
插入排序是一种构建简单的排序算法,最终排序的数组(或列表)一次一个。在大型列表上,效率要比在大型列表上低得多
4+
高级算法,例如快速排序,堆排序或合并排序。
5+
6+
7+
![Algorithm Visualization](https://upload.wikimedia.org/wikipedia/commons/4/42/Insertion_sort.gif)
8+
9+
![Algorithm Visualization](https://upload.wikimedia.org/wikipedia/commons/0/0f/Insertion-sort-example-300px.gif)
10+
11+
## 复杂度
12+
13+
| 名称 | 最优 | 平均 | 最差 | Memory | Stable | Comments |
14+
| --------------------- | :-------------: | :-----------------: | :-----------------: | :-------: | :-------: | :-------- |
15+
| **Insertion sort** | n | n<sup>2</sup> | n<sup>2</sup> | 1 | Yes | |
16+
17+
## References
18+
19+
[Wikipedia](https://en.wikipedia.org/wiki/Insertion_sort)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import InsertionSort from '../InsertionSort';
2+
3+
import {
4+
equalArr,
5+
notSortedArr,
6+
reverseArr,
7+
sortedArr,
8+
SortTester,
9+
} from '../../SortTester';
10+
11+
describe('HeapSort', () => {
12+
it('should sort array', () => {
13+
SortTester.testSort(InsertionSort);
14+
});
15+
16+
it('should sort negative numbers', () => {
17+
SortTester.testNegativeNumbersSort(InsertionSort);
18+
});
19+
20+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export default class QuickSortInPlace extends Sort {
2+
sort(
3+
originalArray,
4+
inputLowIndex = 0,
5+
inputHighIndex = originalArray.length - 1,
6+
recursiveCall = false,
7+
) {
8+
// 复制一份数组,然后对array进行原地排序
9+
const array = recursiveCall ? originalArray : [...originalArray];
10+
11+
const partitionArray = (lowIndex, highIndex) => {
12+
const swap = (leftIndex, rightIndex) => {
13+
const temp = array[leftIndex];
14+
array[leftIndex] = array[rightIndex];
15+
array[rightIndex] = temp;
16+
}
17+
18+
const pivot = array[highIndex];
19+
}
20+
}
21+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function quickSort(arr, low, high) {
2+
// 调整数组
3+
let i = adjustArr(arr, low, high);
4+
// 分治
5+
quickSort(arr, low, i-1);
6+
quickSort(arr, i+1, high);
7+
}
8+
9+
function adjustArr(arr, low, high) {
10+
let pivot = arr[low];
11+
12+
while (low < high) {
13+
while (arr[high] > pivot)
14+
high--;
15+
arr[low++] = arr[high];
16+
while (arr[low] < pivot)
17+
low++;
18+
arr[high--] = arr[low];
19+
}
20+
21+
arr[low] = pivot;
22+
return low;
23+
}
24+
25+
let arr = [72,6,57,88,60,42,83,73,48,85];
26+
quickSort(arr, 0, arr.length-1);
27+
console.log(arr);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
function quickSort(arr, low, high) {
2+
if (low < high) {
3+
// 调整数组
4+
let i = adjustArr(arr, low, high);
5+
// 分治
6+
quickSort(arr, low, i-1);
7+
quickSort(arr, i+1, high);
8+
}
9+
}
10+
11+
function adjustArr(arr, low, high) {
12+
let pivot = arr[low];
13+
14+
while (low < high) {
15+
while (low < high && arr[high] > pivot)
16+
high--;
17+
arr[low] = arr[high];
18+
while (low < high && arr[low] < pivot)
19+
low++;
20+
arr[high] = arr[low];
21+
}
22+
23+
arr[low] = pivot;
24+
return low;
25+
}
26+
27+
let arr = [72,6,57,88,60,42,83,73,48,85];
28+
quickSort(arr, 0, arr.length-1);
29+
console.log(arr);

算法与数据结构学习/data-structures/hash-table/HashTable.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ export default class HashTable {
2626
0,
2727
);
2828

29-
console.log(hash);
3029

3130

3231

0 commit comments

Comments
 (0)