Files
G4G0-1/Semester 2/Programming 2/Week 10 Revision/Q5.md

985 B

a) List the order in which the nodes are visited in: post-order. D,B,F,G,E,C,A

pre-order A,B,D,C,E,F,G

in-order D,B,F,E,G,C,A

b)

public class BinTreeNode
{
	public String info;
	public BinTreeNode leftChild, rightChild;
}
public void postTraverse( BinTreeNode node )
{
	if ( node != null )
	{
		postTraverse( node.leftNode );
		postTraverse( node.rightNode );
		System.out.println( node.info );
	}
}

c) The following method counts the number of nodes in the tree

public int countTree(BinTreeNode t)
{
	int count;
	if (t == null)
		count = 0
	else
		count = countTree(t.leftChild)+countTree(t.rightChild)+1;
	return count;
}

countTree(root) -> count = countTree(t.leftChild) + countTree(t.rightChild+1) countTree(t.leftChild) -> return 1, since left and right child null countTree(t.rightChild) -> return 1, since left and right child null count = 1 + 1 + 1 = 3