From 4ff42c7f53435aff03918f416ef17795cd816bad Mon Sep 17 00:00:00 2001 From: Artem Kanunnykov Date: Tue, 28 Apr 2020 19:36:24 +0200 Subject: [PATCH 1/2] Fix printing order Balanced list of b branch is mixed, because b.append(..) is used. b.insert(0, ..) should be used instead. --- pptree/pptree.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pptree/pptree.py b/pptree/pptree.py index 76abd4b..9f6c771 100644 --- a/pptree/pptree.py +++ b/pptree/pptree.py @@ -33,7 +33,7 @@ def balanced_branches(current_node): a = sorted(children(current_node), key=lambda node: nb_children(node)) b = [] while a and sum(size_branch[node] for node in b) < sum(size_branch[node] for node in a): - b.append(a.pop()) + b.insert(0, a.pop()) return a, b From 27477ce50ca0638027864a394239ab21adb14fc5 Mon Sep 17 00:00:00 2001 From: Artem Kanunnykov Date: Wed, 29 Apr 2020 11:35:50 +0200 Subject: [PATCH 2/2] Fix printing order No need to sort children before balancing. The order should stay untouched. --- pptree/pptree.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pptree/pptree.py b/pptree/pptree.py index 9f6c771..b5e2dc2 100644 --- a/pptree/pptree.py +++ b/pptree/pptree.py @@ -30,7 +30,7 @@ def balanced_branches(current_node): size_branch = {child: nb_children(child) for child in children(current_node)} """ Creation of balanced lists for "a" branch and "b" branch. """ - a = sorted(children(current_node), key=lambda node: nb_children(node)) + a = children(current_node) b = [] while a and sum(size_branch[node] for node in b) < sum(size_branch[node] for node in a): b.insert(0, a.pop())