Category Archives: My Works
Panel
import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.util.LinkedList; import java.util.Queue; import javax.swing.JPanel; /** * * @author Hazem * */ public class Panel extends JPanel { /** * Create the panel. */ public Panel(BinarySearchTree bst) { this.bst = bst; } static boolean check = false; BinarySearchTree bst; public void paint(Graphics g) { super.paint(g); Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); int num = bst.size; int y = 10; int nodes = 1; int level = 1; int lenght = 1020; Queue<Node> q = new LinkedList<Node>(); Queue<Integer> q2 = new LinkedList<Integer>(); q.add(bst.root); while (num > 0) { int pX = (int) Math.round(lenght / (2.0 * nodes)); int x = pX; for (int i = 0; i < nodes; i++) { Node n = q.poll(); if (n != null) { num--; g2.drawOval(x, y, 30 - 2 * level, 30 - 2 * level); g2.drawString(n.value + "", x + 10 - level, y + 15); g2.setColor(Color.black); if (n.leftChild == null) q.add(null); else q.add(n.leftChild); if (n.rightChild == null) q.add(null); else q.add(n.rightChild); if (level != 1) { int xx = q2.poll(); int yy = q2.poll(); g2.drawLine(xx, yy, x + (15 - 1 * level), y); } } else { q2.poll(); q2.poll(); q.add(null); q.add(null); } q2.add(x); q2.add(y + 15 - level); q2.add(x + 30 - 2 * level); q2.add(y + 15 - level); x += 2 * pX; } y += 40; nodes = 1 << level; level++; } } }
Node
/** * * @author Hazem * */ public class Node { public Node parent; public Node leftChild; public Node rightChild; public int value; public int typeOfChild; /** * The constructor * * @param value */ public Node(int value, Node parent) { this.value = value; this.parent = parent; leftChild = null; rightChild = null; } }
Frame
import java.awt.Color; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.SwingConstants; import javax.swing.border.EmptyBorder; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.FileDialog; import org.eclipse.swt.widgets.Shell; /** * * @author Hazem * */ public class Frame extends JFrame { private Panel panel; private JPanel contentPane; private JTextField nodesField; private JTextField pathField; public BinarySearchTree tree; /** * Launch the application. * * @throws FileNotFoundException */ public static void main(String[] args) throws FileNotFoundException { BinarySearchTree bst = new BinarySearchTree(); Frame frame = new Frame(bst); frame.setVisible(true); } /** * Create the frame. */ public Frame(BinarySearchTree bst) { setTitle("Binary Search Tree"); tree = bst; setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 1200, 533); contentPane = new JPanel(); contentPane.setBackground(new Color(165, 42, 42)); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JButton Post_Order = new JButton("Post Order"); Post_Order.setFont(new Font("Verdana", Font.BOLD | Font.ITALIC, 13)); Post_Order.setBounds(375, 378, 114, 23); Post_Order.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { pathField.setText(tree.Post_Order()); } }); contentPane.add(Post_Order); nodesField = new JTextField(); nodesField.setFont(new Font("Verdana", Font.ITALIC, 12)); nodesField.setBounds(84, 412, 1056, 28); contentPane.add(nodesField); nodesField.setColumns(10); JButton Pre_Order = new JButton("Pre Order"); Pre_Order.setFont(new Font("Verdana", Font.BOLD | Font.ITALIC, 13)); Pre_Order.setBounds(572, 378, 114, 23); Pre_Order.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { pathField.setText(tree.Pre_Order()); } }); contentPane.add(Pre_Order); JButton In_Order = new JButton("In Order"); In_Order.setFont(new Font("Verdana", Font.BOLD | Font.ITALIC, 13)); In_Order.setBounds(771, 378, 114, 23); In_Order.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { pathField.setText(tree.In_Order()); } }); contentPane.add(In_Order); pathField = new JTextField(); pathField.setFont(new Font("Verdana", Font.ITALIC, 12)); pathField.setBounds(84, 456, 1056, 28); contentPane.add(pathField); pathField.setColumns(10); JLabel lblNewLabel = new JLabel("Nodes"); lblNewLabel.setFont(new Font("Verdana", Font.BOLD | Font.ITALIC, 15)); lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER); lblNewLabel.setBounds(10, 417, 60, 14); contentPane.add(lblNewLabel); JLabel lblPath = new JLabel("Out Put"); lblPath.setFont(new Font("Verdana", Font.BOLD | Font.ITALIC, 15)); lblPath.setHorizontalAlignment(SwingConstants.CENTER); lblPath.setBounds(10, 461, 75, 14); contentPane.add(lblPath); JButton Add = new JButton("Add"); Add.setFont(new Font("Verdana", Font.BOLD | Font.ITALIC, 13)); Add.setBounds(572, 344, 114, 23); Add.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { tree.insert(Integer.parseInt(nodesField.getText())); panel.repaint(); } }); contentPane.add(Add); JButton Save = new JButton("Save"); Save.setFont(new Font("Verdana", Font.BOLD | Font.ITALIC, 13)); Save.setBounds(696, 344, 114, 23); contentPane.add(Save); Save.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub try { save(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); panel = new Panel(tree); panel.setBackground(Color.WHITE); panel.setBounds(43, 11, 1108, 322); contentPane.add(panel); JButton delete = new JButton("Delete"); delete.setFont(new Font("Verdana", Font.BOLD | Font.ITALIC, 13)); delete.setBounds(446, 344, 114, 23); delete.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { tree.delete(Integer.parseInt(nodesField.getText())); panel.repaint(); } }); contentPane.add(delete); JButton search = new JButton("Search"); search.setFont(new Font("Verdana", Font.BOLD | Font.ITALIC, 13)); search.setBounds(322, 344, 114, 23); search.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { pathField.setText("" + tree.search(Integer.parseInt(nodesField.getText()))); } }); contentPane.add(search); JButton Load = new JButton("Load"); Load.setFont(new Font("Verdana", Font.BOLD | Font.ITALIC, 13)); Load.setBounds(820, 344, 114, 23); Load.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { try { load(); panel.repaint(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); contentPane.add(Load); } private void load() throws FileNotFoundException { FileDialog fd = new FileDialog(new Shell(), SWT.OPEN); String name = fd.open(); if (name != null) { File file = new File(name); Scanner sc = new Scanner(file); tree.root = null; tree.size = 0; while (sc.hasNext()) { tree.insert(sc.nextInt()); } } } private void save() throws FileNotFoundException { FileDialog fd = new FileDialog(new Shell(), SWT.SAVE); String name = fd.open(); if (name != null) { File file = new File(name); PrintWriter writer = new PrintWriter(file); writer.println(pathField.getText()); writer.close(); } } }
BinarySearchTree
/** * * @author Hazem * */ public class BinarySearchTree { Node root; StringBuilder path; int size; public BinarySearchTree() { } /** * * @param value */ public void insert(int value) { if (root == null) { size++; root = new Node(value, null); } else add(value, root); } /** * * @param value */ public void delete(int value) { del(value, root); } /** * * @param value * @return */ public boolean search(int value) { return search_node(value, root); } /** * * @param value * @param currentNode * @return */ private boolean search_node(int value, Node currentNode) { if (currentNode == null) return false; else if (currentNode.value > value) return search_node(value, currentNode.leftChild); else if (currentNode.value < value) return search_node(value, currentNode.rightChild); else return true; } public String Pre_Order() { path = new StringBuilder(""); preOrder(root); return path.toString(); } public String In_Order() { path = new StringBuilder(""); inOrder(root); return path.toString(); } public String Post_Order() { path = new StringBuilder(""); postOrder(root); return path.toString(); } /** * * @param currentNode */ private void postOrder(Node currentNode) { if (currentNode != null) { postOrder(currentNode.leftChild); postOrder(currentNode.rightChild); path.append(currentNode.value + " "); } } /** * * @param currentNode */ private void preOrder(Node currentNode) { if (currentNode != null) { path.append(currentNode.value + " "); preOrder(currentNode.leftChild); preOrder(currentNode.rightChild); } } /** * * @param currentNode */ private void inOrder(Node currentNode) { if (currentNode != null) { inOrder(currentNode.leftChild); path.append(currentNode.value + " "); inOrder(currentNode.rightChild); } } /** * * @param value * @param currentNode */ private void add(int value, Node currentNode) { if (currentNode.value < value) { if (currentNode.rightChild == null) { size++; currentNode.rightChild = new Node(value, currentNode); } else add(value, currentNode.rightChild); } else if (currentNode.value > value) { if (currentNode.leftChild == null) { size++; currentNode.leftChild = new Node(value, currentNode); } else add(value, currentNode.leftChild); } } /** * * @param value * @param currentNode * if the currentNode is null this means that the node not found * else traverse through the tree according to the value */ public void del(int value, Node currentNode) { if (currentNode == null) return; if (currentNode.value > value) del(value, currentNode.leftChild); else if (currentNode.value < value) del(value, currentNode.rightChild); else { if (currentNode.leftChild == null && currentNode.rightChild == null) { size--; if (currentNode.parent == null) root = null; else if (currentNode.value >= currentNode.parent.value) currentNode.parent.rightChild = null; else currentNode.parent.leftChild = null; } else if (currentNode.leftChild == null) { size--; if (currentNode.parent == null) { root = currentNode.rightChild; root.parent = null; } else { if (currentNode.value >= currentNode.parent.value) { currentNode.parent.rightChild = currentNode.rightChild; currentNode.rightChild.parent = currentNode.parent; } else { currentNode.parent.leftChild = currentNode.rightChild; currentNode.rightChild.parent = currentNode.parent; } } } else if (currentNode.rightChild == null) { size--; if (currentNode.parent == null) { root = currentNode.leftChild; root.parent = null; } else if (currentNode.value >= currentNode.parent.value) { currentNode.parent.rightChild = currentNode.leftChild; currentNode.leftChild.parent = currentNode.parent; } else { currentNode.parent.leftChild = currentNode.leftChild; currentNode.leftChild.parent = currentNode.parent; } } else { currentNode.value = getLeast(currentNode.rightChild); } } } /** * * @param rightChild * @return */ private int getLeast(Node rightChild) { /** * replace the value of the current node with the minimum value at the * right of that node and then delete this min node */ while (rightChild.leftChild != null) { rightChild = rightChild.leftChild; } int value = rightChild.value; del(value, rightChild); return value; } }