@@ -347,10 +347,15 @@ function prettyprintstring(node::T, spaces::Int = 0) where {T<:AbstractTreeNode}
347
347
end
348
348
349
349
export getnodelist
350
- function getnodelist (node:: T , nodelist:: Array{T,1} = T[]) where {T<: AbstractTreeNode }
351
- push! (nodelist, node)
352
- for childnode in node. children # Fixing this to avoid implementing iterate(::FelNode)
353
- getnodelist (childnode, nodelist)
350
+ function getnodelist (node:: T ) where {T<: AbstractTreeNode }
351
+ nodelist = []
352
+ nodes = [node]
353
+ while nodes != []
354
+ node = pop! (nodes)
355
+ push! (nodelist, node)
356
+ for child in node. children
357
+ push! (nodes, child)
358
+ end
354
359
end
355
360
return nodelist
356
361
end
@@ -385,23 +390,34 @@ function treedepth(node::T) where {T<:AbstractTreeNode}
385
390
end
386
391
387
392
export getnonleaflist
388
- function getnonleaflist (node:: T , nonleaflist:: Array{T,1} = T[]) where {T<: AbstractTreeNode }
389
- if ! isleafnode (node)
390
- push! (nonleaflist, node)
391
- end
392
- for childnode in node. children
393
- getnonleaflist (childnode, nonleaflist)
393
+ function getnonleaflist (node:: T ) where {T<: AbstractTreeNode }
394
+ nonleaflist = []
395
+ nodes = [node]
396
+ while nodes != []
397
+ node = pop! (nodes)
398
+ if node. children != []
399
+ push! (nonleaflist, node)
400
+ for child in node. children
401
+ push! (nodes, child)
402
+ end
403
+ end
394
404
end
395
405
return nonleaflist
396
406
end
397
407
398
408
export getleaflist
399
- function getleaflist (node:: T , leaflist:: Array{T,1} = T[]) where {T<: AbstractTreeNode }
400
- if isleafnode (node)
401
- push! (leaflist, node)
402
- end
403
- for childnode in node. children
404
- getleaflist (childnode, leaflist)
409
+ function getleaflist (node:: T ) where {T<: AbstractTreeNode }
410
+ leaflist = []
411
+ nodes = [node]
412
+ while nodes != []
413
+ node = pop! (nodes)
414
+ if node. children == []
415
+ push! (leaflist, node)
416
+ else
417
+ for child in node. children
418
+ push! (nodes, child)
419
+ end
420
+ end
405
421
end
406
422
return leaflist
407
423
end
@@ -432,16 +448,45 @@ function ladderize(tree::T) where {T<:AbstractTreeNode}
432
448
end
433
449
434
450
function ladderize! (tree:: T ) where {T<: AbstractTreeNode }
451
+ child_counts = countchildren (tree)
435
452
for node in getnodelist (tree)
436
- if length (node. children) != 0
437
- sort! (
438
- node. children,
439
- lt = (x, y) -> length (getnodelist (x)) < length (getnodelist (y)),
440
- )
453
+ if ! isempty (node. children)
454
+ sort! (node. children, lt = (x, y) -> child_counts[x] < child_counts[y])
441
455
end
442
456
end
443
457
end
444
458
459
+ # Creates a dictionary of all the child counts (including the node itself) which can then be used by ladderize to sort the nodes
460
+ function countchildren (tree:: T ) where {T<: AbstractTreeNode }
461
+ # Initialize the dictionary to store the number of children for each node
462
+ children_count = Dict {T, Int} ()
463
+
464
+ # Initialize the stack for DFS
465
+ stack = [tree]
466
+
467
+ # Initialize a list to keep track of the post-order traversal
468
+ post_order = []
469
+
470
+ # First pass: Perform DFS and store the nodes in post-order
471
+ while ! isempty (stack)
472
+ node = pop! (stack)
473
+ push! (post_order, node)
474
+ for child in node. children
475
+ push! (stack, child)
476
+ end
477
+ end
478
+
479
+ # Second pass: Calculate the number of children for each node in post-order
480
+ for node in reverse (post_order)
481
+ count = 0
482
+ for child in node. children
483
+ count += 1 + children_count[child]
484
+ end
485
+ children_count[node] = count
486
+ end
487
+
488
+ return children_count
489
+ end
445
490
446
491
function getorder (tree:: T ) where {T<: AbstractTreeNode }
447
492
return [node. seqindex for node in getleaflist (tree)]
0 commit comments