Tree Edit Distance - Left-Most-Descendant Method

917阅读 0评论2011-12-20 maunix
分类:Java

  1. /**
  2.  * mht Dec 19, 2011
  3.  * reference Tim Henderson and Steve Johnson's test_tree.py
  4.  * Tree.java is rewrite it into Java code.
  5.  */

  6. package org.mht.ted;

  7. import java.util.*;

  8. public class Tree {
  9.     private Node _root = null;
  10. //    private int _id = -1;
  11.     private LinkedList<Node> _nodes = null;
  12.     private LinkedList<Integer> _lmds = null;    
  13.     private Node[] _keyRoots = null;
  14.     
  15.     public Tree(Node root) {
  16.         _root = root;
  17.         _nodes = new LinkedList<Node>();
  18.         _lmds = new LinkedList<Integer>();    
  19.         LinkedList<Pair> stack = new LinkedList<Pair>();
  20.         LinkedList<Pair> pstack = new LinkedList<Pair>();
  21.         stack.add(new Pair(root, new LinkedList<Integer>()));
  22.         int porder = 0;
  23.         while (stack.size() > 0) {
  24.             
  25.             Pair p = (Pair)stack.removeLast();
  26.             Node n = p.getNode();
  27.             LinkedList<Integer> anc = p.getAncIDs();
  28.             setid(n, porder);
  29.             if (n.isLeaf()) {
  30.                 //stack.add(new Pair(n, anc));
  31.                 pstack.add(new Pair(n, anc));
  32.                 //return;
  33.             }
  34.             else {
  35.                 
  36.                 for (Node c: n.getChildren()) {
  37.                     LinkedList<Integer> a = new LinkedList(anc);
  38.                     a.addFirst(n.getid());
  39.                     stack.addLast(new Pair(c, a));
  40.                     }
  41.                 pstack.addLast(new Pair(n, anc));
  42.                 porder += 1;
  43.                 }
  44.         }
  45.         
  46.         //System.out.println("Here?");
  47.         
  48.         int i = 0;
  49.         
  50.         HashMap lmds = new HashMap();
  51.         while (pstack.size() > 0) {
  52.             
  53.             Pair p = (Pair)pstack.removeLast();
  54.             Node n = p.getNode();
  55.      LinkedList<Integer> anc = p.getAncIDs();
  56.             _nodes.add(n);
  57.             System.out.println(n.getValue());
  58.             int lmd;
  59.             if (n.isLeaf()) {
  60.                 lmd = i;
  61.                 for (int a : anc)
  62.                     if (!(lmds.containsKey(a)))
  63.                         lmds.put(a, i);
  64.                     else
  65.                         break;                    
  66.             }
  67.             else
  68.                 lmd = (Integer)lmds.get(n.getid());
  69.             //System.out.print(lmd);
  70.             _lmds.addLast(lmd);
  71.             i += 1;
  72.             }
  73.         }
  74.             
  75.     public void setid(Node n, int id) {
  76.         n.setid(id);
  77.     }
  78.     
  79.     public Node getRoot() {
  80.         return _root;
  81.     }
  82.     
  83.     public LinkedList<Node> getNodes() {
  84.         return _nodes;
  85.     }
  86.     
  87.     public int getNodeCount() {
  88.         int sum = 1;
  89.         Node[] nodes = this.getRoot().getChildren();
  90.         for (Node d : nodes) {
  91.             sum += d.getNumOfDescendants();
  92.         }
  93.         return sum;
  94.     }
  95.     public LinkedList<Integer> getLmds() {
  96.         return _lmds;
  97.     }
  98.     
  99.     public static void main(String[] args) {
  100.         Node f = new Node("root", "f");
  101.         Node d = new Node("left", "d");
  102.         Node e = new Node("right", "e");
  103.         Node a = new Node("lleft", "a");
  104.         Node c = new Node("lright", "c");
  105.         Node b = new Node("ldescendant", "b");
  106.         
  107.         c.addChild(b);
  108.         d.addChild(a);
  109.         d.addChild(c);
  110.         f.addChild(d);
  111.         f.addChild(e);
  112.         Tree T = new Tree(f);
  113.         LinkedList<Node> nodes = T.getNodes();
  114.         
  115.         for (Node n: nodes) {
  116.             System.out.print(n.getValue());
  117.         }
  118.         System.out.println();
  119.         LinkedList<Integer> lmds = T.getLmds();
  120.         for (int i: lmds)
  121.             System.out.print(i);
  122.     }
  123.     
  124.     /**
  125.      * A pair is a tuple(root, children) where root is the current
  126.      * node and children is list of the node's child nodes from left to right.
  127.      * @author mht
  128.      *
  129.      */
  130.     class Pair {
  131.         private Node _node = null;
  132.         private LinkedList<Integer> _ancIDs = null;
  133.         
  134.         public Pair(Node root, LinkedList<Integer> ancIDs) {
  135.             _node = root;
  136.             _ancIDs = ancIDs;
  137.         }
  138.         public Node getNode() {
  139.             return _node;
  140.         }
  141.         public LinkedList<Integer> getAncIDs() {
  142.             return _ancIDs;
  143.         }
  144.     }
  145.     
  146. }
上一篇:exam3.3.6
下一篇:Tree Edit Distance - Node Class