Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- * @author haytham
- */
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- /**
- *
- * @author Nasr
- */
- public class MyHomeWorkGraph {
- public static void main(String[] args){
- JFrame testHomeWork=new JFrame();
- testHomeWork.setSize(1000,500);
- testHomeWork.setResizable(false);
- testHomeWork.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- testHomeWork.add(new BinaryTreePanelShow());
- testHomeWork.setLocationRelativeTo(null);
- testHomeWork.setVisible(true);
- }
- }
- class BinaryTreePanelShow extends JPanel {
- private JTextField InputText = new JTextField(10);
- private MyGraphPaint paintTree = new MyGraphPaint();
- private JButton Add = new JButton("Add");
- private JButton Delete = new JButton("Delete");
- private MyBST<Integer> myBST;
- public BinaryTreePanelShow() {
- this.myBST = new MyBST();
- BuildGUIAndActionListner();
- }
- private void BuildGUIAndActionListner() {
- InputText.setFont(new Font("Tahoma", 4, 10));
- this.setLayout(new BorderLayout());
- /* I have two panel the first One to Draw Binary tree and the seconde to put add
- and delete buttonand Test Input :D ^_^ ;
- */
- add(paintTree, BorderLayout.CENTER);
- JPanel P1 = new JPanel();
- P1.add(new JLabel("Enter The Element :"));
- P1.add(InputText);
- P1.add(Add);
- P1.add(Delete);
- add(P1, BorderLayout.SOUTH);
- P1.setBackground(Color.GRAY);
- Add.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- int element = Integer.parseInt(InputText.getText());//take the data from text
- //When We add the element first we should search for it
- // if we found the element we will not complete
- if (myBST.search(element)) {
- JOptionPane.showMessageDialog(null,element+" This element is already there..!");
- }
- else {
- myBST.add(element);
- paintTree.repaint();
- }
- }
- });
- Delete.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- int element = Integer.parseInt(InputText.getText());
- if (!myBST.search(element)) {
- JOptionPane.showMessageDialog(null, element + " is not in the There..!");
- }
- else {
- myBST.delete(element);
- paintTree.repaint();
- }
- }
- });
- }
- class MyGraphPaint extends JPanel {
- @Override
- public void setBackground(Color bg) {
- super.setBackground(Color.white);
- }
- private int radius = 20;
- private int vericalDistance = 50;
- protected void paintComponent(Graphics g) {
- super.paintComponent(g);
- if (myBST.root != null) {
- printTree(g,myBST.root,getWidth()/2,25,getWidth()/4);
- }
- }
- private void printTree(Graphics g,Node root,int x, int y, int HorizantalDistance) {
- g.drawOval(x - radius, y - radius, 2 * radius, 2 * radius);
- g.drawString(root.data + "", x - 6, y + 4);
- if (root.left != null) {
- connectLeftChild(g, x - HorizantalDistance, y + vericalDistance, x, y);
- printTree(g, root.left, x - HorizantalDistance, y + vericalDistance, HorizantalDistance / 3);
- }
- if (root.right != null) {
- connectRightChild(g, x + HorizantalDistance, y + vericalDistance, x, y);
- printTree(g, root.right, x + HorizantalDistance, y + vericalDistance, HorizantalDistance / 3);
- }
- }
- private void connectLeftChild(Graphics g,int x1, int y1, int x2, int y2) {
- double d = Math.sqrt(vericalDistance * vericalDistance + (x2 - x1) * (x2 - x1));
- int x11 = (int)(x1 + radius * (x2 - x1) / d);
- int y11 = (int)(y1 - radius * vericalDistance / d);
- int x21 = (int)(x2 - radius * (x2 - x1) / d);
- int y21 = (int)(y2 + radius * vericalDistance / d);
- g.drawLine(x11, y11, x21, y21);
- }
- private void connectRightChild(Graphics g,int x1, int y1, int x2, int y2) {
- double d = Math.sqrt(vericalDistance * vericalDistance + (x2 - x1) * (x2 - x1));
- int x11 = (int)(x1 - radius * (x1 - x2) / d);
- int y11 = (int)(y1 - radius * vericalDistance / d);
- int x21 = (int)(x2 + radius * (x1 - x2) / d);
- int y21 = (int)(y2 + radius * vericalDistance / d);
- g.drawLine(x11, y11, x21, y21);
- }
- }
- }
- .................................................................................................ز
- import java.util.ArrayList;
- import java.util.Comparator;
- import java.util.Stack;
- /**
- *
- * @author Nasr
- */
- public class MyBST<E> {
- public Node<E> root=null;
- public MyBST(){}
- public MyBST(Comparator<E> comp)
- {
- root = null;
- }
- MyBST(E arr[]){
- root= sortedArrayToBST(arr);
- }
- public Node<E> sortedArrayToBST(E[] num) {
- if (num.length == 0)
- return null;
- return sortedArrayToBST(num, 0, num.length - 1);
- }
- public Node sortedArrayToBST(E[] num, int start, int end) {
- if (start > end)
- return null;
- int mid = (start + end) / 2;
- Node root = new Node (num[mid]);
- root.left = sortedArrayToBST(num, start, mid - 1);
- root.right = sortedArrayToBST(num, mid + 1, end);
- return root;
- }
- public void preOrder(){
- Stack<Node<E>> s=new Stack<Node<E>>();
- if(root==null)
- return;
- s.push(root);
- while (!s.empty()) {
- Node<E> n=s.pop();
- if(n.right!=null)
- s.push(n.right);
- if(n.left!=null)
- s.push(n.left);
- System.out.print(n.data+" ");
- }
- }
- public void inOrder() {
- if (root == null) return;
- Stack<Node<E>> stack = new Stack<Node<E>>();
- Node<E> node = root;
- while (!stack.isEmpty() || node != null) {
- if (node != null) {
- stack.add(node);
- node = node.left;
- } else {
- node = stack.pop();
- System.out.print(node.data + " ");
- node = node.right;
- }
- }
- }
- public void delete(E dataDeleted)
- {
- root = delete(root, dataDeleted);
- }
- private Node<E> delete(Node<E> root, E dataDeleted)
- {
- if (root == null)
- return null;
- else if (((Comparable)dataDeleted).compareTo(root.data) < 0)
- root.left = delete(root.left, dataDeleted);
- else if (((Comparable)dataDeleted).compareTo(root.data) > 0)
- root.right = delete (root.right, dataDeleted);
- else
- { if (root.left == null)
- return root.right;
- else if(root.right == null)
- return root.left;
- else
- {
- // get data from the rightmost node in the left subtree
- root.data = root.left.data;
- // delete the rightmost node in the left subtree
- root.left = delete(root.left, root.data) ;
- }
- }
- if(BF(root)==2){
- if(BF(root.right)<0){
- root.right= RR(root.right);
- root=RL(root);
- }
- else
- root=RL(root);
- }
- if(BF(root)==-2){
- if(BF(root.left)>0){
- root.left= RL(root.left);
- root=RR(root);}
- else
- root=RR(root);}
- return root;
- }
- //I Used two methods for recursion
- public int height()
- {
- return height(root);
- }
- private int height(Node<E> myNode)
- {
- if(myNode == null)
- return -1;
- else{
- return
- 1 + Math.max( height(myNode.left), height(myNode.right));
- }
- }
- public int BF(Node<E> t){
- return height(t.right)-height(t.left);
- }
- public Node<E> RR(Node<E> x){
- Node<E> y=x.left;
- x.left=y.right;
- y.right=x;
- return y;
- }
- public Node<E> RL(Node<E> x){
- Node<E> y=x.right;
- x.right=y.left;
- y.left=x;
- return y;
- }
- public int count(){
- return count(root);
- }
- public int count(Node<E> root){
- if(root==null)
- return 0;
- return 1+count(root.left)+count(root.right);
- }
- public void add(E data)
- {
- root = add(root, data);
- }
- private Node<E> add(Node<E> root, E data) {
- if (root==null||((Comparable)data).compareTo(root.data) == 0){
- root= new Node<E>(data);
- return root;
- }
- if (((Comparable)data).compareTo(root.data) < 0)
- root.left = add(root.left, data);
- else
- root.right = add(root.right, data);
- if(BF(root)==2){
- if(BF(root.right)<0){
- root.right= RR(root.right);
- root=RL(root);
- }
- else
- root=RL(root);
- }
- if(BF(root)==-2){
- if(BF(root.left)>0){
- root.left= RL(root.left);
- root=RR(root);}
- else
- root=RR(root);}
- return root;
- }
- //Here we will search for data and return the node for it
- public boolean search(E data){
- return search(root, data);
- }
- private boolean search(Node<E> myNode, E data) {
- if ( myNode== null)
- return false;
- else if (((Comparable)data).compareTo( myNode.data)==0){
- return true;}
- else if (((Comparable)data).compareTo( myNode.data)<0){
- return search(myNode.left, data);}
- else{
- return search(myNode.right, data);}
- }
- public void BreadthFirstRaversal(){
- ArrayList<Node<E>> list=new ArrayList<Node<E>>();
- list.add(root);
- while(list.size()!=0){
- Node<E> cNode=list.remove(0);
- System.out.print(cNode.data+" ");
- if(cNode.left!=null)
- list.add(cNode.left);
- if(cNode.right!=null)
- list.add(cNode.right);
- }
- }
- public boolean hasPathWithSum(int sum){
- return hasPathWithSum(root,sum);
- }
- private boolean hasPathWithSum(Node<E> root,int sum){
- if(root==null)
- return sum==0;
- else
- sum=sum-(Integer)root.data;
- return
- hasPathWithSum(root.left, sum)||hasPathWithSum(root.right, sum);
- }
- }
- ..................................................................................................
- public class Node<E> {
- E data;
- Node<E> left;
- Node<E> right;
- public Node(E data)
- {
- this.data=data;
- }
- public Node(E data, Node<E> Left, Node<E> rgiht)
- {
- this.left = left;
- this.right = rgiht;
- this.data = data;
- }
- public E getData(){
- return data;
- }
- }
- .
Advertisement
Add Comment
Please, Sign In to add comment