Sabtu, 06 September 2025

JAVA - Binary Search Tree With Swing GUI (1)

 






/*

 * To change this license header, choose License Headers in Project Properties.

 * To change this template file, choose Tools | Templates

 * and open the template in the editor.

 */

package bstgui;

import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;


// Node class for the Binary Search Tree

class Node {

    int key;

    Node left, right;


    public Node(int item) {

        key = item;

        left = right = null;

    }

}


// Binary Search Tree class

class BinarySearchTree {

    Node root;


    public BinarySearchTree() {

        root = null;

    }


    // Method to insert a new key

    public void insert(int key) {

        root = insertRec(root, key);

    }


    private Node insertRec(Node root, int key) {

        if (root == null) {

            root = new Node(key);

            return root;

        }

        if (key < root.key) {

            root.left = insertRec(root.left, key);

        } else if (key > root.key) {

            root.right = insertRec(root.right, key);

        }

        return root;

    }


    // Other BST methods (search, delete, etc.) would go here

}


// GUI class for displaying the BST

class BSTGUI extends JFrame {

    private BinarySearchTree bst;

    private JTextField inputField;

    private JPanel treePanel;


    public BSTGUI() {

        bst = new BinarySearchTree();

        setTitle("Binary Search Tree Visualization");

        setSize(800, 600);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setLayout(new BorderLayout());


        // Input and control panel

        JPanel controlPanel = new JPanel();

        inputField = new JTextField(10);

        JButton addButton = new JButton("Add Node");

        addButton.addActionListener(new ActionListener() {

            @Override

            public void actionPerformed(ActionEvent e) {

                try {

                    int value = Integer.parseInt(inputField.getText());

                    bst.insert(value);

                    treePanel.repaint(); // Redraw the tree

                    inputField.setText("");

                } catch (NumberFormatException ex) {

                    JOptionPane.showMessageDialog(BSTGUI.this, "Please enter a valid number.");

                }

            }

        });

        controlPanel.add(inputField);

        controlPanel.add(addButton);

        add(controlPanel, BorderLayout.NORTH);


        // Panel for drawing the tree

        treePanel = new JPanel() {

            @Override

            protected void paintComponent(Graphics g) {

                super.paintComponent(g);

                if (bst.root != null) {

                    drawTree(g, bst.root, getWidth() / 2, 30, 150);

                }

            }


            private void drawTree(Graphics g, Node node, int x, int y, int hGap) {

                if (node == null) return;


                // Draw node

                g.setColor(Color.BLUE);

                g.fillOval(x - 15, y - 15, 30, 30);

                g.setColor(Color.WHITE);

                g.drawString(String.valueOf(node.key), x - 8, y + 5);


                // Draw left child and line

                if (node.left != null) {

                    g.setColor(Color.BLACK);

                    g.drawLine(x, y + 15, x - hGap, y + 75 - 15);

                    drawTree(g, node.left, x - hGap, y + 75, hGap / 2);

                }


                // Draw right child and line

                if (node.right != null) {

                    g.setColor(Color.BLACK);

                    g.drawLine(x, y + 15, x + hGap, y + 75 - 15);

                    drawTree(g, node.right, x + hGap, y + 75, hGap / 2);

                }

            }

        };

        add(treePanel, BorderLayout.CENTER);

    }


    public static void main(String[] args) {

        SwingUtilities.invokeLater(() -> {

            new BSTGUI().setVisible(true);

        });

    }

}

Tidak ada komentar: