From 6993c97dfaf469833676d65357d7f0865e4d58e4 Mon Sep 17 00:00:00 2001 From: Tito Cheriachan Date: Mon, 9 Jan 2017 13:32:17 -0500 Subject: [PATCH] added a covenience method getAllLeafNodes --- src/main/java/net/vivin/GenericTreeNode.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/main/java/net/vivin/GenericTreeNode.java b/src/main/java/net/vivin/GenericTreeNode.java index b973355..a063436 100755 --- a/src/main/java/net/vivin/GenericTreeNode.java +++ b/src/main/java/net/vivin/GenericTreeNode.java @@ -71,6 +71,18 @@ public void removeChildAt(int index) throws IndexOutOfBoundsException { public GenericTreeNode getChildAt(int index) throws IndexOutOfBoundsException { return children.get(index); } + + public Set> getAllLeafNodes() { + Set> leafNodes = new HashSet>(); + if (this.children.isEmpty()) { + leafNodes.add(this); + } else { + for (GenericTreeNode child : this.children) { + leafNodes.addAll(child.getAllLeafNodes()); + } + } + return leafNodes; + } public T getData() { return this.data;