Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Binärbaum grafisch darstellen?

Empfohlene Antworten

Veröffentlicht

Hallo

und zwar habe ich folgende Klassen programmiert um einen Binärbaum zu erhalten:

Nun suche ich eine möglichkeit die es mir ermöglicht ihn als applet(oder irgendwie anders grafisch) darzustellen, hat jemand einen rat für mich??????????

und eine andere Frage die ich mir stelle ist es wie ich aus diesem Baum einen Binärbaum für Objekte mache, denn im Moment ist er ja nur für int zu gebrauchen? hat da jemand eine idee?

import java.io.*;

public class Binärbaum

{

protected String Knoten;

protected int index = 0;

protected int w;

protected int Wert;

Binärbaum vater, links, rechts;

/************************Konstruktor für den Binärbaum, der einen Eingegebenen Wert erwartet***********************/

Binärbaum(int w)

{

Wert = w;

vater = null;

links = null;

rechts = null;

index++;

hinzufuegen(w);

}

/*

public String toString()

{

String s = (NamedesKnotens+" :"+Knoten);

return s;

}

*/

/************************Eingabe der hinzuzufügenden einzelnen Werte*******************************/

public int eingabe()

{

System.out.print("Eingabe des Wertes: ");

try

{

BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));

Knoten = buffer.readLine();

w = Integer.parseInt(Knoten);

hinzufuegen(w); //Startet den Vorgang hinzufügen mit dem Eingegebenen Wert

}

catch (IOException ioe)

{

System.out.print("Fehler: " + ioe);

System.exit(1);

}

return w;

}

/********************************Hinzufuegen des eingegebenen Wertes zum Binärbaum*******************************/

public void hinzufuegen(int w)

{

if (w != Wert)

{

if (w < Wert)

{

if (links == null) //Ist der linke Sohn frei?

{

links = new Binärbaum(w);

links.vater = this;

index++;

System.out.println(w + " links von :" + Wert + " eingehangen"); //Ausgabe wo der Wert angehängt wird

}// Ende if (links == null)

else links.hinzufuegen(w); //nicht frei, dann starte funktion hinzufuegen am nächsten Knoten neu

}// Ende if (w < Wert)

else

{

if (rechts == null) // Ist der rechte Sohn frei?

{

rechts = new Binärbaum(w);

rechts.vater = this;

index++;

System.out.println(w + " rechts von :" + Wert + " eingehangen"); //Ausgabe wo der Wert angehängt wird

}// Ende if (rechts == null)

else rechts.hinzufuegen(w); //nicht frei, dann starte funktion hinzufuegen am nächsten Knoten neu

}//Ende Else (w < Wert)

}// Ende (w != Wert)

}

/*******************************Suchfunktion für einen angegebenen Wert ******************************/

public void search(int Gesucht)

{

if (Gesucht == Wert){

System.out.println("Der von ihnen gesuchte Wert("+Gesucht+") ist vorhanden");

}

else

{

if (links != null && Gesucht < Wert)

{

links.search(Gesucht);

}

else

{

if (rechts != null && Gesucht > Wert)

{

rechts.search(Gesucht);

}

else

{

System.out.println("Der von ihnen gesuchte Wert("+Gesucht+") ist nicht vorhanden");

}

}

}

}

/**********************************************Ausgabe des gesamten Baums**************************/

public void ausgabe()

{

if(links != null )

{

System.out.println(links.Wert+" ist ein linker Wert von: "+links.vater.Wert);

links.ausgabe();

}

if(rechts != null)

{

System.out.println(rechts.Wert+" ist ein rechter Wert von: "+rechts.vater.Wert);

rechts.ausgabe();

}

}

/*************************************Löschen eines eingegebenen Wertes********************************************/

public void löschen(int w)

{

{

if (w == this.Wert)

{

if ( this.links == null && this.rechts == null)

{

if ( this.vater.links == this)

{

this.vater.links = null;

System.out.println(w + " Gelöscht!");

}

if ( this.vater.rechts == this)

{

this.vater.rechts = null;

System.out.println(w + " Gelöscht!");

}

}

else

{

if (this.links != null && this.rechts == null)

{

this.links.vater = this.vater;

if ( this.vater.links == this)

{

this.vater.links = this.links;

System.out.println(w + " Gelöscht!");

}

if ( this.vater.rechts == this)

{

this.vater.rechts = this.links;

System.out.println(w + " Gelöscht!");

}

}

else

{

if (this.rechts != null && this.links == null)

{

this.rechts.vater = this.vater;

if ( this.vater.links == this)

{

this.vater.links = this.rechts;

System.out.println(w + " Gelöscht!");

}

if ( this.vater.rechts == this)

{

this.vater.rechts = this.rechts;

System.out.println(w + " Gelöscht!");

}

}

else

{

if (this.rechts.links == null)

{

this.links.vater = this.rechts;

this.rechts.vater = this.vater;

if ( this.vater.links == this)

{

this.vater.links = this.rechts;

System.out.println(w + " Gelöscht!");

}

if ( this.vater.rechts == this)

{

this.vater.rechts = this.rechts;

System.out.println(w + " Gelöscht!");

}

}

else

{

this.Wert = this.rechts.getlastleftwert();

}

}

}

}

}

else

{

if (w < this.Wert)

{

if (this.links != null)

{

this.links.löschen(w);

}

else

{

System.out.println(w + " ist nicht vorhanden");

}

}

if (w > this.Wert)

{

if (this.rechts != null)

{

this.rechts.löschen(w);

}

else

{

System.out.println(w + " ist nicht vorhanden");

}

}

}

return;

}

}

private int getlastleftwert()

{

int Hilf;

if(this.links != null)

{

return this.links.getlastleftwert();

}

else

{

this.vater.links = null;

return this.Wert;

}

}

}

Generiert wird er durch folgendes:

public class Binärausführen {

public static void main(String[] args) {

Binärbaum aloha = new Binärbaum(12);

aloha.eingabe();

aloha.eingabe();

aloha.eingabe();

aloha.eingabe();

aloha.eingabe();

aloha.eingabe();

aloha.eingabe();

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.