-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
251 lines (182 loc) · 6.54 KB
/
test.js
File metadata and controls
251 lines (182 loc) · 6.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
const algorm=require("./algorm")
// stack
var stack_t = new algorm.stack([1,2,3])
var stack_t2 = new algorm.stack([1,2,4])
stack_t.push(1); // 在最后插入
console.log("stcak_t.push(1): ",stack_t)
console.log("stcak_t.top(): ",stack_t.top()) //第一个元素
stack_t.emplace(4); //在最前插入
console.log("stcak_t.emplace(4): ",stack_t)
stack_t.pop() // 弹出最后一个元素
console.log("stack_t.pop(): ",stack_t)
stack_t.clear() //清除
console.log("stack_t.clear(): ",stack_t,"\nstcak_t.size(): ",stack_t.size,"\nstack_t.empty()",stack_t.empty())
stack_t.swap(stack_t2); // 交换
console.log("stack_t.swap(stack_t2): \n stcak_t: ",stack_t,"\n stcak_t2: ",stack_t2,"\n\n")
// queue
var queue_t = new algorm.queue([1,2,3])
var queue_t2 = new algorm.queue([1,2,4])
queue_t.push(1); // 在最后插入
console.log("queue_t.push(1): ",queue_t)
console.log("queue_t.front(): ",queue_t.front()) //首个元素
console.log("queue_t.back(): ",queue_t.back()) //末个元素
queue_t.pop() // 弹出第一个元素
console.log("queue_t.pop(): ",queue_t)
console.log("queue_t.size(): ",queue_t.size,"\nqueue_t.empty(): ",queue_t.empty())
queue_t.swap(queue_t2);
console.log("queue_t.swap(queue_t2): \n queue_t: ",queue_t,"\n queue_t2: ",queue_t2,"\n\n")
//linkedlist
var linkedlist1 = new algorm.linkedlist()
linkedlist1.append("A")
linkedlist1.append("B")
linkedlist1.append("C")
console.log(".append('A')/'B'/'C' : ",linkedlist1)
console.log("linkedlist1.toString(): ",linkedlist1.toString());
linkedlist1.insert(0, "123");
linkedlist1.insert(2, "456");
console.log(".insert(0,'123')/(2,'456') : ",linkedlist1.toString());
console.log("linkedlist1.getData(0): ",linkedlist1.getData(0));
console.log("linkedlist1.getData(1): ",linkedlist1.getData(1));
linkedlist1.update(0, "12345");
console.log(linkedlist1.toString());
linkedlist1.update(1, "54321");
console.log(linkedlist1.toString());
// 测试 removeAt 方法
linkedlist1.removeAt(3);
console.log(linkedlist1.toString());
// 测试 remove 方法
linkedlist1.remove("CC");
console.log(linkedlist1.toString());
// 测试 isEmpty 方法
console.log(linkedlist1.isEmpty());
// 测试 size 方法
console.log(linkedlist1.size());
//doublylinkedlist
const doublyLinkedList = new algorm.doublylinkedlist();
// append() 测试
doublyLinkedList.append("ZZ");
doublyLinkedList.append("XX");
doublyLinkedList.append("CC");
console.log(doublyLinkedList);
// insert() 测试
doublyLinkedList.insert(0, "00");
doublyLinkedList.insert(2, "22");
console.log(doublyLinkedList);
// getData() 测试
console.log(doublyLinkedList.getData(1)); //--> ZZ
// indexOf() 测试
console.log(doublyLinkedList.indexOf("XX")); //--> 3
console.log(doublyLinkedList);
// removeAt() 测试
doublyLinkedList.removeAt(0);
doublyLinkedList.removeAt(1);
console.log(doublyLinkedList);
// update() 测试
doublyLinkedList.update(0, "111111");
console.log(doublyLinkedList);
// remove() 测试
doublyLinkedList.remove("111111");
doublyLinkedList.remove("22222");
console.log(doublyLinkedList);
// forwardToString() 测试
console.log(doublyLinkedList.forwardToString());
// backwardString() 测试
console.log(doublyLinkedList.backwardString(),"\n\n");
//Set
const set = new algorm.set();
// add() 测试
set.add("abc");
set.add("abc");
set.add("123");
set.add("zxc");
console.log(set); //--> {items: {123: "123", abc: "abc", zxc: "zxc"}}
// has() 测试
console.log(set.has("123")); //--> true
console.log(set.has("456")); //--> false
// remove() 测试
set.remove("abc");
console.log(set); //--> {items: {123: "123", zxc: "zxc"}}
// size() 测试
console.log(set.size()); //--> 2
// values() 测试
console.log(set.values()); //--> ["123", "zxc"]
// clear() 测试
set.clear();
console.log(set.values(),"\n\n"); //--> []
// hash
let hTable = new algorm.hash(137);
let someWords = ["detect", "investigate", "scold", "difficulty",
"complication", "complexity", "intricacy", "detail", "contents"];
let definition = [`discover or identify the presence or existence of.`,
`carry out a systematic or formal inquiry to discover and examine the
facts of (an incident, allegation, etc.) so as to establish the truth.`,
`remonstrate with or rebuke (someone) angrily.`,
`the state or condition of being difficult.`,
`a circumstance that complicates something; a difficulty.`,
`the state or quality of being intricate or complicated.`,
`the quality of being intricate.`,
`an individual feature, fact, or item.`,
`a state of satisfaction.`];
// 线性探查法
for (let i = 0; i < someWords.length; ++i) {
hTable.putWithDete(definition[i], someWords[i]);
}
console.log(hTable.getWithDete('detect'));
// 开链法
let chains = new algorm.hash(137);
chains.buildChains();
for(let i=0; i<someWords.length; i++){
chains.putWithChains(definition[i], someWords[i]);
}
console.log(chains.getWithChains('investigate'),"\n\n");
// tree
let preOrder = [20, 13, 7, 9, 15, 14, 42, 22, 21, 24, 57];
let inOrder = [7, 9, 13, 14, 15, 20, 21, 22, 24, 42, 57];
let inLstIdx = inOrder.length - 1;
let preLstIdx = preOrder.length - 1;
let myTree = new algorm.tree();
myTree.preInCreate(preOrder, inOrder, 0, preLstIdx, 0, inLstIdx);
console.log(myTree.inOrderNonRec());
console.log(myTree.preOrderNonRec());
console.log(myTree.postOrderNonRec());
myTree.remove(20)
console.log(myTree.postOrderNonRec());
console.log(myTree.getMaxNode())
console.log(myTree.getMinNode())
console.log(myTree.isAVLTree())
console.log(myTree.isCompleteTree())
console.log(myTree.isFullTree())
console.log(myTree.getLevelNodeNumber(3))
console.log(myTree.getNodeLevel(7))
console.log(myTree.getTreeWidth())
console.log(myTree.getTreeDepth())
console.log(myTree.getLeafNodeNumber())
console.log(myTree.getEdgeNumber())
console.log(myTree.getNodeNumber())
console.log(myTree.find(7),"\n\n")
// algorithm
//huffman
var str = 'ew qew qd ef 24 gf ewr getElementsByTagName';
var huffman = new algorm.huffman(str)
huffman.encode(str)
huffman.decode();
huffman.encodeBase64();
//kmp
console.log(algorm.kmp("ABABCABAA", "AA"));
//lcs
console.log(algorm.lcs("mdrrrrr","idr"));
//lcss
console.log(algorm.lcss("mdrgra","mdra"));
//lsd
console.log(algorm.lsd('xinglan', 'mdr'))
//rabinkarp
const text = "Hello, world!";
const pattern = "world";
console.log(algorm.rabinkarp(text, pattern));
//sort
var arr=[3,4,1,0,-9,2,3,5,6,7,5,4,3,2,5,6,7,9,0,8,11,3545,6,7,67,8,87,5,1,2,3,546,45,43,2,45,7,889,0,86,7,6];
var arr1=[3,4,1,0,-9,2,3,5,6,7,5,4,3,2,5,6,7,9,0,8,11,3545,6,7,67,8,87,5,1,2,3,546,45,43,2,45,7,889,0,86,7,6];
algorm.sort.shellSort(arr,1);
console.log(arr);
algorm.sort.quickSort(arr1);
console.log(arr1)