Skip to content

Commit d5d0bec

Browse files
committed
dir structure change
1 parent 1d6cd76 commit d5d0bec

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+297
-357
lines changed

BinaryTree/EBTNode.js

-30
This file was deleted.

karma.conf.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ var PATHS = {
99
'dist/Binary tree/*.js',
1010
'dist/Queue/*.js',
1111
'dist/Stack/*.js',
12-
'Binary tree/BinaryTree-spec.js'
12+
'./src/BinaryTree/BinaryTree-spec.js'
1313
]
1414
};
1515

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
"description": "data structure",
55
"main": "index.js",
66
"scripts": {
7-
"build": "set NODE_ENV=production&&webpack",
8-
"build:dev": "set NODE_ENV=development&&webpack",
7+
"build": "set NODE_ENV=production&&webpack --progress --colors",
8+
"build:dev": "set NODE_ENV=development&&webpack --progress --colors",
99
"test": "echo \"Error: no test specified\" && exit 1"
1010
},
1111
"repository": {

Array/CrossList.js src/Array/CrossList.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@
1010

1111
// 稀疏矩阵的十字链表存储表示
1212
class OLNode {
13-
constructor(i, j, e) {
13+
constructor(i = 0, j = 0, e) {
1414
// 该非零元的行和列下标
15-
this.i = i || 0;
16-
this.j = j || 0;
15+
this.i = i;
16+
this.j = j;
1717
this.e = e;
1818
// 该非零元所在行表和列表的后继链域
1919
this.right = null; // type: OLNode
2020
this.down = null; // type: OLNode
2121
}
2222
}
2323

24-
class CrossList {
24+
export default class CrossList {
2525
constructor() {
2626
// 行和列链表头指针向量基址由CreateSMatrix分配
2727
this.rhead = [];

Array/TSMatrix.js src/Array/TSMatrix.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Triple {
1111
}
1212
}
1313

14-
class TSMatrix {
14+
export class TSMatrix {
1515
constructor(mu, nu) {
1616
// 非零元三元组表
1717
this.data = [];
@@ -124,7 +124,7 @@ console.log(matrix.fastTransposeSMatrix());
124124
* 为此可将快速转置矩阵的算法中创建的,指示“行”信息的辅助数组cpot固定在稀疏矩阵的存储结构中。
125125
* 称这种“带行链接信息”的三元组表为行逻辑链接的顺序表
126126
*/
127-
class RLSMatrix extends TSMatrix {
127+
export class RLSMatrix extends TSMatrix {
128128
constructor(){
129129
super(...arguments);
130130
this.rpos = [0];
File renamed without changes.

BinaryTree/BinaryThreadTree.js src/BinaryTree/BinaryThreadTree.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
const LINK = 0;
3737
const THREAD = 1;
3838

39-
export class BinaryThreadTree_inOrder {
39+
export default class BinaryThreadTree_inOrder {
4040
// 二叉树中序线索化
4141
static inOrderThreading(tree) {
4242
var threadTree = new this.constructor();
File renamed without changes.
File renamed without changes.

src/BinaryTree/EBTNode.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// 有mark域和双亲指针域的二叉树结点类型
2+
export default class EBTNode {
3+
constructor() {
4+
this.data = null;
5+
this.leftChild = null;
6+
this.rightChild = null;
7+
this.parent = null;
8+
this.mark = 0;
9+
}
10+
11+
postOrder_nonrecursive_nonstack(visit) {
12+
var p = this;
13+
while (p) {
14+
switch (p.mark) {
15+
case 0:
16+
p.mark = 1;
17+
if (p.leftChild) p = p.leftChild;
18+
break;
19+
case 1:
20+
p.mark = 2;
21+
if (p.rightChild) p = p.rightChild;
22+
break;
23+
case 2:
24+
p.data && visit(p.data);
25+
p.mark = 0; // 恢复mark域
26+
p = p.parent; // 返回双亲结点
27+
}
28+
}
29+
}
30+
}
File renamed without changes.
File renamed without changes.

BinaryTree/huffManCoding.js src/BinaryTree/huffManCoding.js

+4-61
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,6 @@ export function huffManCoding(weights) {
4242
return [huffmanTree, hc];
4343
}
4444

45-
function huffManCoding2(weights) {
46-
let n = weights.length;
47-
if (n < 1) return;
48-
49-
let huffmanTree = buildHuffmanTree(weights, n);
50-
51-
// 从叶子到根逆向求每个字符的赫夫曼编码
52-
let hc = calcHuffmanCode2(huffmanTree, n);
53-
54-
return [huffmanTree, hc];
55-
}
5645

5746
function calcHuffmanCode(huffmanTree, n) {
5847
// 从叶子到根逆向求每个字符的赫夫曼编码
@@ -71,63 +60,20 @@ function calcHuffmanCode(huffmanTree, n) {
7160
return hc;
7261
}
7362

74-
// 无栈非递归遍历赫夫曼树
75-
// todo 该算法有问题
76-
function calcHuffmanCode2(huffmanTree, n) {
77-
let p = 2 * n - 2;
78-
let hc = [];
79-
let cd = [];
80-
let cdLen = 0;
81-
82-
// 遍历赫夫曼树时用作结点状态标识
83-
for (let i = 0; i <= p; i++) huffmanTree[i].weight = 0;
84-
85-
while (p) {
86-
// 向左
87-
if (huffmanTree[p].weight === 0) {
88-
huffmanTree[p].weight = 1;
89-
if (huffmanTree[p].leftChild !== 0) {
90-
p = huffmanTree[p].leftChild;
91-
cd[cdLen++] = '0';
92-
}
93-
// 登记叶子结点的字符的编码
94-
else if (huffmanTree[p].rightChild === 0) {
95-
hc[p - 1] = cd.join('');
96-
}
97-
}
98-
// 向右
99-
else if (huffmanTree[p].weight === 1) {
100-
huffmanTree[p].weight = 2;
101-
if (huffmanTree[p].rightChild !== 0) {
102-
p = huffmanTree[p].rightChild;
103-
cd[cdLen++] = '1';
104-
}
105-
}
106-
// huffmanTree[p].weight == 2,退回
107-
// 退到父节点,编码长度减一
108-
else {
109-
huffmanTree[p].weight = 0;
110-
p = huffmanTree[p].parent;
111-
--cdLen;
112-
}
113-
}
114-
115-
return hc;
116-
}
117-
11863
// 创建一棵叶子结点数为n的Huffman树
11964
function buildHuffmanTree(weights, n) {
12065
n = n || weights.length;
12166
let m = 2 * n - 1;
12267
let huffmanTree = [];
12368

12469
// 初始化
125-
for (let i = 0; i < n; i++)
70+
let i;
71+
for (i = 0; i < n; i++)
12672
huffmanTree[i] = new HuffmanNode(weights[i], 0, 0, 0);
12773
for (; i < m; i++)
12874
huffmanTree[i] = new HuffmanNode(0, 0, 0, 0);
12975

130-
for (i = n; i < m; i++) {
76+
for (let i = n; i < m; i++) {
13177
// 在HT[1..i-1]选择parent为0且weight最小的两个结点,返回其序号为[s1, s2]
13278
let ret = select(huffmanTree, i);
13379
let s1 = ret[0];
@@ -177,7 +123,4 @@ function select(huffmanTree, len) {
177123
}
178124

179125
console.log('-------huffman coding 1:------');
180-
console.log(huffManCoding([5, 29, 7, 8, 14, 23, 3, 11]));
181-
182-
console.log('\n-------huffman coding 2:------');
183-
console.log(huffManCoding2([5, 29, 7, 8, 14, 23, 3, 11]));
126+
console.log(huffManCoding([5, 29, 7, 8, 14, 23, 3, 11]));
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)