Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.39 KB | None | 0 0
  1. package bst;
  2.  
  3. import java.awt.Color;
  4.  
  5. import bst.BinarySearchTree.BinaryNode;
  6. import drawing.*;
  7.  
  8. public class BSTVisualizer {
  9.     private DrawingArea canvas;
  10.    
  11.     // width of empty tree (i.e. "null nodes")
  12.     private final static int EMPTY_TREE_WIDTH = 0; // = 1;
  13.     // diameter of nodes;
  14.     private final static int DIAMETER = 20;
  15.     // horizontal distance between nodes on same level
  16.     private final static int HORIZONTAL_DIST = 1;
  17.     // vertical distance between levels
  18.     private final static int VERTICAL_DIST = 10;
  19.     // distance between node (circle) center and content string
  20.     private final static int OFFSET = -10;
  21.  
  22.     /**
  23.      * Creates a canvas with a certain title, width, height.
  24.      */
  25.     public BSTVisualizer(String title, int width, int height) {
  26.         canvas = new DrawingArea(title, width, height, Color.white);
  27.     }
  28.  
  29.     /**
  30.      * Draws a binary search tree on the canvas.
  31.      */
  32.     public void drawTree(BinarySearchTree<?> bst) {
  33.         if (bst.root != null) {
  34.             canvas.erase();
  35.             VNode root = buildVTree(bst);
  36.             calculateWidth(root);
  37.             drawTree(root, 1, 0);
  38.             canvas.paint();
  39.         }
  40.     }
  41.    
  42.     /* ------ Private auxiliary methods -------------- */
  43.  
  44.     private void calculateWidth(VNode root) {
  45.         if(root.node == null) {
  46.             root.width = EMPTY_TREE_WIDTH;
  47.         } else {
  48.             calculateWidth(root.left  = new VNode(root.node.left));
  49.             calculateWidth(root.right = new VNode(root.node.right));
  50.             root.width = 1 + root.left.width + root.right.width;
  51.         }
  52.     }
  53.  
  54.     private VNode buildVTree(BinarySearchTree<?> bst) {
  55.         VNode root = new VNode(bst.root);
  56.         buildVTree(root);
  57.         return root;
  58.     }
  59.    
  60.     private void buildVTree(VNode root) {
  61.         if(root.node != null) {
  62.             root.left = new VNode(root.node.left);
  63.             root.right = new VNode(root.node.right);
  64.         }
  65.     }
  66.  
  67.     private void drawTree(VNode vnode, int level, int offset) {
  68.         int col = 1 + vnode.left.width + offset;
  69.         int xPos = computeXpos(col);
  70.         int yPos = computeYpos(level);
  71.         int childYpos = computeYpos(level + 1);
  72.        
  73.         if (vnode.left.node != null) {
  74.             int leftChildXpos = computeXpos(1+vnode.left.left.width+offset);
  75.             canvas.drawLine(Color.BLACK, xPos, yPos, leftChildXpos, childYpos);
  76.             drawTree(vnode.left, level + 1, offset);
  77.         }
  78.         if (vnode.node.right != null) {
  79.             int rightChildXpos = computeXpos(1+col+vnode.right.left.width);
  80.             canvas.drawLine(Color.BLACK, xPos, yPos, rightChildXpos, childYpos);
  81.             drawTree(vnode.right, level + 1, col);
  82.         }
  83.        
  84.         String text = String.valueOf(vnode.node.element);
  85.         canvas.fillCircle(Color.BLUE, xPos, yPos, DIAMETER);
  86.         canvas.drawString(Color.BLACK, text, xPos + OFFSET, yPos + OFFSET);
  87.     }
  88.  
  89.     /* Compute y-position of a node from its level. */
  90.     private int computeYpos(int level) {
  91.         return level * (DIAMETER + VERTICAL_DIST);
  92.     }
  93.  
  94.     /* Compute x-position of a node from its inordernumber. */
  95.     private int computeXpos(int actNodeNbr) {
  96.         return actNodeNbr * (DIAMETER + HORIZONTAL_DIST);
  97.     }
  98.    
  99.     private static class VNode {
  100.         BinarySearchTree.BinaryNode<?> node;
  101.         VNode left;
  102.         VNode right;
  103.         int width;
  104.         public VNode(BinaryNode<?> node) {
  105.             this.node = node;
  106.         }
  107.        
  108.     }
  109.    
  110.     public static void main(String[] args) {
  111.         BinarySearchTree<Integer> tree = new BinarySearchTree<Integer>();
  112.         BSTVisualizer vis = new BSTVisualizer("Hallihallå", 500, 600);
  113.        
  114.         tree.add(50);
  115.         tree.add(40);
  116.         tree.add(6);
  117.         tree.add(1);
  118.         tree.add(2);
  119.         tree.add(3);
  120.  
  121.        
  122.         tree.rebuild();
  123.         vis.drawTree(tree);
  124.     }
  125.    
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement