-bash-4.2$ nano -w 1111a.phps
-bash-4.2$ ls -ld 1111a.phps
-rw-r--r-- 1 dgerman faculty 1 Nov 11 15:34 1111a.phps
-bash-4.2$ date
Wed Nov 11 15:34:20 EST 2020
-bash-4.2$ pwd
/u/dgerman/apache/htdocs/c212/fall2020
-bash-4.2$

--

https://legacy.cs.indiana.edu/classes/c212-dgerman/fall2017/studyGuide.html

--

import javax.swing.JComponent; 
import java.awt.Graphics; 

public class Drawing extends JComponent {
  BST bst; 
  public Drawing(BST bst) {
    this.bst = bst;     
  }
  public void paintComponent(Graphics g) {
    this.bst.draw(g); 
  }
}

--

import javax.swing.JFrame; 
import java.awt.Graphics;
import java.awt.Color;
import java.util.Scanner; 

public class BST {
  int x, y; // top-left corner bounding rectangle 
  int depth; // y is 50 + depth (maybe) 
  // depth also controls the longitude 
  int value; 
  BST left, right; 
  // ------------------------------------------------------( data representation )------------------
  public BST(int value, BST left, BST right) {
    this.value = value; 
    this.left = left; 
    this.right = right; 
  }
  // ------------------------------------------------------( constructor )------------------
  public boolean searchFor(int value) {
    if (this.value == value) 
      return true; 
    if (this.value > value) {
      if (this.left == null) return false; 
      else return this.left.searchFor(value); 
    } else {
      if (this.right == null) return false; 
      else return this.left.searchFor(value); 
    }
  }
  // ------------------------------------------------------( one method )------------------
  public boolean insert(int value) {
    if (this.value == value) 
      return false; 
    if (this.value > value) {
      if (this.left == null) {
        BST node = new BST(value, null, null); 
        this.left = node; 
        node.depth = 1 + this.depth;
        node.x = this.x - (int) (Math.pow(2, - node.depth) * 200) ; 
        node.y = this.y + 100; 
        return true; 
      } else return this.left.insert(value); 
    } else {
      if (this.right == null) {
        BST node = new BST(value, null, null); 
        this.right = node; 
        node.depth = 1 + this.depth;
        node.x = this.x + (int) (Math.pow(2, - node.depth) * 200) ; 
        node.y = this.y + 100; 
        return true; 
      } else return this.right.insert(value); 
    
    }      
  }
  public void draw(Graphics g) {
    int radius = 20; 
    
    if (this.left != null) {
      g.drawLine(this.x + radius, this.y + radius, this.left.x + radius, this.left.y + radius); 
      this.left.draw(g); 
    }
    if (this.right != null) {
      g.drawLine(this.x + radius, this.y + radius, this.right.x + radius, this.right.y + radius); 
      this.right.draw(g); 
    }

    g.setColor(Color.WHITE); 
    g.fillOval(this.x, this.y, 40, 40); // radius 20 

    g.setColor(Color.BLACK); 
    g.drawOval(this.x, this.y, 40, 40);  
    
    g.drawString(this.value + "", this.x + 40 / 2 - 5, this.y + 40 / 2 + 5); 
    
  }
  // ------------------------------------------------------( another method )------------------
  public static void main(String[] args) {
    BST a = new BST( 8, new BST( 3, new BST(1, null, 
                                               null), 
                                    new BST(6, null, 
                                               null)), 
                        new BST(10, null, 
                                    new BST(14, null, 
                                                null)));
    a.x = 200; a.y = 0;                                         a.depth = 0; 
    a.left.x        = 100; a.left.y        = 100; /* 3   */     a.left.depth = a.depth + 1; 
    a.right.x       = 300; a.right.y       = 100; /* 10  */     a.right.depth = a.depth + 1; 
    a.right.right.x = 350; a.right.right.y = 200; /* 14  */     a.right.right.depth = 2; 
    a.left.left.x   =  50; a.left.left.y   = 200; /* 1   */     a.left.left.depth = 2; 
    a.left.right.x  = 150; a.left.right.y  = 200; /* 6   */     a.left.right.depth = 2; 
    
    BST node = new BST(13, null, null); // this is what add should look like 
    a.right.right.left = node; 
    node.x = a.right.right.x - 25 ; 
    node.y = a.right.right.y + 100; 
    node.depth = 1 + a.right.right.depth; 
    
    a.insert(4); 
    a.insert(7); 
    
    JFrame f = new JFrame("Exercise"); 
    Drawing d = new Drawing(a); 
    f.add(d); 
    f.setVisible(true); 
    f.setSize(500, 500); 

    Scanner in = new Scanner(System.in); 
    System.out.print("enter> "); 
    int number = Integer.parseInt(in.nextLine()); 
    d.bst.insert(number); // reach out into the drawing and talk to that tree 
    d.repaint();     
    
    System.out.print("enter> "); 
    number = Integer.parseInt(in.nextLine()); 
    d.bst.insert(number); // reach out into the drawing and talk to that tree 
    d.repaint();     
    
  
  }
}

--