binary tree

Status
Nicht offen für weitere Antworten.
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








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);
    }
 }
 

0x7F800000

Top Contributor
"null" ist in java eine referenz, die ins nirvana zeigt, also auf gar kein Object.
Primitive datentypen wie int sind keine objekte, für die gibt es die stinknormale mathematische integer-0.
In C++ war es egal, da wurde "#define NULL 0" irgendwo einfach so traditionell gesetzt, eigentlich sollte man dort auch "0" statt "NULL" verwenden, das wurde dann automatisch (implizit) mit (MyObject*)0 in einen 0-zeiger gecastet, der auch nirgendwohin zeigt.

wie bist du überhaupt auf die idee gekommen, integer mit "null" zu vergleichen, wenn ich fragen dürfte...?? tut in C++ doch auch kein mensch :bahnhof:

[edit] omg, nicht schon wieder dieser baum, da war doch neulich auch einer mit derselben aufgabe... :lol:
 
G

Guest

Gast
Hehe, ich hoffe zumindest, dass sie nicht ganz gleich war. Sind eben so Standardaufgaben, die man anfangs so bekommt, musste es auch schon mal (mit Mühe und Not) in C++ machen :cool: .

Wie würdest due es dann angehen, wenn ich überprüfen möchte, ob das Array an dieser Stelle leer ist?

lg
 

0x7F800000

Top Contributor
gar nicht, es geht nicht. bei integer gibt es nicht einmal eine bit kombination die "NaN" bedeutet oder ähnliches. Es kann nicht leer sein, da steht immer irgendetwas drin: 0 per default.

Ich verstehe gar nicht was die frage eigentlich soll, in C++ geht das doch genausowenig...
 
G

Guest

Gast
Ja, stimmt, Aber jetzt sieht es viel besser aus, funktioniert aber immer noch nicht... Sieht jemand den Fehler? Wäre sehr dankbar.

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( tree.length >= i || tree[i] == ' ' ) return;
        //if(tree[i] != ' ' && tree[2*i] != ' ')
        	
       // if(tree[i] != ' ' && tree[2*i] == ' ' && tree[2*i+1] != ' ')
        	
        
        
        // Print a space for each new tree-level
        for( int n=0; n<j; n++ ) {
            System.out.print( " " );
        }

        System.out.println(tree[i]);
        indent( 2*i, j+1);
        indent( 2*i+1, j+1);
        //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(123,64);
    }
 }
 
G

Gast

Gast
Versuche mal als Abbruchbedingung: if(tree.length <= i || tree == ' ' && tree[i+1] == ' ') return;

Beim ersten wird sichergestellt, damit die Rekursion abgebrochen wird, sobald der String zuende ist. Die anderen Bedingungen sind die Abbruchbedingungen fuer leere Zweige.
 
Status
Nicht offen für weitere Antworten.

Oben