Java Programming (Trees) - Help Please
Posted by ~Ray @ 2008-11-13 12:38:14
***************************************************************************************Edit:To expand on this the output when trying to print out the tree's are null it is a empty []. This should contain the bit string in an ArrayList.***************************************************************************************
Hello. I am in a class where we have to create a Java programming implementing a tree. We are supposed to take two numbers (but right now I can't even get one to work) and convert it to a bit string (e g. 4: would be 00000100) but I am having some difficulty. I wrote this so far and was guided by the instructor to implement the tree in this manner. He also gave us another program that he said we could use to convert the number but it converts it to a string. Anyway. I am a little lost and I believe I am missing some parts in the "private final static ArrayList generateTree( BigInteger[] vs int n int k )" in the last for loop before the Ts add(T) part. Please if anyone could help me out in leading me in the right direction. I would greatly appreciate it. (I added some println statements throughout the program that I was using to help me out but I am still stuck.)import java math.*;import java util.*;import java io.*;import java lang.*;public class Tree { private int id; private BigInteger val; private Tree left; private Tree right; public Tree ( int id. String s ) { this id = id; this val = new BigInteger(s); } public Tree( int id. BigInteger val ) { this id = id; this val = val; } public Tree( Tree left. Tree right ) { this id = 0; this val = null; this left = left; this right = right; } public Tree( int id. String s. Tree left. Tree right ) { this( id s ); this left = left; this right = right; } public static ArrayList generateTree( int n ) { return generateTree( new BigInteger[n] ); } public static ArrayList generateTree( BigInteger[] vs ) { /*vs[0]=zero; vs[1]=one; vs[2]=two; vs[3]=three;*/ return generateTree( vs vs length. 0 ); } private final static ArrayList generateTree( BigInteger[] vs int n int k ) { ArrayList<Tree> Ts = new ArrayList<Tree>(); System out println("vs[k] = "+vs[k]);System out println("n = "+n);System out println("k = "+k); if( n == 1 ) { Ts add( new Tree( k vs[k] ) ); } else { for( int i=1; i < n-1; i++ ) { ArrayList Ls = generateTree( vs i k ); ArrayList Rs = generateTree( vs i+1 k+1 ); System out println("hello"); for( int l=0; l < Ls size(); l++ ) { for( int r=0; r < Rs size(); r++ ) { Tree T = new Tree((Tree)Ls get(l). (Tree)Rs get(l)); Ts add(T); } } } } return Ts; } public static final BigInteger zero=BigInteger valueOf(0); public static final BigInteger one=BigInteger valueOf(1); public static BigInteger bigones(int nbits) { return one shiftLeft(nbits) subtract(one); } public static BigInteger bignot(int nbits,BigInteger B) { return bigones(nbits) andNot(B); } public static String big2string(BigInteger BI,int bits) { String s = BI toString(2); StringBuffer b = new StringBuffer(bits); int n = bits-s length(); while(--n >= 0) { b append('0'); } b append(s); return b toString(); } public static final BigInteger nand(int nbits,BigInteger X,BigInteger Y) { X=X and(Y); return bignot(nbits,X); } public static void main(String[] args) { InputStreamReader isr = new InputStreamReader( System in ); BufferedReader stdin = new BufferedReader( isr ); String inputOne = ""; String inputTwo = ""; try { System out print( "Type some data for the program: " ); inputOne = stdin readLine(); System out print( "Type some data for the program: " ); inputTwo = stdin readLine(); } catch (IOException e) { e printStackTrace(); } BigInteger A = new BigInteger(inputOne); BigInteger B = new BigInteger(inputTwo); //BigInteger C = nand(8,A,B); String AString = big2string(A,8); String BString = big2string(B,8); //String CString = big2string(C,8); int input1 = Integer parseInt(inputOne); int input2 = Integer parseInt(inputTwo); System out println("A="+AString); System out println("B="+BString); //System out println("C="+CString); Tree T1 = new Tree(input1,8); //Tree T2 = new Tree(input2,8); ArrayList newT1 = T1 generateTree(input1); //ArrayList newT2 = T2 generateTree(input2); //T1 generateTree(input1); //T2 generateTree(input2); //ArrayList newT1 = generateTree(8); //ArrayList newT2 = generateTree(input2); //for(int m = 0; m < T1 size //Tree T = new Tree((Tree)Ls get(l). (Tree)Rs get(l)); System out print("newT1 = ");System out println(newT1); //System out print("newT2 = ");System out println(newT2); //System out print("input1 = ");System out println(T1 toString()); //System out println("input2 = "+T2 toString()); //System out println("input1 = "+T1 generateTree(input1)); //System out println("input2 = "+T2 generateTree(input2)); //System out println("input1 = "+generateTree(input1)); //System out println("input2 = "+generateTree(input2)); }}Edited by: laughdat08 on Nov 14. 2007 5:39 PMEdited by: laughdat08 on Nov 14. 2007 5:41 PMEdited by: laughdat08 on Nov 14. 2007 5:43 PM[ADVERTHERE]Related article:
http://forum.java.sun.com/thread.jspa?threadID=5236679
0 Comments:
No comments have been posted yet!
|