Author: srowen
Date: Tue Nov 15 08:49:13 2011
New Revision: 1202094

URL: http://svn.apache.org/viewvc?rev=1202094&view=rev
Log:
MAHOUT-886 avoid adding child nodes several times

Modified:
    
mahout/trunk/core/src/main/java/org/apache/mahout/fpm/pfpgrowth/fpgrowth/FPGrowth.java
    
mahout/trunk/core/src/main/java/org/apache/mahout/fpm/pfpgrowth/fpgrowth/FPTree.java

Modified: 
mahout/trunk/core/src/main/java/org/apache/mahout/fpm/pfpgrowth/fpgrowth/FPGrowth.java
URL: 
http://svn.apache.org/viewvc/mahout/trunk/core/src/main/java/org/apache/mahout/fpm/pfpgrowth/fpgrowth/FPGrowth.java?rev=1202094&r1=1202093&r2=1202094&view=diff
==============================================================================
--- 
mahout/trunk/core/src/main/java/org/apache/mahout/fpm/pfpgrowth/fpgrowth/FPGrowth.java
 (original)
+++ 
mahout/trunk/core/src/main/java/org/apache/mahout/fpm/pfpgrowth/fpgrowth/FPGrowth.java
 Tue Nov 15 08:49:13 2011
@@ -563,7 +563,12 @@ public class FPGrowth<A extends Comparab
         }
 
         if (prevConditional != -1) { // if there is a child element
-          conditionalTree.setParent(prevConditional, conditional);
+          int prevParent = conditionalTree.parent(prevConditional);
+          if (prevParent == -1) {
+            conditionalTree.setParent(prevConditional, conditional);
+               } else if (prevParent != conditional) {
+                 throw new IllegalStateException();
+          }
         }
 
         conditionalTree.addCount(conditional, nextNodeCount);
@@ -572,12 +577,16 @@ public class FPGrowth<A extends Comparab
         pathNode = tree.parent(pathNode);
 
       }
+
       if (prevConditional != -1) {
-        conditionalTree.setParent(prevConditional, FPTree.ROOTNODEID);
-        if (conditionalTree.childCount(FPTree.ROOTNODEID) > 1
-            && conditionalTree.singlePath()) {
+        int prevParent = conditionalTree.parent(prevConditional);
+        if (prevParent == -1) {
+          conditionalTree.setParent(prevConditional, FPTree.ROOTNODEID);
+             } else if (prevParent != FPTree.ROOTNODEID) {
+          throw new IllegalStateException();
+        }
+        if (conditionalTree.childCount(FPTree.ROOTNODEID) > 1 && 
conditionalTree.singlePath()) {
           conditionalTree.setSinglePath(false);
-
         }
       }
       conditionalNode = tree.next(conditionalNode);

Modified: 
mahout/trunk/core/src/main/java/org/apache/mahout/fpm/pfpgrowth/fpgrowth/FPTree.java
URL: 
http://svn.apache.org/viewvc/mahout/trunk/core/src/main/java/org/apache/mahout/fpm/pfpgrowth/fpgrowth/FPTree.java?rev=1202094&r1=1202093&r2=1202094&view=diff
==============================================================================
--- 
mahout/trunk/core/src/main/java/org/apache/mahout/fpm/pfpgrowth/fpgrowth/FPTree.java
 (original)
+++ 
mahout/trunk/core/src/main/java/org/apache/mahout/fpm/pfpgrowth/fpgrowth/FPTree.java
 Tue Nov 15 08:49:13 2011
@@ -409,4 +409,33 @@ public class FPTree {
     System.arraycopy(oldProperties, 0, this.headerTableProperties, 0,
       headerTableCount);
   }
+
+  private void toStringHelper(StringBuilder sb, int currNode, String prefix) {
+    if (childCount[currNode] == 0) {
+      sb.append(prefix).append("-{attr:").append(attribute[currNode])
+        .append(", id: ").append(currNode)
+        .append(", cnt:").append(nodeCount[currNode]).append("}\n");
+    } else {
+      StringBuilder newPre = new StringBuilder(prefix);
+      newPre.append("-{attr:").append(attribute[currNode])
+        .append(", id: ").append(currNode)
+        .append(", cnt:").append(nodeCount[currNode]).append('}');
+      StringBuilder fakePre = new StringBuilder();
+      while (fakePre.length() < newPre.length()) {
+        fakePre.append(' ');
+      }
+      for (int i = 0; i < childCount[currNode]; i++) {
+        toStringHelper(sb, nodeChildren[currNode][i], (i == 0 ? newPre : 
fakePre).toString() + '-' + i + "->");
+      }
+    }
+  }
+  
+  @Override
+  public String toString() {
+    StringBuilder sb = new StringBuilder("[FPTree\n");
+    toStringHelper(sb, 0, "  ");
+    sb.append("\n]\n");
+    return sb.toString();
+  }
+
 }


Reply via email to