Chinaunix首页 | 论坛 | 博客
  • 博客访问: 209274
  • 博文数量: 136
  • 博客积分: 2919
  • 博客等级: 少校
  • 技术积分: 1299
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-11 09:08
文章分类

全部博文(136)

文章存档

2013年(1)

2011年(135)

我的朋友

分类: Java

2011-12-20 09:51:14

  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> _lmlds = null;    // left-most-leaf-descendants
  13.     private Object[] _keyroots = null; // k either has a left sibling or is the root.
  14.     
  15.     public Tree(Node root) {
  16.         _root = root;
  17.         _nodes = new LinkedList<Node>();
  18.         _lmlds = new LinkedList<Integer>();    
  19.         //_keyroots = new ArrayList();
  20.         LinkedList<Pair> stack = new LinkedList<Pair>();
  21.         LinkedList<Pair> pstack = new LinkedList<Pair>();
  22.         stack.add(new Pair(root, new LinkedList<Integer>()));
  23.         int porder = 0;
  24.         while (stack.size() > 0) {
  25.             
  26.             Pair p = (Pair)stack.removeLast();
  27.             Node n = p.getNode();
  28.             LinkedList<Integer> anc = p.getAncIDs();
  29.             setid(n, porder);
  30.             if (n.isLeaf()) {
  31.                 //stack.add(new Pair(n, anc));
  32.                 pstack.add(new Pair(n, anc));
  33.                 //return;
  34.             }
  35.             else {
  36.                 
  37.                 for (Node c: n.getChildren()) {
  38.                     LinkedList<Integer> a = new LinkedList(anc);
  39.                     a.addFirst(n.getid());
  40.                     stack.addLast(new Pair(c, a));
  41.                     }
  42.                 pstack.addLast(new Pair(n, anc));
  43.                 porder += 1;
  44.                 }
  45.         }
  46.         
  47.         //System.out.println("Here?");
  48.         
  49.         int i = 0;
  50.         
  51.         HashMap lmds = new HashMap();
  52.         HashMap keyroots = new HashMap();
  53.         while (pstack.size() > 0) {
  54.             
  55.             Pair p = (Pair)pstack.removeLast();
  56.             Node n = p.getNode();
  57.      LinkedList<Integer> anc = p.getAncIDs();
  58.             _nodes.add(n);
  59.             System.out.println(n.getValue());
  60.             int lmd;
  61.             if (n.isLeaf()) {
  62.                 lmd = i;
  63.                 for (int a : anc)
  64.                     if (!(lmds.containsKey(a)))
  65.                         lmds.put(a, i);
  66.                     else
  67.                         break;                    
  68.             }
  69.             else
  70.                 lmd = (Integer)lmds.get(n.getid());
  71.             _lmlds.addLast(lmd);
  72.             keyroots.put(lmd, i);
  73.             
  74.             i += 1;
  75.         }
  76.         Object[] c = keyroots.values().toArray();
  77.         Arrays.sort(c);
  78.         _keyroots = c;
  79.         }
  80.     
  81.             
  82.     public void setid(Node n, int id) {
  83.         n.setid(id);
  84.     }
  85.     
  86.     public Node getRoot() {
  87.         return _root;
  88.     }
  89.     
  90.     public LinkedList<Node> getNodes() {
  91.         return _nodes;
  92.     }
  93.     
  94.     public int getNodeCount() {
  95.         int sum = 1;
  96.         Node[] nodes = this.getRoot().getChildren();
  97.         for (Node d : nodes) {
  98.             sum += d.getNumOfDescendants();
  99.         }
  100.         return sum;
  101.     }
  102.     public LinkedList<Integer> getLmds() {
  103.         return _lmlds;
  104.     }
  105.     
  106.     public int[] getKeyRoots() {
  107.         int[] result = new int[_keyroots.length];
  108.         for(int i = 0; i < _keyroots.length; i++)
  109.             result[i] = (Integer)_keyroots[i];
  110.         return result;
  111.     }
  112.     
  113.     public static void main(String[] args) {
  114.         Node f = new Node("root", "f");
  115.         Node d = new Node("left", "d");
  116.         Node e = new Node("right", "e");
  117.         Node a = new Node("lleft", "a");
  118.         Node c = new Node("lright", "c");
  119.         Node b = new Node("ldescendant", "b");
  120.         
  121.         c.addChild(b);
  122.         d.addChild(a);
  123.         d.addChild(c);
  124.         f.addChild(d);
  125.         f.addChild(e);
  126.         Tree T = new Tree(f);
  127.         LinkedList<Node> nodes = T.getNodes();
  128.         
  129.         for (Node n: nodes) {
  130.             System.out.print(n.getValue());
  131.         }
  132.         System.out.println();
  133.         LinkedList<Integer> lmds = T.getLmds();
  134.         System.out.print("Left-most-leaf-descendats are [");
  135.         for (int i: lmds)
  136.             System.out.print(i + " ");
  137.         System.out.print("]");
  138.         
  139.         System.out.println();
  140.         int[] keyroots = T.getKeyRoots();
  141.         System.out.print("Key roots are [ ");
  142.         for (Object o: keyroots)
  143.             System.out.print( o + " ");
  144.         System.out.print("]");
  145.         System.out.println();
  146.         
  147.         
  148.     }
  149.     
  150.     /**
  151.      * A pair is a tuple(root, children) where root is the current
  152.      * node and children is list of the node's child nodes from left to right.
  153.      * @author mht
  154.      *
  155.      */
  156.     class Pair {
  157.         private Node _node = null;
  158.         private LinkedList<Integer> _ancIDs = null;
  159.         
  160.         public Pair(Node root, LinkedList<Integer> ancIDs) {
  161.             _node = root;
  162.             _ancIDs = ancIDs;
  163.         }
  164.         public Node getNode() {
  165.             return _node;
  166.         }
  167.         public LinkedList<Integer> getAncIDs() {
  168.             return _ancIDs;
  169.         }
  170.     }
  171.     
  172. }
阅读(1202) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~