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

全部博文(136)

文章存档

2013年(1)

2011年(135)

我的朋友

分类: Java

2011-12-20 08:17:19

  1. /**
  2. Author: mht
  3. Date: Dc 12, 2011
  4. */

  5. package org.mht.ted;

  6. import java.util.LinkedList;
  7. import java.util.Queue;
  8. import java.util.Iterator;

  9. public class Node {

  10.     private int _id = -1;
  11.     private int _start ; // internal array position of left most leaf descendant of the root node
  12.     private int _nodeCount ; // number of nodes
  13.     private int _numOfChildren = 0;
  14.     private int _numOfDescendants = 0;
  15.     private int[] _lmld = null; // left-most-leaf-descendants
  16.     private String[] _labels = null;
  17.     private static int _postID = -1;
  18.     
  19.     private static LinkedList _postOrder = new LinkedList();
  20.     
  21.     private String _tag = null;
  22.     private String _value = null;
  23.     private Node[] _children = null;
  24.     
  25.     
  26.     
  27.     public Node(String tag, String value) {
  28.         _tag = tag;
  29.         _value = value;
  30.         //_children = new Node[];
  31.         }
  32.     
  33.     public Node (Node node) {
  34.         this._start = 0;
  35.         this._nodeCount = node.getNodeCount();
  36.         this._lmld = new int[_start + _nodeCount];
  37.         this._labels = new String[_start + _nodeCount];
  38.         int k = 1;
  39.         postTraverse(node);
  40.         Iterator i = _postOrder.iterator();
  41.         while (i.hasNext()) {
  42.             Node c = (Node)i.next();
  43.             c.setPostID(k);
  44.             this.setLeftMostLeafDescendant(k, c.getFirstLeaf().getPostID());
  45.             k++;
  46.         }
  47.         }
  48.     
  49.     public int getPostID() {
  50.         return _postID;
  51.     }
  52.     
  53.     public void setPostID(int i) {
  54.         _postID = i;
  55.     }
  56.      public void addChild(Node child) {
  57.          // define a new bigger array for children and copy the old ones
  58.         Node[] newChildren = new Node[_numOfChildren+1];
  59.          if ( _numOfChildren > 0)
  60.              System.arraycopy(_children, 0 , newChildren, 0,_numOfChildren);
  61.          newChildren[ _numOfChildren] = child; // add the new son
  62.          _children = newChildren;
  63.         
  64.          _numOfDescendants += child.getNumOfDescendants() + 1;
  65.          _numOfChildren ++;
  66.          }    
  67.     
  68.      public int getNodeCount() {
  69.          int sum = 1;
  70.          Node[] nodes = getChildren();
  71.          for (Node d : nodes){
  72.              if (d.isLeaf())
  73.                  sum += 1;
  74.              else
  75.                  sum += d.getNodeCount();
  76.          }        
  77.          return sum;
  78.      }
  79.     
  80.      public boolean isLeaf() {
  81.          return this.getNumOfDescendants() == 0;
  82.      }
  83.     
  84.      public Node getFirstLeaf() {
  85.          if (this.isLeaf()) return this;
  86.          else {
  87.              Node[] nodes = this.getChildren();
  88.          for (Node d: nodes) {
  89.              if (d.isLeaf())
  90.                  return d;
  91.              else
  92.                  return d.getFirstLeaf();
  93.              }        
  94.          }
  95.          return this;
  96.      }
  97.     
  98.      public int getLeftMostLeafDescendant(int i) {
  99.          return _lmld[i+_start-1] - _start + 1;
  100.      }
  101.     
  102.      public void setLeftMostLeafDescendant(int i, int newLld) {
  103.          _lmld[i+_start-1] = newLld + _start - 1;
  104.          if (_nodeCount < i)
  105.              _nodeCount = i;
  106.      }
  107.     
  108.     public int getNumOfDescendants() {
  109.         return _numOfDescendants;
  110.     }
  111.     
  112.     public Node[] getChildren() {
  113.         return _children;
  114.     }
  115.     
  116.     public String getValue() {
  117.         return _value;
  118.     }
  119.     
  120.     public String getTag() {
  121.         return _tag;
  122.     }
  123.     
  124.     public int getid() {
  125.         return _id;
  126.     }
  127.     public void setid(int id) {
  128.         this._id = id;
  129.     }
  130.     public static void postTraverse(Node node) {
  131.         if (node.isLeaf()) {
  132.             _postOrder.add(node);
  133.             return;
  134.         }
  135.         else {
  136.             Node[] nodes = new Node[node.getNodeCount()];
  137.             nodes = node.getChildren();
  138.             int k = 1;
  139.             for (int i = 0; i < nodes.length; i++) {
  140.                 Node c = nodes[i];
  141.                 postTraverse(c);
  142.                 }
  143.          _postID = k;
  144.             _postOrder.add(node);
  145.             k += 1;
  146.             }
  147.     }
  148.     
  149.     /**
  150.      * to produce a list of nodes in post-order traverse a tree rooted at root
  151.      * reference to Tim Henderson and Steve Johnson's Compare.py for tree edit distance implementation.
  152.      *
  153.      * @param root
  154.      * @return
  155.      */
  156.     public static LinkedList postTraverseV1(Node root) {
  157.         LinkedList<Node> result = new LinkedList<Node>();
  158.         LinkedList<Node> stack = new LinkedList<Node>();
  159.         LinkedList<Node> pstack = new LinkedList<Node>();
  160.         stack.add(root);
  161.         while (stack.size() > 0) {
  162.             Node n = (Node) stack.removeLast();
  163.             if (n.isLeaf()) pstack.add(n);
  164.             else {
  165.                 for (Node c: n.getChildren())
  166.                     stack.add(c);
  167.                 pstack.add(n);
  168.                 }
  169.         }
  170.         
  171.         while (pstack.size() > 0) {
  172.             Node n = (Node) pstack.removeLast();
  173.             result.add(n);
  174.             //System.out.println(n.getValue());
  175.         }
  176.         return result;
  177.     }
  178.     
  179.         
  180.         
  181.     
  182.     
  183.     public static void main(String[] args) {
  184.         Node a = new Node("6", "a");
  185.         Node d = new Node("3", "d");
  186.         Node c = new Node("4", "c");
  187.         Node b = new Node("5", "b");
  188.         Node f = new Node("1", "f");
  189.         Node e = new Node("2", "e");
  190.         Node g = new Node("7", "g");
  191.         
  192.         d.addChild(f);
  193.         d.addChild(e);
  194.         a.addChild(d);
  195.         a.addChild(c);
  196.         a.addChild(b);
  197.         a.addChild(g);
  198.         
  199.         Node[] children = e.getChildren();
  200.         //System.out.println(children[0]);
  201.      // postTraverse(a);
  202.      LinkedList<Node> porder = new LinkedList<Node>();
  203.      porder = postTraverseV1(a);
  204.      for (Node x : porder)
  205.          System.out.println(x.getValue());
  206.     
  207.     
  208.      // Node root = new Node(a);
  209.      //int lmld = root.getLeftMostLeafDescendant(1);
  210.      // System.out.println(lmld);
  211.     }
  212.         
  213.     
  214. }
阅读(539) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~