G
Guest
Gast
Hallo zusammen,
hab mal so an einem binarytree-code rumgebastelt, weiss aber nicht, ob das alles stimmt. Zumindest dieses "null" stimmt nicht... Was ist denn bei Java das Pendant zu "NULL" bei C++? Wie siehts mit dem Rest aus, den ich geschrieben habe? Ist noch nicht fertig, bin noch dran, z.b. wenn es gar keine Tochterbäume mehr gibt.
Lg
hab mal so an einem binarytree-code rumgebastelt, weiss aber nicht, ob das alles stimmt. Zumindest dieses "null" stimmt nicht... Was ist denn bei Java das Pendant zu "NULL" bei C++? Wie siehts mit dem Rest aus, den ich geschrieben habe? Ist noch nicht fertig, bin noch dran, z.b. wenn es gar keine Tochterbäume mehr gibt.
Lg
Code:
package sort;
/**
* Informatik II - SS2007
* Uebungsserie 2, Aufgabe 3
* Template for class ConvertTree.java
*
* @author Silvia Santini
*/
public class ConvertTree {
char[] tree; //tree is a reference to an rray of char
/**
* Constructor
**/
ConvertTree(char[] a) {
tree = a;
}
// param i: position in array
// param j: current indention-level
void indent( int i, int j ) {
/* if(i==null)
{
return;
//break condition...) return;
}
*/
if(i != null && 2*i != null)
indent( 2*i ,j+1);
if(i != null && 2*i == null && 2*i+1 != null)
indent( 2*i+1 ,j+1);
// Print a space for each new tree-level
for( int n=0; n<j; n++ ) {
System.out.print( " " );
}
System.out.println(tree[i]);
//recursive calls
/* indent( 2*i ,j+1);
......*/
}
/**
* Give the programm the tree's array-representation as a
* single string argument.
* If your representation includes space characters,
* then include quotes.
* Example:
*
* java CovertTree $"123 67"$
*
* Test the programm with the following inputs (a missing node
* is represented by a space character):
*
* "1234567"
* "1 3 67"
* "AB D H P"
* "A C G O"
**/
public static void main( String args[] ) {
if( args.length == 0 || args.length > 1 ) {
System.out.println( "Invalid input" );
System.exit( 1 );
}
// Prefix the input-string with a space character, so that
// the effective tree starts at index 1
String input = " " + args[0];
//the method toCharArray converts a string in an array of
//characters
ConvertTree ct = new ConvertTree(input.toCharArray());
// ct is an instance of the CovertTree class
// which is initialised with the given array
// representation
ct.indent(1,0);
}
}