Question & Answer: mented as arrays. Provide a driver program to demonstrate that the method functions properly. Use the t…..

Write a method to perform an NLR scan on a binary tree. Assume the method will operate on trees implemented as arrays. Provide a driver program to demonstrate that the method functions properly. Use the trees shown in Figure 7.3 as test cases.

G) (H 1B
Fig.7.3

Don't use plagiarized sources. Get Your Custom Essay on
Question & Answer: mented as arrays. Provide a driver program to demonstrate that the method functions properly. Use the t…..
GET AN ESSAY WRITTEN FOR YOU FROM AS LOW AS $13/PAGE
Order Essay

G) (H 1B

Expert Answer

 

NLR or preorder traversal is done with trees or arrays. The binary tree can be represented as below if the root is ith element,

root:tree[root]

left child:tree[root+1]

right child:tree[root+2]

The array representation of first tree given is: tree={‘A’,’B’,’D’,’’,’E’,’F’,’’,’’,’’,’G’,’H’,’’,’’,’’,’’,’’,’’,’’,’’,’’,’’,’’,’’};

Java code:

public class NLR {
public static void main(String args[])
{

// tree to which the nlr scan to be performed is kept in an array format.
char[] tree={‘A’,’B’,’D’,’’,’E’,’F’,’’,’’,’’,’G’,’H’,’’,’’,’’,’’,’’,’’,’’,’’,’’,’’,’’,’’};
NLRScan(tree,0);
}
static void NLRScan(char[] tree,int root)
{
System.out.print(tree[root]+”,”);
if(tree[(2*root)+1]!=’’)
NLRScan(tree,(2*root)+1);
if(tree[(2*root)+2]!=’’)
NLRScan(tree,(2*root)+2);
}
}

Output:

A, B, E, G, H, D, F

Still stressed from student homework?
Get quality assistance from academic writers!