Wednesday, July 16, 2008

Find Largest Sub Tree


import java.util.ArrayList;

import java.util.HashMap;
import java.util.Map;

/**
*
* This class find out the largest subtree from the Tree
* nodeMap Store the node and it weight value
* rootNode Root node
*
* Class is not thread safe
*
* @author mamun
* @Date 15-07-2008
* @version 1.0.0
*
*/
public class SubTreeLarestFound {

private Map nodeMap = new HashMap();
public Node rootNode;

public void initData() {
Node t1 = new Node(1);
Node t2 = new Node(2);
Node t3 = new Node(3);
Node t4 = new Node(4);
Node t5 = new Node(5);
Node t6 = new Node(6);
Node t7 = new Node(7);

Node t8 = new Node(18);
Node t9 = new Node(9);

t1.addNode(t2);
t1.addNode(t3);
t1.addNode(t8);

t2.addNode(t5);
t2.addNode(t7);

t3.addNode(t4);
t3.addNode(t6);
t6.addNode(t8);
t6.addNode(t9);

rootNode = t2;
Node largestNode = findLargestSubtree(rootNode);
System.out.println("The node value is "+largestNode.value);

}




/**
* Get the tree and Return the Largest SubTree Node
* It is recursive function if node has child node then it call itself and start
* return after reach to the child node
* nodeMap store the node value and its weight
* largestNode Largest sub tree node
*
*
*
* @param currentNode -Root Node of the Tree
* @return Node - Larest SubTree Node
*/
public Node findLargestSubtree(Node currentNode) {

nodeMap.put(currentNode, currentNode.value); //put weight for each node

if (currentNode.getChilden().length > 0) { //Yes if Node has child node

int totalweight = 0; // Total weight for each SubTree
for (Node childNode : currentNode.getChilden()) {
Node node = findLargestSubtree(childNode); // Find the child node weight(recursive call)
totalweight += nodeMap.get(node); // Add Child node weight to the total weight
}
totalweight += nodeMap.get(currentNode);
nodeMap.put(currentNode, totalweight); //Update Total wight for SubTree

if (rootNode == currentNode) { // True for root Node find the largest weight
Node largestNode = null;
for (Node node : nodeMap.keySet()) {
if (node != rootNode) {
if (largestNode == null) {
largestNode = node;
}
if (nodeMap.get(node) > nodeMap.get(largestNode)) {
largestNode = node; //set largest subtree
}
}
}
return largestNode; //return largest subtree node
}
return currentNode;
} else {
return currentNode;
}
}

public static void main(String[] argv) {
SubTreeLarestFound test = new SubTreeLarestFound();
test.initData();
}

/**
* Node class of the Tree
* It's store its child node and wieght value
* Each Node must have a value but may not child node
* value store the node value
* Childen store the child node
* ZERO_NODE represent the zero node of the Node
* @author mamun
*
*/
public static class Node {

private ArrayList Childen; //child node
private int value; //node weight(value)
private Node[] ZERO_NODE = new Node[0];
public Node(int value) {
this.value = value;
this.Childen = new ArrayList();
}

/**
*
* @return the node value
*/
public int getValue() {
return value;
}

/**
*
* @param value Node Value
*/
public void setValue(int value) {
this.value = value;
}

/**
*
* @param node Add node to the child node list
*/
public void addNode(Node node) {
this.Childen.add(node);
}

/**
*
* @return the Node Array
*/
public Node[] getChilden() {
return Childen.toArray(ZERO_NODE);
}
}
}

No comments: