Advertisement
alvsjo

BST

May 10th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.82 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. import java.util.*;
  7.  
  8. public class BST  <Key extends Comparable<Key>>{
  9.  
  10.     private class Node{
  11.         Key info;
  12.         Node left,right;
  13.         public Node(Key info, BST<Key>.Node left, BST<Key>.Node right) {
  14.             this.info = info;
  15.             this.left = left;
  16.             this.right = right;
  17.         }
  18.        
  19.     }
  20.    
  21.    
  22.     Node root;
  23.  
  24.  
  25.     public BST(BST<Key>.Node root) {
  26.         super();
  27.         this.root = root;
  28.     }
  29.    
  30.     private Node insert(Node x, Key k)
  31.     {
  32.         if (x==null)
  33.         {
  34.             x =new Node(k,null,null);
  35.         }
  36.         else //drvo nije prazno
  37.         {
  38.         //  if (k.compareTo(x.info)==0) return null;
  39.             if(k.compareTo(x.info)<0)
  40.             {
  41.                 //idi lijevo
  42.                 x.left=insert(x.left,k);
  43.             }
  44.             else
  45.             {
  46.                 //idi desno
  47.                 x.right=insert(x.right,k);
  48.             }
  49.         }
  50.         return x;
  51.     }
  52.    
  53.     public void insert(Key k)
  54.     {
  55.         root=insert(root,k);
  56.     }
  57.    
  58.     private Node delete (Node x,Key k)
  59.     {
  60.         if (x==null) return x;
  61.         int cmp=k.compareTo(x.info);
  62.         if(cmp<0) x.left=delete(x.left,k);
  63.         else if(cmp>0) x.right=delete(x.right,k);
  64.         else
  65.         {
  66.             if(x.left==null)
  67.                 x=x.right;
  68.             else if(x.right==null) x=x.left;
  69.             else
  70.             {
  71.                 Node t=x.right;
  72.                 while(t.left!=null) t=t.left;
  73.                 x.info=t.info;
  74.                 x.right=delete(x.right,t.info);
  75.             }
  76.         }
  77.         return x;
  78.     }
  79.    
  80.     public void delete(Key k)
  81.     {
  82.         root=delete(root,k);
  83.     }
  84.    
  85.    
  86.    
  87.     private void inorder (Node x)
  88.     {
  89.         if(x==null)
  90.             return;
  91.         inorder(x.left);
  92.         System.out.println(x.info);
  93.         inorder(x.right);
  94.     }
  95.    
  96.     public void inorder()
  97.     {
  98.         inorder(root);
  99.     }
  100.    
  101.     private int broj (Node x)
  102.     {
  103.         if(x==null) return 0;
  104.         return 1+broj(x.left)+broj(x.right);
  105.     }
  106.    
  107.     public int size()
  108.     {
  109.         return broj(root);
  110.     }
  111.    
  112.     private Node getElR(Node n, Key x)
  113.     {
  114.         if(n==null) return null;
  115.         if(x==null) throw new IllegalArgumentException("argument u metodu getEl je null :(");
  116.         int cmp=x.compareTo(n.info);
  117.         if (cmp==0) return n;
  118.         if(cmp<0) return getElR(n.left,x);
  119.         return getElR(n.right,x);
  120.     }
  121.     public Node getR(Key k)
  122.     {
  123.         return getElR(root,k);
  124.     }
  125.        
  126.    
  127.    
  128.     private Node getElN(Node n, Key x)
  129.     {
  130.         if(n==null) return null;
  131.         if(x==null) throw new IllegalArgumentException("argument u metodu getEl je null :(");
  132.         int cmp=0;
  133.         Node curr =n;
  134.         while (curr != null)
  135.         {
  136.             cmp=x.compareTo(curr.info);
  137.             if (cmp==0) return curr;
  138.             if(cmp<0) curr=curr.left;
  139.             else curr=curr.right;
  140.         }
  141.         return null;
  142.     }
  143.     public Node getN(Key k)
  144.     {
  145.         return getElN(root,k);
  146.     }
  147.    
  148.    
  149.     public boolean contains(Key k)
  150.     {
  151.         return getR(k)!=null;
  152.     }
  153.    
  154.     //dubina stabla
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.     public ArrayList<Integer> djelioci(int n)
  167.     {
  168.         /*  ArrayList<Integer> res= new ArrayList<>();
  169.         //rjesenje slozenosti O(n)
  170.         res.add(1);
  171.         for (int i=2;i<=n/2;i++)
  172.         {
  173.             if (n%i==0)
  174.             {
  175.                 res.add(i);
  176.             }
  177.            
  178.         }
  179.         if(n>1)res.add(n);
  180.  
  181.         return res;
  182.     */ 
  183.    
  184.         ArrayList<Integer> res= new ArrayList<>();
  185.         //rjesenje slozenosti O(sqrt(n))
  186.         int i;
  187.         for (i=1;i*i<n;i++)
  188.         {
  189.             if (n%i==0)
  190.             {
  191.                 res.add(i);
  192.                 res.add(n/i);
  193.             }
  194.         }
  195.         if(i*i==n)res.add(i);
  196.         return res;
  197.        
  198.     }
  199.  
  200.     ArrayList<Integer> sito(int n)
  201.     {
  202.         return null;
  203.        
  204.     }
  205.  
  206.  
  207.     public void printLevel()
  208.     {
  209.         Node n= root;
  210.         Queue<Node> red = new LinkedList<>();
  211.         red.offer(n);
  212.         while(!red.isEmpty())
  213.         {
  214.             n=red.poll();
  215.             System.out.println(n.info);
  216.             if(n.left!=null) red.offer(n.left);
  217.             if(n.right!=null) red.offer(n.right);
  218.         }
  219.     }
  220.  
  221.     public void inorderNRec()
  222.     {
  223.         Node t= root;
  224.         Deque<Node> stek=new ArrayDeque<>();
  225.         while (t!=null)
  226.         {
  227.             stek.addFirst(t);
  228.             t=t.left;
  229.         }
  230.         while(!stek.isEmpty())
  231.         {
  232.             t=stek.removeFirst();
  233.             System.out.println(t.info);
  234.             t=t.right;
  235.             while (t!=null)
  236.             {
  237.                 stek.addFirst(t);
  238.                 t=t.left;
  239.             }
  240.         }
  241.    
  242.     }
  243.  
  244. // Tree, Hash, LinkedHash +(Set || Map)
  245.  
  246.     public void duplikati(String filename)
  247.     {
  248.         Set<String> jed = new HashSet<>();
  249.         Set<String> dup = new TreeSet<>();
  250.         BufferedReader br;
  251.         String line="";
  252.         String [] rijeci;
  253.         try {
  254.             br = new BufferedReader(new FileReader(new File(filename)));
  255.             while ((line= br.readLine())!=null)
  256.             {
  257.                 rijeci=line.split("\\b");
  258.                 for(String s:rijeci)
  259.                 {
  260.                     boolean b=jed.add(s);
  261.                     if (!b)
  262.                     {
  263.                         dup.add(s);
  264.                     }
  265.                 }
  266.                
  267.             }
  268.             jed.removeAll(dup);
  269.             System.out.println("jed: "+jed.size()+" "+jed);
  270.             System.out.println("dup: "+dup.size()+" "+dup);
  271.             Iterator <String> it;
  272.             for( it=jed.iterator(); it.hasNext();)
  273.             {
  274.                 if(it.next().length()<2) it.remove();
  275.             }
  276.             System.out.println("jed: "+jed.size()+" "+jed);
  277.  
  278.         } catch (FileNotFoundException e) {
  279.             // TODO Auto-generated catch block
  280.             e.printStackTrace();
  281.         } catch (IOException e) {
  282.             // TODO Auto-generated catch block
  283.             e.printStackTrace();
  284.         }
  285.        
  286.     }
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement