//Titel:            Dialog-Fenster
//Version:
//Copyright:		Copyright (c) 2010
//Autor:
//Organisation:
//Beschreibung:


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;


public class wirbel1 extends JFrame {

  JButton BnInsert = new JButton("Insert");
  JButton BnTraversierung = new JButton("Traversierung");
  JButton BnEsc = new JButton("Abbruch");
  JTextArea editor = new JTextArea();

  node root;

  public wirbel1() {
    setSize(800, 430);
    setLocation(200,100);
    setTitle("JFrame als Dialog");
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    setGUI();
  }

  private void setGUI() {
    BnInsert.setFont( new Font("Arial", Font.BOLD,18)  );
    BnTraversierung.setFont( new Font("Arial", Font.BOLD,18)  );
    BnEsc.setFont( new Font("Arial", Font.BOLD,18)  );
    editor.setFont( new Font("Arial", Font.BOLD,18)  );

    BnTraversierung.setEnabled(false);

    this.getContentPane().setLayout( new BorderLayout() );

    this.getContentPane().add( new JScrollPane(editor), BorderLayout.CENTER );

	JPanel pnButton = new JPanel();
	//pnButton.setBackground(Color.red);
	pnButton.setLayout( new FlowLayout() );
	pnButton.add(BnInsert);
	pnButton.add(BnTraversierung);
	pnButton.add(BnEsc);

    this.getContentPane().add( pnButton, BorderLayout.SOUTH );

    BnInsert.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
              BnInsert_Click();
            }
    });
    BnTraversierung.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
              BnTraversierung_Click();
            }
    });


    BnEsc.addActionListener(new java.awt.event.ActionListener() {
       public void actionPerformed(ActionEvent e) {
          BnEsc_Click();
      }
    });

  } // setGUI

    private void BnInsert_Click() {
        node n1, n2, n3, n4, n5, n6, n7, n8;
        n1 = new node(2);
        n2 = new node(5);
        n3 = new node(9);
        n4 = new node(14);
        n5 = new node(4,n1,n2);
        n6 = new node(12,n3,n4);
        n7 = new node(8,n5,n6);

        n1 = new node(22);
        n2 = new node(45);
        n3 = new node(58);
        n4 = new node(63);
        n5 = new node(42,n1,n2);
        n6 = new node(60,n3,n4);

        n8 = new node(55,n5,n6);
        root = new node(17,n7,n8);
        BnTraversierung.setEnabled(true);
    }  // BnInsert



    private void Traversierung(node root) {
      node temp;
      if (root.weg==0) {
        // rechts
        root.weg=1;
        if (root.right != null) {
          temp = root.right;
          editor.append("\n              R:Knoten: ("+temp.data+")");
          Traversierung(temp);
        }
        editor.append("\nKnoten: "+root.data);
      }
     
      if (root.weg==1) {
        // rechts
         root.weg=2;
         if (root.left != null) {
           temp = root.left;
           editor.append("\n              L:Knoten: ("+temp.data+")");
           Traversierung(temp);
         }
      }
      else {
        // weg==2 
      }
    }  // Traversierung

    private void BnTraversierung_Click() {
       editor.setText("Starte Traversierung");
       Traversierung(root);
    }

    private void BnEsc_Click() {
        setVisible(false);
        System.exit(0);
    }

  public static void main(String[] args) {
    wirbel1 frame = new wirbel1();
    frame.setVisible(true);
 }

}  // wirbel1

class node {
    public node left=null;
    public node right=null;
    public int data=0;  
    public int weg=0;

   public node(int data) {
     this.data = data;
   }
   public node(int data, node right, node left) {
     this.data = data;
     this.left = left;
     this.right = right;
   }

}