53 lines
985 B
Markdown
53 lines
985 B
Markdown
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)
|
|
```java
|
|
public class BinTreeNode
|
|
{
|
|
public String info;
|
|
public BinTreeNode leftChild, rightChild;
|
|
}
|
|
```
|
|
|
|
```java
|
|
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
|
|
```java
|
|
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 |