patternjavaMinor
Printing different aspects of a binary tree
Viewed 0 times
differentprintingbinaryaspectstree
Problem
I had an assignment, for which I got full marks, but am really upset about the code I have written. I feel it is too manual and repetitive. Please help me make it more optimized.
I am making many objects of
The code basically uses a class
I am learning data structures and algorithms, which hugely deal with the complexity of code.
```
import java.util.*;
import java.text.*;
import java.io.*;
class CS6085BTolani
{
static Lab5BTMethods one = new Lab5BTMethods();
static int array[] ;
static int counter=0;
static int level = 0;
static int right = 0;
static int left = 0;
static int numberOfNodesInLevel = 0;
static int levelWithMaxNodes = -1;
static File f1;
static PrintWriter pw;
public static void main(String[] args) throws Exception
{
new MyInfo().identity();
one.createBinaryTree();
array = new int[numberOfNodes(one.root)];
System.out.println();
System.out.println("Pre Order Travesal");
one.preOrder(one.root);
System.out.println("\n");
System.out.println("Height of the Tree = "+one.height(one.root));
System.out.print("\nThe Level Order of the Tree");
one.displayTree(one.root);
S
I am making many objects of
StringTokenizer and using many static variables. I wish to reduce that, and instead of using declaring 10 Strings, I wish to do it fewer number of times and get better code.The code basically uses a class
Lab5BTmethods which is given by my professor. We had to do the following:- Create a binary tree using the method
createBinaryTree()from the given class file
- Traverse the tree in
preOrder(), a method in the given class file
- Print the
height()(method in the class file)
- Print the level order of the tree (method in class file)
- Print number of nodes in tree (method in the class file)
- Print the largest number in the tree, which we had to code it
sumOfElements()
searchfor(n)
levelWithlargestNumberOfNodes()(I feel this is the worst in my code)
I am learning data structures and algorithms, which hugely deal with the complexity of code.
```
import java.util.*;
import java.text.*;
import java.io.*;
class CS6085BTolani
{
static Lab5BTMethods one = new Lab5BTMethods();
static int array[] ;
static int counter=0;
static int level = 0;
static int right = 0;
static int left = 0;
static int numberOfNodesInLevel = 0;
static int levelWithMaxNodes = -1;
static File f1;
static PrintWriter pw;
public static void main(String[] args) throws Exception
{
new MyInfo().identity();
one.createBinaryTree();
array = new int[numberOfNodes(one.root)];
System.out.println();
System.out.println("Pre Order Travesal");
one.preOrder(one.root);
System.out.println("\n");
System.out.println("Height of the Tree = "+one.height(one.root));
System.out.print("\nThe Level Order of the Tree");
one.displayTree(one.root);
S
Solution
Employ the use of arrays
You should refactor your 'lev0
removing the need for:
while (sc.hasNextLine())
{
if(count==0)
lev0 = sc.nextLine();
if(count==1)
lev1 = sc.nextLine();
if(count==2)
lev2 = sc.nextLine();
if(count==3)
lev3 = sc.nextLine();
if(count==4)
lev4 = sc.nextLine();
if(count==5)
lev5 = sc.nextLine();
if(count==6)
lev6 = sc.nextLine();
if(count==7)
lev7 = sc.nextLine();
if(count==8)
lev8 = sc.nextLine();
if(count==9)
lev9 = sc.nextLine();
if(count==10)
lev10 = sc.nextLine();
count++;
}You should refactor your 'lev0
throughlev10 to utilize an array. This is effectively just a for loop and could be simplified to the following.
for (int i = 0; i <= count; count++) {
// optional point of validation
lev[i] = sc.nextLine();
}
Likewise here:
StringTokenizer tokens = new StringTokenizer(lev0," ");
int firstcount = tokens.countTokens();
//System.out.println(firstcount);
StringTokenizer tokens1 = new StringTokenizer(lev1," ");
int secondcount = tokens1.countTokens();
StringTokenizer tokens2 = new StringTokenizer(lev2," ");
int thirdcount = tokens2.countTokens();
StringTokenizer tokens3 = new StringTokenizer(lev3," ");
int fourthcount = tokens3.countTokens();
StringTokenizer tokens4 = new StringTokenizer(lev4," ");
int fifthcount = tokens4.countTokens();
StringTokenizer tokens5 = new StringTokenizer(lev5," ");
int sixthcount = tokens5.countTokens();
StringTokenizer tokens6 = new StringTokenizer(lev6," ");
int seventhcount = tokens6.countTokens();
StringTokenizer tokens7 = new StringTokenizer(lev7," ");
int eighthcount = tokens7.countTokens();
StringTokenizer tokens8 = new StringTokenizer(lev8," ");
int ninthcount = tokens8.countTokens();
StringTokenizer tokens9 = new StringTokenizer(lev9," ");
int tenthcount = tokens9.countTokens();
StringTokenizer tokens10 = new StringTokenizer(lev10," ");
int eleventhcount = tokens10.countTokens();
Would be better as:
StringTokenizer[] tokens = new StringTokenizer[10];
int[] tokenCount = new int[tokens.length];
for (int i = 0; i < tokens.length; i++) {
tokens[i] = new StringTokenizer(lev[i], " ");
tokenCount[i] = tokens[i].countTokens();
}
Notice how the use of arrays before lends itself to the following tokenizer loop.
This also applies to your max value logic. Interestingly, you loop through to find it, but then essentially 'loop' again to find out which one it was, why not simply store the position of the element (more about the use of arrays)?
So, your logic loop could be refactored thus:
int maxValue = tokenCount[0];
int maxPosition = 0;
for (int i = 1; i maxValue) {
maxValue = tokenCount[i];
maxPosition = i;
}
}
So you may just proceed with System.out.println(lev[maxPosition]);`removing the need for:
if(maxValue == firstcount)
System.out.println(lev0);
if(maxValue == secondcount)
System.out.println(lev1);
if(maxValue == thirdcount)
System.out.println(lev2);
if(maxValue == fourthcount)
System.out.println(lev3);
if(maxValue == fifthcount)
System.out.println(lev4);
if(maxValue == sixthcount)
System.out.println(lev5);
if(maxValue == seventhcount)
System.out.println(lev6);
if(maxValue == eighthcount)
System.out.println(lev7);
if(maxValue == ninthcount)
System.out.println(lev8);
if(maxValue == tenthcount)
System.out.println(lev9);
if(maxValue == eleventhcount)
System.out.println(lev10);Code Snippets
while (sc.hasNextLine())
{
if(count==0)
lev0 = sc.nextLine();
if(count==1)
lev1 = sc.nextLine();
if(count==2)
lev2 = sc.nextLine();
if(count==3)
lev3 = sc.nextLine();
if(count==4)
lev4 = sc.nextLine();
if(count==5)
lev5 = sc.nextLine();
if(count==6)
lev6 = sc.nextLine();
if(count==7)
lev7 = sc.nextLine();
if(count==8)
lev8 = sc.nextLine();
if(count==9)
lev9 = sc.nextLine();
if(count==10)
lev10 = sc.nextLine();
count++;
}for (int i = 0; i <= count; count++) {
// optional point of validation
lev[i] = sc.nextLine();
}StringTokenizer tokens = new StringTokenizer(lev0," ");
int firstcount = tokens.countTokens();
//System.out.println(firstcount);
StringTokenizer tokens1 = new StringTokenizer(lev1," ");
int secondcount = tokens1.countTokens();
StringTokenizer tokens2 = new StringTokenizer(lev2," ");
int thirdcount = tokens2.countTokens();
StringTokenizer tokens3 = new StringTokenizer(lev3," ");
int fourthcount = tokens3.countTokens();
StringTokenizer tokens4 = new StringTokenizer(lev4," ");
int fifthcount = tokens4.countTokens();
StringTokenizer tokens5 = new StringTokenizer(lev5," ");
int sixthcount = tokens5.countTokens();
StringTokenizer tokens6 = new StringTokenizer(lev6," ");
int seventhcount = tokens6.countTokens();
StringTokenizer tokens7 = new StringTokenizer(lev7," ");
int eighthcount = tokens7.countTokens();
StringTokenizer tokens8 = new StringTokenizer(lev8," ");
int ninthcount = tokens8.countTokens();
StringTokenizer tokens9 = new StringTokenizer(lev9," ");
int tenthcount = tokens9.countTokens();
StringTokenizer tokens10 = new StringTokenizer(lev10," ");
int eleventhcount = tokens10.countTokens();StringTokenizer[] tokens = new StringTokenizer[10];
int[] tokenCount = new int[tokens.length];
for (int i = 0; i < tokens.length; i++) {
tokens[i] = new StringTokenizer(lev[i], " ");
tokenCount[i] = tokens[i].countTokens();
}int maxValue = tokenCount[0];
int maxPosition = 0;
for (int i = 1; i < tokenCount.length; i++) {
if (tokenCount[i] > maxValue) {
maxValue = tokenCount[i];
maxPosition = i;
}
}Context
StackExchange Code Review Q#143904, answer score: 2
Revisions (0)
No revisions yet.