Problem:
Binary Tree

A rooted binary tree is a tree with a root node in which every node has at most two children.

Your task is to write a program which reads a rooted binary tree T and prints the following information for each node u of T:

node ID of u

parent of u

sibling of u

the number of children of u

depth of u

height of u

node type (root, internal node or leaf)

If two nodes have the same parent, they are siblings. Here, if u and v have the same parent, we say u is a sibling of v (vice versa).

The height of a node in a tree is the number of edges on the longest simple downward path from the node to a leaf.

Here, the given binary tree consists of n nodes and evey node has a unique ID from 0 to n-1.

Input

The first line of the input includes an integer n, the number of nodes of the tree.

In the next n lines, the information of each node is given in the following format:

id left right

id is the node ID, left is ID of the left child and right is ID of the right child. If the node does not have the left (right) child, the left(right) is indicated by -1.

Output

Print the information of each node in the following format:

node id: parent = p, sibling = s, degree = deg, depth = dep, height = h, type

p is ID of its parent. If the node does not have a parent, print -1.

s is ID of its sibling. If the node does not have a sibling, print -1.

deg, dep and h are the number of children, depth and height of the node respectively.

type is a type of nodes represented by a string (root, internal node or leaf. If the root can be considered as a leaf or an internal node, print root.

Please follow the format presented in a sample output below.

Constraints

1 ≤ n ≤ 25

Sample Input 1

9
0 1 4
1 2 3
2 -1 -1
3 -1 -1
4 5 8
5 6 7
6 -1 -1
7 -1 -1
8 -1 -1

Sample Output 1

node 0: parent = -1, sibling = -1, degree = 2, depth = 0, height = 3, root
node 1: parent = 0, sibling = 4, degree = 2, depth = 1, height = 1, internal node
node 2: parent = 1, sibling = 3, degree = 0, depth = 2, height = 0, leaf
node 3: parent = 1, sibling = 2, degree = 0, depth = 2, height = 0, leaf
node 4: parent = 0, sibling = 1, degree = 2, depth = 1, height = 2, internal node
node 5: parent = 4, sibling = 8, degree = 2, depth = 2, height = 1, internal node
node 6: parent = 5, sibling = 7, degree = 0, depth = 3, height = 0, leaf
node 7: parent = 5, sibling = 6, degree = 0, depth = 3, height = 0, leaf
node 8: parent = 4, sibling = 5, degree = 0, depth = 2, height = 0, leaf

Reference

Introduction to Algorithms, Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. The MIT Press.

Solution in JAVA (judge verdict: Accepted)


import java.util.*;


public class Main{ 

	public static void main(String args[]){
		Scanner scan = new Scanner(System.in);
		//int numNode = scan.nextInt();
		TreeNodeData data = new TreeNodeData(scan.nextInt());
		//scan.nextLine();
		data.readNodeData(scan);
		Root root = data.getNodeList();
		ArrayList<AbstractNode> tree = root.connectTree();
		Collections.sort(tree);
		for(int i=0; i<tree.size();i++)
			System.out.println(tree.get(i));

	}

}
enum NODETYPE{
	ROOT,INTERNAL,LEAF,LEFTCHILD,RIGHTCHILD
}
class NodeData{
	private int leftID;
	private int rightID;
	private ArrayList<Integer> children;
	private NODETYPE nodeType;
	public NodeData(int l, int r){
		leftID= l;
		rightID = r;
		if(l != -1 && r != -1)
			nodeType = NODETYPE.INTERNAL;
		else if(l == -1 && r == -1)
			nodeType = NODETYPE.LEAF;
		else if(r==-1)
			nodeType = NODETYPE.LEFTCHILD;
		else
			nodeType = NODETYPE.RIGHTCHILD;
	}
	public NODETYPE getType(){
		return nodeType;
	}
	public int getLeftID(){
		return leftID;
	}
	public int getRightID(){
		return rightID;
	}
	@Override
	public String toString(){
		StringBuilder temp = new StringBuilder();
		temp.append(leftID+", ");
		temp.append(rightID+", ");
		temp.append(nodeType+", ");
		return new String(temp);
	}
}

class TreeNodeData{
	int numNode;
	private HashMap<Integer, NodeData> nodeData = new HashMap<Integer, NodeData>();
	public TreeNodeData(int n){
		this.numNode = n;	
	}

	public void readNodeData(Scanner scan){
		for(int i=0; i<numNode; i++){
			int id = scan.nextInt();
			int left = scan.nextInt();
			int right = scan.nextInt();
			NodeData data = new NodeData(left, right);
			nodeData.put(id, data);
			///System.out.println(data);
		}
	}
	private int rootID(){
		ArrayList<Integer> notParent = new ArrayList<Integer>();
		for(int i=0; i<numNode; i++){
			notParent.add(nodeData.get(i).getLeftID());
			notParent.add(nodeData.get(i).getRightID());
		}
		for(int i=0; i<numNode; i++)
			if(!notParent.contains(i))
				return i;
		return -1;
	}
	public Root getNodeList(){
		return new Root(rootID(), nodeData);	
	}
}


class Leaf extends AbstractNode{
	public Leaf(int n, HashMap<Integer, NodeData> nodeData){
		super(n, nodeData);
	}

	@Override 
	String getType(){
			return "leaf";
	}
	@Override 
	public int getDepth(){//(AbstractNode n){
			return parent.getDepth()+1;
	}
	@Override 
	public int getParentID(){
		return parent.getID();
	}
}
class Root extends AbstractNode{
	public Root(int n, HashMap<Integer, NodeData> nodeData){
		super(n, nodeData);
	}
	@Override 
	public int getDepth(){
		return 0;
	}
	@Override 
	String getType(){
			return "root";
	}
	@Override 
	public int getParentID(){
		return -1;
	}
}
class InternalNode extends AbstractNode{
	public InternalNode(int n, HashMap<Integer, NodeData> nodeData){
		super(n, nodeData);
	}

	@Override 
	int getDepth(){//(AbstractNode n){
			return parent.getDepth()+1;
	}
	@Override 
	String getType(){
			return "internal node";
	}
	@Override 
	public int getParentID(){
		return parent.getID();
	}
}
class NodeFactory{	
	static AbstractNode getInstance(int id, HashMap<Integer, NodeData> nodeData){
		NODETYPE t = nodeData.get(id).getType();
/*		if(t==NODETYPE.LEFTCHILD)
			return new LeftChildNode(id ,nodeData);
		if(t==NODETYPE.RIGHTCHILD)
			return new RightChildNode(id ,nodeData);*/
		if(t==NODETYPE.LEAF)
			return new Leaf(id ,nodeData);
		return new InternalNode(id ,nodeData);
	}
}
abstract class AbstractNode implements Comparable<AbstractNode>{
	protected int id;
	protected AbstractNode parent;
	protected ArrayList<AbstractNode> children = new ArrayList<AbstractNode>();
	protected AbstractNode LeftChild=null;
	protected AbstractNode RightChild=null;
	abstract int getDepth();
	abstract String getType();
	abstract public int getParentID();
	public AbstractNode(int id, HashMap<Integer, NodeData> nodeData){
		this.id = id;
		NodeData self = nodeData.get(id);
		if(self.getType()==NODETYPE.LEAF)
			return;
		if(self.getType()!=NODETYPE.RIGHTCHILD){
			AbstractNode temp = NodeFactory.getInstance(self.getLeftID(), nodeData);
			temp.setParent(this);
			this.LeftChild = temp;
			//this.addChild(temp);
		}
		if(self.getType()!=NODETYPE.LEFTCHILD){
			AbstractNode temp = NodeFactory.getInstance(self.getRightID(), nodeData);
			temp.setParent(this);
			this.RightChild = temp;
		}

	}

	public void addChild(AbstractNode c){
		children.add(c);
	}
	public int getID(){
		return id;
	}
	public void setParent(AbstractNode p){
		this.parent = p;
	}
	String childrenID(){
		StringBuilder temp = new StringBuilder();
		temp.append("[");
		if(children.size()>0)
		for(int i=0; ; i++){
			temp.append(children.get(i).getID());
			if( i == children.size() - 1 )
				break;
			temp.append(", ");
		}
		temp.append("]");
		return new String(temp);
	}
	public int getHeight(){
		int leftHeight=0;
		int rightHeight=0;
		if(LeftChild==null && RightChild==null)
			return 0;
		if(LeftChild!=null)
			leftHeight = LeftChild.getHeight();
		if(RightChild!=null)
			rightHeight = RightChild.getHeight();
		return Math.max(leftHeight, rightHeight) +1;

	}
	int getDegree(){
		int countDegree = 0;
		if(LeftChild!=null)
			countDegree++;
		if(RightChild!=null)
			countDegree++;	
		return countDegree;
	}
	public int getSiblingID(){
		if(parent!=null)
			return parent.getAnotherChild(this.id);
		return -1;
	}
	public int getAnotherChild(int id){
		if(LeftChild!=null && LeftChild.getID() == id)
			if(RightChild!=null)
				return RightChild.getID();
		if(RightChild!=null && RightChild.getID() == id)
			if(LeftChild!=null)
				return LeftChild.getID();
		return -1;
	}
	public void nodeInfomation(){
		System.out.println(this);
		for(int i=0; i< children.size();i++)
			children.get(i).nodeInfomation();
	}
	public ArrayList<AbstractNode> connectTree(){
		ArrayList<AbstractNode> temp = new ArrayList<AbstractNode>();
		temp.add(this);
		if(LeftChild!=null)
			temp.addAll(LeftChild.connectTree());
		if(RightChild!=null)
			temp.addAll(RightChild.connectTree());
/*		for(int i=0; i< children.size();i++)
			temp.addAll(children.get(i));*/
		return 	temp;
	}
	@Override
	public String toString(){
		StringBuilder temp = new StringBuilder();
		temp.append("node " + this.id);
		temp.append(": parent = " + getParentID());
		temp.append(", sibling = " + getSiblingID());
		temp.append(", degree = " + getDegree());
		temp.append(", depth = " + getDepth());
		temp.append(", height = " + getHeight());
		temp.append(", "  + getType());
		//temp.append(", "   + childrenID());
		return new String(temp);
	}
	@Override
 	public int compareTo(AbstractNode temp) {
		return this.id - temp.getID();
	}
}