コード例 #1
0
 public void trav(ArrayList nodes, node cur) {
   node temp;
   while (!nodes.isEmpty()) {
     temp = (node) nodes.get(0);
     if (temp.getLevel() - 1 == cur.getLevel()) {
       nodes.remove(0);
       cur.addChild(temp);
       trav(nodes, temp);
     } else {
       return;
     }
   }
 }
コード例 #2
0
  public void draw(node leaf, Graphics2D g, int px, int py) {
    int lvl = leaf.getLevel();
    double l = lvl;
    counts[lvl]++;

    double xfraq = counts[lvl] / (spacing[lvl] + 1);
    double yfraq = l / depth;
    int x = new Double(1600 * xfraq).intValue();
    int y = new Double(1200 * yfraq).intValue() + 10;

    if (leaf.getAttr() != null) {
      g.drawString(leaf.getAttr(), x - 20, y);
    }
    if (leaf.getCrit() != null) {
      g.drawString(leaf.getCrit(), x - 20, y + 10);
    }
    if (leaf.getResult() != null) {
      g.drawString(leaf.getResult(), x - 20, y + 10);
    }
    g.drawLine(x, y, px, py);
    // g.fillRect(x,y,20,20);
    ArrayList children = leaf.getChildren();
    while (!children.isEmpty()) {
      draw((node) children.remove(0), g, x, y);
    }
  }
コード例 #3
0
  public void recreateTree() {
    System.out.println("Recreating tree");
    ArrayList nodes = new ArrayList();
    String attr, crit, result;
    int level;
    node leaf;
    while (!list.isEmpty()) {
      if (((String) list.remove(0)).compareTo("Node") == 0) {
        leaf = new node();
        attr = (String) list.remove(0);
        // System.out.println("ATTR:"+attr);
        crit = (String) list.remove(0);
        try {
          level = (new Double(crit)).intValue();
          crit = null;
        } catch (Exception e) {
          // System.out.println("crit:"+crit);
          level = (new Double((String) list.remove(0))).intValue();
        }
        // System.out.println("lvl:"+level);
        if (level > depth) {
          depth = level;
        }
        if (((String) list.get(0)).compareTo("Node") != 0) {
          result = (String) list.remove(0);
          leaf.setResult(result);
        }
        leaf.setSplitCriteria(crit, 0);
        leaf.setLevel(level);
        leaf.setAttr(attr);

        if (attr.compareTo("_root") == 0) {
          root = leaf;
        } else {

          nodes.add(leaf);
        }
      }
    }
    depth++;
    levelCount(nodes);

    System.out.println("Linking " + nodes.size() + " tree nodes");
    trav(nodes, root);
  }
コード例 #4
0
 public void levelCount(ArrayList nodes) {
   System.out.println("Doing level counts");
   spacing = new double[depth];
   for (int i = 0; i < depth; i++) {
     spacing[i] = 0;
   }
   spacing[0]++;
   node leaf;
   for (int i = 0; i < nodes.size(); i++) {
     leaf = (node) nodes.get(i);
     spacing[leaf.getLevel()]++;
   }
   max = 0;
   for (int i = 0; i < depth; i++) {
     if (spacing[i] > max) {
       max = spacing[i];
     }
     // System.out.println("Level "+i+" has "+spacing[i]+" nodes");
   }
 }