-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathclass_MultiTree Data Structure.ahk
435 lines (348 loc) · 7.72 KB
/
class_MultiTree Data Structure.ahk
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
;================Tree Node & Linked List================
;===============Multi-Tree Data Structure===============
;== ==
;===https://autohotkey.com/board/topic/147436-multi-tree-data-structure/====
;=== http://www.autohotkey.com/board/topic/131314-linked-list-in-ahk/=====
;== ==
;================Author: Za3root=================
;============AutoHotkey Version: 1.1.19.03 ==============
;==You can contact the author on reddit (/u/za3root) or on AutoHotkey Forums===
;== ==
;=====This to classes are collected and combined on 16.04.2018=========
;=========Node & Linked List==============
class Node {
data := ""
next := ""
__New(data)
{
this.data := data
}
}
class LinkedList {
first := ""
;-----------Adding Items-------
addFirst(a) {
; Adds an item to the front of the list.
; Parameters:
; "a" : The item to insert
p := new Node(a)
if !this.first
this.first := p
else
{
p.next := this.first
this.first := p
}
}
;-------------------
addLast(a) {
; Adds an item to the back of the list.
; Parameters:
; "a" : The item to insert
p := new Node(a)
if !this.first
this.first := p
else
{
current := this.first
while(not !current)
{
if (!current.next)
{
current.next := p
break
}
else
current := current.next
}
}
}
;-------------------
addAt(a,v) {
; Adds an item at a specific location in the list.
; Parameters:
; "a" : The index to append the item.
; "v" : The item to insert.
p := new Node(v)
current := this.first
counter := 1
while(not !current)
{
if (counter + 1 = a)
{
p.next := current.next
current.next := p
break
}
else
{
counter++
current := current.next
}
}
}
;---------------Removing Items---------------
removeFirst() {
; Removes an item from the front of the list.
tempN := this.first
this.first := this.first.next
tempN.next := ""
return % tempN.data
}
;-------------------
removeLast() {
; Removes an item from the back of the list.
current := this.first
while(not !current)
{
if (!current.next.next)
{
tempN := current.next
current.next := ""
return % tempN.data
}
else
current := current.next
}
}
;-------------------
removeAt(a) {
; Removes an item from a specific location in the list.
; Parameters:
; "a" : The index to remove the item.
current := this.first
counter := 1
while(not !current)
{
if (counter + 1 = a)
{
p := current.next
current.next := p.next
p.next := ""
return % p.data
}
else
{
counter++
current := current.next
}
}
}
;---------------Other---------------
clear() {
; Empties the list.
current := this.first
while(not !current)
{
current.data := ""
current := current.next
}
}
;-------------------
get(a) {
; Retrieves an item from the list. Returns "-1" if the element didn't exist.
; Parameters:
; "a" : The index of the item to be retrieved.
current := this.first
counter := 1
while(not !current)
{
if (counter = a)
return % current.data
else
{
counter++
current := current.next
}
}
return -1
}
;-------------------
contains(v) {
; Checks if an element exists in a list. It returns the number of occurrences of the specific element. It returns "0" if the element doesn't exist.
; Parameters:
; "v" : The value of the element.
current := this.first
occurrence := 0
while(not !current)
{
if (current.data = v)
occurrence++
current := current.next
}
return % occurrence
}
;-------------------
location(v,occ := "f") {
; Retrieves the location of the first/last occurrence of the element. It returns "-1" if the element doesn't exist.
; Parameters:
; "v" : The value of the element.
; "occ" : Whether it retrieves the location of the first or last occurrence of the element. It is first by default.
current := this.first
loc := -1
counter := 1
while(not !current)
{
if (current.data = v)
{
loc := counter
if ( occ = "f")
break
}
current := current.next
counter++
}
return % loc
}
;I've added the following methods.
length() {
; returns the length of the linked list
current := this.first
counter := 0
while(not !current)
{
counter++
current := current.next
}
return % counter
}
toArray() {
; converts this linked list to an array
array := [""]
Loop, % this.length()
Array.Insert(this.get(A_index))
return % array
}
fromArray(array) {
; Populates this linked list from an array
; Parameters:
; array: the array to populate the linked list
Loop, % array.MaxIndex()
{
this.addLast(array[A_Index])
}
}
}
;========TreeNode & MultiTree============
; You can use an array instead of a linked list
class TNode {
parent := ""
data := ""
children := new LinkedList()
childrenNb := 0
__New(data)
{
this.data := data
}
setData(data)
; Sets the data of the node
; Parameters:
; data: the data for the node to hold
{
this.data := data
}
add(data)
{
node := new TNode(data)
this.addNode(node)
}
addNode(node)
; Adds a node to the node's children
; Parameters:
; node: the node to add
{
node.parent := this
this.children.addFirst(node)
this.childrenNb++
}
getChild(index)
; Retrives a childe node
; Parameters:
; index: the index of the node in the children
{
if(index <= this.childrenNb and index > 0)
return this.children.get(index)
else
return -1
}
removeChild(index)
; Deletes a childe node
; Parameters:
; index: the index of the node in the children
{
this.children.RemoveAt(index)
this.childrenNb--
}
isLeaf()
; Checks if the node has no children
; Parameters:
; index: the index of the node in the children
{
if(this.childrenNb = 0)
return true
else
return false
}
}
class MultiTree {
;===================================================
; AutoHotkey Version: 1.1.19.03
; Author: Za3root
; Contact: You can contact me on reddit (/u/za3root) or on AutoHotkey Forums
; Date(dd/mm/yyyy): 7/28/2015
;===================================================
root := new TNode("")
path := new LinkedList
setRootData(data)
; Sets the data of the root node
; Parameters:
; data: the data for the node to hold
{
this.root.data := data
}
search(data)
; Searches the tree for a node holding a specific value then returns it
; Parameters:
; data: the data to search for
{
return % this.searchAlt(data,this.root)
}
searchAlt(data, node)
; this method is used by the search() method
{
if !node
return
if(node.data = data)
return node
else
Loop, % node.childrenNb
{
tempNode := node.children.get(A_Index)
current := this.searchAlt(data, tempNode)
if(current.data = data)
return current
}
return -1
}
getPathToRoot(node)
; after using this method, this.path will hold nodes from the specific node to the root
{
this.path.clear()
this.getPath(node)
}
getPath(node)
; this method id used by the getPathToRoot() method
{
global
parent := node.parent
if(!parent)
{
return
}
else
{
this.path.addFirst(parent)
this.getPath(parent)
}
}
}