Skip to content

Commit b54a499

Browse files
author
Ralf Gommers
committed
Merge branch 'pr/3394' into master. Closes scipygh-2177.
See scipy#3394
2 parents 10bdec2 + 49533d3 commit b54a499

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

scipy/cluster/hierarchy.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -795,8 +795,8 @@ def pre_order(self, func=(lambda x: x.id)):
795795
n = self.count
796796

797797
curNode = [None] * (2 * n)
798-
lvisited = np.zeros((2 * n,), dtype=bool)
799-
rvisited = np.zeros((2 * n,), dtype=bool)
798+
lvisited = set()
799+
rvisited = set()
800800
curNode[0] = self
801801
k = 0
802802
preorder = []
@@ -807,13 +807,13 @@ def pre_order(self, func=(lambda x: x.id)):
807807
preorder.append(func(nd))
808808
k = k - 1
809809
else:
810-
if not lvisited[ndid]:
810+
if ndid not in lvisited:
811811
curNode[k + 1] = nd.left
812-
lvisited[ndid] = True
812+
lvisited.add(ndid)
813813
k = k + 1
814-
elif not rvisited[ndid]:
814+
elif ndid not in rvisited:
815815
curNode[k + 1] = nd.right
816-
rvisited[ndid] = True
816+
rvisited.add(ndid)
817817
k = k + 1
818818
# If we've visited the left and right of this non-leaf
819819
# node already, go up in the tree.

scipy/cluster/tests/test_hierarchy.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,15 @@ def test_leaves_list_iris_average(self):
813813
node = to_tree(Z)
814814
self.assertTrue((node.pre_order() == leaves_list(Z)).all())
815815

816+
def test_iris_subtree_pre_order(self):
817+
# Tests that pre_order() works when called on sub-trees.
818+
X = eo['iris']
819+
Y = pdist(X)
820+
Z = linkage(X, 'single')
821+
node = to_tree(Z)
822+
self.assertTrue(node.pre_order() == (node.get_left().pre_order()
823+
+ node.get_right().pre_order()))
824+
816825

817826
class TestCorrespond(TestCase):
818827
def test_correspond_empty(self):

0 commit comments

Comments
 (0)