Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.31 KB | None | 0 0
  1. public class Main {
  2.     public static void main(String[] args) {
  3.         BinTree tree = new BinTree(7);
  4.  
  5.         tree.left = new BinTree(0);
  6.         tree.left.left = new BinTree(-2);
  7.         tree.left.left.right = new BinTree(-1);
  8.         tree.left.right = new BinTree(4);
  9.  
  10.         tree.right = new BinTree(9);
  11.         tree.right.right = new BinTree(99);
  12.         tree.right.left = new BinTree(8);
  13.  
  14.         System.out.println("min: " + tree.getMin());
  15.         System.out.println("max: " + tree.getMax());
  16.  
  17.         System.out.println(tree.isSearchTree());
  18.     }
  19. }
  20.  
  21. class BinTree {
  22.     BinTree left, right;
  23.     private int data;
  24.  
  25.     private static int min;
  26.     private static int max;
  27.  
  28.     BinTree(int data) {
  29.         this.data = data;
  30.     }
  31.  
  32.     int getMin() {
  33.         min = Integer.MAX_VALUE;
  34.         return this.getMinHelper();
  35.     }
  36.  
  37.     private int getMinHelper() {
  38.         if (this.data < BinTree.min) {
  39.             BinTree.min = this.data;
  40.         }
  41.  
  42.         if (this.left != null) {
  43.             this.left.getMinHelper();
  44.         }
  45.  
  46.         if (this.right != null) {
  47.             this.right.getMinHelper();
  48.         }
  49.         return BinTree.min;
  50.     }
  51.  
  52.     int getMax() {
  53.         max = Integer.MIN_VALUE;
  54.         return this.getMaxHelper();
  55.     }
  56.  
  57.     private int getMaxHelper() {
  58.         if (this.data > BinTree.max) {
  59.             BinTree.max = this.data;
  60.         }
  61.  
  62.         if (this.left != null) {
  63.             this.left.getMaxHelper();
  64.         }
  65.  
  66.         if (this.right != null) {
  67.             this.right.getMaxHelper();
  68.         }
  69.         return BinTree.max;
  70.     }
  71.  
  72.     boolean isSearchTree() {
  73.         if (this.left == null && this.right == null) {
  74.             return true;
  75.         } else if (this.left == null) {
  76.             if (this.right.getMin() > this.data) {
  77.                 return this.right.isSearchTree();
  78.             }
  79.             return false;
  80.         } else if (this.right == null) {
  81.             if (this.left.getMax() < this.data) {
  82.                 return this.left.isSearchTree();
  83.             }
  84.             return false;
  85.         } else  {
  86.             if (this.left.getMax() < this.data && this.right.getMin() > this.data) {
  87.                 return this.left.isSearchTree() && this.right.isSearchTree();
  88.             }
  89.             return false;
  90.         }
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement