Manioc

DST

Jul 22nd, 2018
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.03 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class BST {
  4.     private static Scanner sc;
  5.    
  6.     static class Node{
  7.         private Node left;
  8.         private Node right;
  9.         private Node parent;
  10.         private Integer valor;
  11.        
  12.         public Node() {
  13.             left = null;
  14.             right = null;
  15.             parent = null;
  16.             valor = null;
  17.         }
  18.        
  19.         public Node(Integer v) {
  20.             left = null;
  21.             right = null;
  22.             parent = null;
  23.             valor = v;
  24.         }
  25.         public Node(Integer v,Node l, Node r, Node p) {
  26.             left = l;
  27.             right = r;
  28.             parent = p;
  29.             valor = v;
  30.         }
  31.        
  32.         public Node getLeft() {return this.left;}
  33.        
  34.         public Node getRight() {return this.right;}
  35.        
  36.         public Node getParent() {return this.parent;}
  37.        
  38.         public Integer getValor() {return this.valor; }
  39.        
  40.         public void setLeft(Node l) { this.left = l; }
  41.        
  42.         public void setRight(Node r) { this.right = r; }
  43.        
  44.         public void setParent(Node p) { this.parent = p; }
  45.        
  46.         public void setValor(Integer v) {this.valor = v;}
  47.        
  48.         public boolean empty() { return this.valor == null;}
  49.        
  50.         public boolean isLeaf() {return this.left.getValor() == null && this.right.getValor() == null;}
  51.     }
  52.    
  53.     static class Tree{
  54.         private Node root;
  55.         private String ans;
  56.        
  57.         public Tree() {
  58.             root = new Node();
  59.         }
  60.         public void insert(Integer element) {
  61.             if(element != null)
  62.             insert(root, element);
  63.         }
  64.        
  65.         private void insert(Node no, int element) {
  66.             if(no.empty()) {
  67.                 no.setValor(element);
  68.                 no.setLeft(new Node());
  69.                 no.setRight(new Node());
  70.                 no.getLeft().setParent(no);
  71.                 no.getRight().setParent(no);
  72.             }
  73.             else {
  74.                 if(element > no.getValor()) insert(no.getRight(), element);
  75.                 else if(element < no.getValor()) insert(no.getLeft(), element);
  76.             }
  77.         }
  78.        
  79.         public int height() {
  80.             return height(root)-1;
  81.         }
  82.        
  83.         public int height(Node no) {
  84.             if(no.empty()) return 0;
  85.             return Math.max(this.height(no.getLeft()), this.height(no.getRight())) + 1;
  86.         }
  87.        
  88.         public Integer search(Integer element) {
  89.             return search(root, element).getValor();
  90.         }
  91.        
  92.         public Node search(Node no, Integer element) {
  93.             if(no != null && !no.empty()) {
  94.                 //System.out.print(no.getValor() + ", ");
  95.                 this.ans += no.getValor() + ", ";
  96.                
  97.                 if(element > no.getValor() && no.getRight() != null) return search(no.getRight(), element);
  98.                 else if(element < no.getValor() && no.getLeft() != null) return search(no.getLeft(), element);
  99.                
  100.                 return no;
  101.             }
  102.            
  103.             Node painho = no.getParent().getParent();
  104.             Node bambam = no.getParent();
  105.             while(painho != null && Math.abs(painho.getValor()-element) < Math.abs(bambam.getValor()-element)) {
  106.                 bambam = painho;
  107.                 painho = painho.getParent();
  108.             }
  109.            
  110.             return bambam;
  111.         }
  112.        
  113.         public Node maximum() {
  114.            return maximum(root);
  115.         }
  116.  
  117.         private Node maximum(Node no) {
  118.             ans += no.getValor() + ", ";
  119.             if (no.getRight().empty()) return no;
  120.             return maximum(no.getRight());
  121.         }
  122.  
  123.        public Node minimum() {
  124.            return minimum(this.root);
  125.        }
  126.  
  127.        private Node minimum(Node no) {
  128.            //ans += no.getValor() + ", ";
  129.           if (no.getLeft().empty()) return no;
  130.           return minimum(no.getLeft());
  131.        }
  132.        
  133.        private Node alternativeParent(Node no, int element, boolean tipo){
  134.            Node dad = no.getParent();
  135.            //if(dad != null) ans += dad.getValor() + ", ";
  136.            if((dad == null || dad.getValor().compareTo(element) < 0) && !tipo) return dad;
  137.            if((dad == null || dad.getValor().compareTo(element) > 0) && tipo) return dad;
  138.            
  139.            return alternativeParent(dad, element, tipo);
  140.        }
  141.        
  142. //     public Node sucessor(int element) {
  143. //           Node no = search(element);
  144. //           if(no != null && !no.empty()) {
  145. //               //ans += no.getValor() + ", ";
  146. //               if (!no.getRight().empty()) {
  147. //                  return minimum(no.getRight());
  148. //              }else {
  149. //                  return alternativeParent(no, no.getValor(), true);
  150. //              }
  151. //           }
  152. //           return null;
  153. //     }
  154. //
  155. //     public String predecessor(int element) {
  156. //         Node no = search(element);
  157. //         ans = "";
  158. //         if(no != null && !no.empty()) {
  159. //             ans += no.getValor() + ", ";
  160. //             if (!no.getLeft().empty()) {
  161. //                 maximum(no.getLeft());
  162. //             }else {
  163. //                 alternativeParent(no, element, false);
  164. //             }
  165. //         }
  166. //         return "[" + ans.substring(0, ans.length()-2) + "]";
  167. //     }
  168. //     
  169. //     public void remove(int element) {
  170. //         Node no = search(element);
  171. //         remove(no);
  172. //     }
  173. //
  174. //     private void remove(Node no) {
  175. //        if(no != null && !no.empty()) {
  176. //            if(no.isLeaf()) {
  177. //                replace(no, new Node());
  178. //            }else if(no.getRight().empty()) {
  179. //                replace(no, no.getLeft());
  180. //            }else if(no.getLeft().empty()) {   
  181. //                replace(no, no.getRight());
  182. //            }else {
  183. //                Node sucess = sucessor(no.getValor());
  184. //                no.setValor(sucess.getValor());
  185. //                remove(sucess);
  186. //            }
  187. //        }
  188. //     }
  189. //     
  190. //     private void replace(Node no, Node set) {
  191. //         Node dad = no.getParent();
  192. //         if(dad == null) {
  193. //             this.root = set;
  194. //             set.setParent(null);
  195. //         }else {
  196. //            if(dad.getValor() <= no.getValor()) {
  197. //                dad.setRight(set);
  198. //                dad.getRight().setParent(dad);
  199. //            }else {
  200. //                dad.setLeft(set);
  201. //                dad.getLeft().setParent(dad);
  202. //            }
  203. //         }
  204. //     }
  205.  
  206.        public int[] preOrder() {
  207.           int[] arr = new int[this.size()];
  208.           if(!root.empty()) preOrder(this.root, arr, 0);
  209.           return arr;
  210.        }
  211.  
  212.        private int preOrder(Node no, int[] arr, int idx) {
  213.            if(!no.empty()) {
  214.               arr[idx++] = no.getValor();
  215.               idx = preOrder(no.getLeft(), arr, idx);
  216.               idx = preOrder(no.getRight(), arr, idx);
  217.            }
  218.            return idx;
  219.        }
  220.        
  221.        public int size() {
  222.           return size(root);
  223.        }
  224.  
  225.        private int size(Node no) {
  226.           int result = 0;
  227.           if (!no.empty()) {
  228.              result = 1 + size(no.getLeft()) + size( no.getRight());
  229.           }
  230.           return result;
  231.        }
  232.        
  233.        public int leaf() {
  234.            return leaf(root);
  235.        }
  236.        
  237.        private int leaf(Node no) {
  238.            int numFolhas = 0;
  239.             if (no.getValor() == null)
  240.                 return 0;
  241.             if (no.isLeaf()) {
  242.                 return 1;
  243.             } else {
  244.                 numFolhas = leaf(no.getLeft()) + leaf(no.getRight());
  245.             }
  246.  
  247.             return numFolhas;
  248.        }
  249.        
  250.        public String bfs() {
  251.            ans = "";
  252.            Queue<Node> q = new LinkedList<>();
  253.            q.add(root);
  254.            
  255.            while(!q.isEmpty()) {
  256.                Node top = q.poll();
  257.                ans += top.getValor() + " ";
  258.                if(top.getRight() != null && !top.getRight().empty()) q.add(top.getRight());
  259.                if(top.getLeft() != null && !top.getLeft().empty()) q.add(top.getLeft());
  260.                
  261.            }
  262.            
  263.            return ans.substring(0, ans.length()-1);
  264.        }
  265.        
  266.        public int proximo(Integer element) {
  267.            return proximo(element, root).getValor();
  268.        }
  269.        
  270.        public Node comp(Node l, Node r, int element) {
  271.             if(l != null && !l.empty() && r != null && !r.empty()) {
  272.                 if(Math.abs(l.getValor()-element) < Math.abs(r.getValor()-element)) {
  273.                     return l;
  274.                 }else return r;
  275.             }else if(l != null && !l.empty()) return l;
  276.             else if(r != null && !r.empty()) return r;
  277.             return null;
  278.         }
  279.        public Node proximo(Integer element, Node no) {
  280.            
  281.            if(no.isLeaf() || no.getValor() == element) return no;
  282.            
  283.            Node bestParent = proximo(element,comp(no.getLeft(), no.getRight(), element));
  284.            
  285.            int dif = Math.abs(no.getValor()-element);
  286.            if(dif > Math.abs(bestParent.getValor()-element)) return bestParent;
  287.            else return no;
  288.        }
  289.     }
  290.     public static void main(String[] args) {
  291.         sc = new Scanner(System.in);
  292.        
  293.         String[] vals = sc.nextLine().split(" ");
  294.         int[] arr = new int[vals.length];
  295.         Tree DST = new Tree();
  296.         for(int i = 0; i < vals.length; i++) {
  297.             arr[i] = Integer.parseInt(vals[i]);
  298.             DST.insert(arr[i]);
  299.         }
  300.        
  301.         int elem = sc.nextInt();
  302.         System.out.println(Arrays.toString(DST.preOrder()));
  303.         System.out.println(DST.proximo(elem));
  304.     }
  305.  
  306. }
Advertisement
Add Comment
Please, Sign In to add comment