- /**
-
Author: mht
-
Date: Dc 12, 2011
-
*/
-
-
package org.mht.ted;
-
-
import java.util.LinkedList;
-
import java.util.Queue;
-
import java.util.Iterator;
-
-
public class Node {
-
-
private int _id = -1;
-
private int _start ; // internal array position of left most leaf descendant of the root node
-
private int _nodeCount ; // number of nodes
-
private int _numOfChildren = 0;
-
private int _numOfDescendants = 0;
-
private int[] _lmld = null; // left-most-leaf-descendants
-
private String[] _labels = null;
-
private static int _postID = -1;
-
-
private static LinkedList _postOrder = new LinkedList();
-
-
private String _tag = null;
-
private String _value = null;
-
private Node[] _children = null;
-
-
-
-
public Node(String tag, String value) {
-
_tag = tag;
-
_value = value;
-
//_children = new Node[];
-
}
-
-
public Node (Node node) {
-
this._start = 0;
-
this._nodeCount = node.getNodeCount();
-
this._lmld = new int[_start + _nodeCount];
-
this._labels = new String[_start + _nodeCount];
-
int k = 1;
-
postTraverse(node);
-
Iterator i = _postOrder.iterator();
-
while (i.hasNext()) {
-
Node c = (Node)i.next();
-
c.setPostID(k);
-
this.setLeftMostLeafDescendant(k, c.getFirstLeaf().getPostID());
-
k++;
-
}
-
}
-
-
public int getPostID() {
-
return _postID;
-
}
-
-
public void setPostID(int i) {
-
_postID = i;
-
}
-
public void addChild(Node child) {
-
// define a new bigger array for children and copy the old ones
-
Node[] newChildren = new Node[_numOfChildren+1];
-
if ( _numOfChildren > 0)
-
System.arraycopy(_children, 0 , newChildren, 0,_numOfChildren);
-
newChildren[ _numOfChildren] = child; // add the new son
-
_children = newChildren;
-
-
_numOfDescendants += child.getNumOfDescendants() + 1;
-
_numOfChildren ++;
-
}
-
-
public int getNodeCount() {
-
int sum = 1;
-
Node[] nodes = getChildren();
-
for (Node d : nodes){
-
if (d.isLeaf())
-
sum += 1;
-
else
-
sum += d.getNodeCount();
-
}
-
return sum;
-
}
-
-
public boolean isLeaf() {
-
return this.getNumOfDescendants() == 0;
-
}
-
-
public Node getFirstLeaf() {
-
if (this.isLeaf()) return this;
-
else {
-
Node[] nodes = this.getChildren();
-
for (Node d: nodes) {
-
if (d.isLeaf())
-
return d;
-
else
-
return d.getFirstLeaf();
-
}
-
}
-
return this;
-
}
-
-
public int getLeftMostLeafDescendant(int i) {
-
return _lmld[i+_start-1] - _start + 1;
-
}
-
-
public void setLeftMostLeafDescendant(int i, int newLld) {
-
_lmld[i+_start-1] = newLld + _start - 1;
-
if (_nodeCount < i)
-
_nodeCount = i;
-
}
-
-
public int getNumOfDescendants() {
-
return _numOfDescendants;
-
}
-
-
public Node[] getChildren() {
-
return _children;
-
}
-
-
public String getValue() {
-
return _value;
-
}
-
-
public String getTag() {
-
return _tag;
-
}
-
-
public int getid() {
-
return _id;
-
}
-
public void setid(int id) {
-
this._id = id;
-
}
-
public static void postTraverse(Node node) {
-
if (node.isLeaf()) {
-
_postOrder.add(node);
-
return;
-
}
-
else {
-
Node[] nodes = new Node[node.getNodeCount()];
-
nodes = node.getChildren();
-
int k = 1;
-
for (int i = 0; i < nodes.length; i++) {
-
Node c = nodes[i];
-
postTraverse(c);
-
}
-
_postID = k;
-
_postOrder.add(node);
-
k += 1;
-
}
-
}
-
-
/**
-
* to produce a list of nodes in post-order traverse a tree rooted at root
-
* reference to Tim Henderson and Steve Johnson's Compare.py for tree edit distance implementation.
-
*
-
* @param root
-
* @return
-
*/
-
public static LinkedList postTraverseV1(Node root) {
-
LinkedList<Node> result = new LinkedList<Node>();
-
LinkedList<Node> stack = new LinkedList<Node>();
-
LinkedList<Node> pstack = new LinkedList<Node>();
-
stack.add(root);
-
while (stack.size() > 0) {
-
Node n = (Node) stack.removeLast();
-
if (n.isLeaf()) pstack.add(n);
-
else {
-
for (Node c: n.getChildren())
-
stack.add(c);
-
pstack.add(n);
-
}
-
}
-
-
while (pstack.size() > 0) {
-
Node n = (Node) pstack.removeLast();
-
result.add(n);
-
//System.out.println(n.getValue());
-
}
-
return result;
-
}
-
-
-
-
-
-
public static void main(String[] args) {
-
Node a = new Node("6", "a");
-
Node d = new Node("3", "d");
-
Node c = new Node("4", "c");
-
Node b = new Node("5", "b");
-
Node f = new Node("1", "f");
-
Node e = new Node("2", "e");
-
Node g = new Node("7", "g");
-
-
d.addChild(f);
-
d.addChild(e);
-
a.addChild(d);
-
a.addChild(c);
-
a.addChild(b);
-
a.addChild(g);
-
-
Node[] children = e.getChildren();
-
//System.out.println(children[0]);
-
// postTraverse(a);
-
LinkedList<Node> porder = new LinkedList<Node>();
-
porder = postTraverseV1(a);
-
for (Node x : porder)
-
System.out.println(x.getValue());
-
-
-
// Node root = new Node(a);
-
//int lmld = root.getLeftMostLeafDescendant(1);
-
// System.out.println(lmld);
-
}
-
-
- }