Advertisement
Guest User

słownik

a guest
Jan 26th, 2015
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.21 KB | None | 0 0
  1. package avl2;
  2.  
  3. import java.io.DataInputStream;
  4. import java.io.DataOutputStream;
  5. import java.io.FileInputStream;
  6. import java.io.FileNotFoundException;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9. import java.util.ArrayList;
  10. import java.util.Scanner;
  11.  
  12. public class AvlTree<AnyType extends Comparable<? super AnyType>>
  13. {
  14.  
  15.  public AvlTree( )
  16.  {
  17.      root = null;
  18.  }
  19.  
  20.  public void insert( AnyType x, ArrayList<String> s )
  21.  {
  22.      root = insert( x,s, root );
  23.  }
  24.  
  25.  public void remove( AnyType x )
  26.  {
  27.      root = remove( x, root );
  28.  }
  29.  
  30.  private AvlNode<AnyType> remove( AnyType x, AvlNode<AnyType> t )
  31.  {
  32.      if( t == null )
  33.          return t;   // Item not found; do nothing
  34.          
  35.      int compareResult = x.compareTo( t.element );
  36.          
  37.      if( compareResult < 0 ){
  38.          porównania++;
  39.          t.left = remove( x, t.left );}
  40.      else if( compareResult > 0 ){
  41.          porównania++;
  42.          t.right = remove( x, t.right );}
  43.      else if( t.left != null && t.right != null ) // Two children
  44.      {
  45.          porównania++;
  46.          t.element = findMin( t.right ).element;
  47.          t.right = remove( t.element, t.right );
  48.      }
  49.      else
  50.          t = ( t.left != null ) ? t.left : t.right;
  51.      return balance( t );
  52.  }
  53.  
  54.  public AnyType findMin( )
  55.  {
  56.      if( isEmpty( ) )
  57.          return null;
  58.      return findMin( root ).element;
  59.  }
  60.  
  61.  public AnyType findMax( )
  62.  {
  63.      if( isEmpty( ) )
  64.          return null;
  65.      return findMax( root ).element;
  66.  }
  67.  
  68.  public ArrayList<String> find( AnyType x )
  69.  {
  70.      return find( x, root );
  71.  }
  72.  
  73.  private ArrayList<String> find( AnyType x, AvlNode<AnyType> t )
  74.  {
  75.      while( t != null )
  76.          if( x.compareTo( t.element ) < 0 ){
  77.              porównania++;
  78.              t = t.left;}
  79.          else if( x.compareTo( t.element ) > 0 ){
  80.              porównania++;
  81.              t = t.right;}
  82.          else
  83.              return t.meaning;    // Match
  84.      
  85.      ArrayList<String> tmp = new ArrayList<>();
  86.      tmp.add("Brak tlumaczen slowa ....");
  87.      return tmp;   // No match
  88.  }
  89.  
  90.  public boolean contains( AnyType x )
  91.  {
  92.      return contains( x, root );
  93.  }
  94.  
  95.  public void makeEmpty( )
  96.  {
  97.      root = null;
  98.  }
  99.  
  100.  public boolean isEmpty( )
  101.  {
  102.      return root == null;
  103.  }
  104.  
  105.  public void printTree( )
  106.  {
  107.      if(isEmpty())
  108.          System.out.println( "Pusty Plik" );
  109.      else
  110.          printTree( root );
  111.  }
  112.  
  113.  public void binaryWrite() throws IOException
  114.  {
  115.      if(isEmpty())
  116.          System.out.println( "Pusty Plik" );
  117.      else
  118.      {
  119.          DataOutputStream DIS = new DataOutputStream(new FileOutputStream("file"));
  120.          DIS.close();
  121.          binaryWrite(root);
  122.      }
  123.  }
  124.  
  125.  public void binaryRead()
  126.  {
  127.      binaryRead(root);
  128.  }
  129.  
  130.  private void binaryRead( AvlNode<AnyType> t)
  131.  {
  132.          try {
  133.             DataInputStream DIS = new DataInputStream(new FileInputStream("file"));
  134.             int k = (DIS.available()/2);
  135.             String s = new String();
  136.             String bufor = new String();
  137.             ArrayList<String> mean = new ArrayList<>();
  138.             char h;
  139.             while((k--) != 0)
  140.             {
  141.                 h = DIS.readChar();
  142.                 if(h == '|')
  143.                 {
  144.                     s = bufor;
  145.                     bufor = new String();
  146.                 }
  147.                 else if(h == '@')
  148.                 {
  149.                     mean.add(bufor);
  150.                     bufor = new String();
  151.                 }
  152.                 else if(h == '#')
  153.                 {
  154.                     root = insert((AnyType)s, mean, root);
  155.                     bufor = new String();
  156.                     mean = new ArrayList<>();
  157.                 }
  158.                 else
  159.                 {
  160.                     bufor += h;
  161.                 }
  162.             }
  163.             DIS.close();
  164.         }
  165.          catch (FileNotFoundException e) {
  166.             System.out.println("Nie ma takiego pliku");
  167.          }
  168.          catch(IOException e) {
  169.               e.printStackTrace();
  170.               System.out.println("Blad I/O");
  171.          }
  172.  }
  173.  
  174.  private void  binaryWrite(AvlNode<AnyType> t) throws IOException
  175.  {
  176.      if(t != null)
  177.      {
  178.          DataOutputStream DOS = new DataOutputStream(new FileOutputStream("file", true));
  179.          DOS.writeChars((String) t.element);
  180.          DOS.writeChar('|');
  181.          for(int i =0; i < t.meaning.size(); ++i)
  182.          {
  183.              DOS.writeChars(t.meaning.get(i));
  184.              DOS.writeChar('@');
  185.          }
  186.          DOS.writeChar('#');
  187.          DOS.close();
  188.          binaryWrite(t.left);
  189.          binaryWrite(t.right);
  190.      }
  191.  }
  192.  
  193.  private static final int ALLOWED_IMBALANCE = 1;
  194.  
  195.  private AvlNode<AnyType> balance( AvlNode<AnyType> t )
  196.  {
  197.      if( t == null )
  198.          return t;
  199.      
  200.      if( height( t.left ) - height( t.right ) > ALLOWED_IMBALANCE )
  201.          if( height( t.left.left ) >= height( t.left.right ) )
  202.              t = rotateWithLeftChild( t );
  203.          else
  204.              t = doubleWithLeftChild( t );
  205.      else
  206.      if( height( t.right ) - height( t.left ) > ALLOWED_IMBALANCE )
  207.          if( height( t.right.right ) >= height( t.right.left ) )
  208.              t = rotateWithRightChild( t );
  209.          else
  210.              t = doubleWithRightChild( t );
  211.  
  212.      t.height = Math.max( height( t.left ), height( t.right ) ) + 1;
  213.      return t;
  214.  }
  215.  
  216.  public void checkBalance( )
  217.  {
  218.      checkBalance( root );
  219.  }
  220.  
  221.  private int checkBalance( AvlNode<AnyType> t )
  222.  {
  223.      if( t == null )
  224.          return -1;
  225.      
  226.      if( t != null )
  227.      {
  228.          int hl = checkBalance( t.left );
  229.          int hr = checkBalance( t.right );
  230.          if( Math.abs( height( t.left ) - height( t.right ) ) > 1 ||
  231.                  height( t.left ) != hl || height( t.right ) != hr )
  232.              System.out.println( "OOPS!!" );
  233.      }
  234.      
  235.      return height( t );
  236.  }
  237.  static int porównania=0;
  238.  private AvlNode<AnyType> insert( AnyType x,ArrayList<String> s , AvlNode<AnyType> t )
  239.  {
  240.      if( t == null )
  241.          return new AvlNode<>( x,s, null, null );
  242.      
  243.      int compareResult = x.compareTo( t.element );
  244.      
  245.      if( compareResult < 0 ){
  246.          porównania++;
  247.          t.left = insert( x,s, t.left );}
  248.      else if( compareResult > 0 ){
  249.          porównania++;
  250.          t.right = insert( x,s, t.right );}
  251.      else
  252.          ;  // Duplicate; do nothing
  253.      return balance( t );
  254.  }
  255.  
  256.  private AvlNode<AnyType> findMin( AvlNode<AnyType> t )
  257.  {
  258.      if( t == null )
  259.          return t;
  260.  
  261.      while( t.left != null )
  262.          t = t.left;
  263.      return t;
  264.  }
  265.  
  266.  private AvlNode<AnyType> findMax( AvlNode<AnyType> t )
  267.  {
  268.      if( t == null )
  269.          return t;
  270.  
  271.      while( t.right != null )
  272.          t = t.right;
  273.      return t;
  274.  }
  275.  
  276.  private boolean contains( AnyType x, AvlNode<AnyType> t )
  277.  {
  278.      while( t != null )
  279.      {
  280.          int compareResult = x.compareTo( t.element );
  281.          
  282.          if( compareResult < 0 )
  283.              t = t.left;
  284.          else if( compareResult > 0 )
  285.              t = t.right;
  286.          else
  287.              return true;    // Match
  288.      }
  289.  
  290.      return false;   // No match
  291.  }
  292.  
  293.  private void printTree( AvlNode<AnyType> t )
  294.  {
  295.      if( t != null )
  296.      {
  297.          printTree( t.left );
  298.          System.out.println( t.element );
  299.          printTree( t.right );
  300.      }
  301.  }
  302.  
  303.  private int height( AvlNode<AnyType> t )
  304.  {
  305.      return t == null ? -1 : t.height;
  306.  }
  307. static int rotacja=0;
  308.  private AvlNode<AnyType> rotateWithLeftChild( AvlNode<AnyType> k2 )
  309.  {
  310.      AvlNode<AnyType> k1 = k2.left;
  311.      k2.left = k1.right;
  312.      k1.right = k2;
  313.      k2.height = Math.max( height( k2.left ), height( k2.right ) ) + 1;
  314.      k1.height = Math.max( height( k1.left ), k2.height ) + 1;
  315.      rotacja++;
  316.      return k1;
  317.  }
  318.  
  319.  private AvlNode<AnyType> rotateWithRightChild( AvlNode<AnyType> k1 )
  320.  {
  321.      AvlNode<AnyType> k2 = k1.right;
  322.      k1.right = k2.left;
  323.      k2.left = k1;
  324.      k1.height = Math.max( height( k1.left ), height( k1.right ) ) + 1;
  325.      k2.height = Math.max( height( k2.right ), k1.height ) + 1;
  326.      rotacja++;
  327.      return k2;
  328.  }
  329.  static int rotacja2=0;
  330.  private AvlNode<AnyType> doubleWithLeftChild( AvlNode<AnyType> k3 )
  331.  {
  332.      k3.left = rotateWithRightChild( k3.left );
  333.      rotacja2++;
  334.      return rotateWithLeftChild( k3 );
  335.  }
  336.  
  337.  private AvlNode<AnyType> doubleWithRightChild( AvlNode<AnyType> k1 )
  338.  {
  339.      k1.right = rotateWithLeftChild( k1.right );
  340.      rotacja2++;
  341.      return rotateWithRightChild( k1 );
  342.  }
  343.  
  344.  private static class AvlNode<AnyType>
  345.  {
  346.      AvlNode( AnyType theElement,ArrayList<String> m ,AvlNode<AnyType> lt, AvlNode<AnyType> rt )
  347.      {
  348.          element  = theElement;
  349.          meaning =  m;
  350.          left     = lt;
  351.          right    = rt;
  352.          height   = 0;
  353.      }
  354.  
  355.      AnyType           element;      // The data in the node
  356.      ArrayList<String> meaning;
  357.      AvlNode<AnyType>  left;         // Left child
  358.      AvlNode<AnyType>  right;        // Right child
  359.      int               height;       // Height
  360.  }
  361.  
  362.  
  363.  private AvlNode<AnyType> root;
  364.  
  365.  public static void main( String [ ] args ) throws IOException
  366.  {
  367.      AvlTree<String> t = new AvlTree<>( );
  368.      Scanner sc = new Scanner(System.in);
  369.      int opt = 1;
  370.      while(opt != 0)
  371.      {
  372.          System.out.println("rotacje1:"+rotacja);
  373.          System.out.println("rotacje2:"+rotacja2);
  374.          System.out.println("porównania:"+porównania);
  375.         System.out.println("0- Zakoncz program, 1- Wstaw, 2- Szukaj, 3- Skasuj, 4- Zapisz slownik, 5- Wczytaj slownik, 6- Wypisz elementy slownika");
  376.         opt = sc.nextInt();
  377.         switch(opt)
  378.         {
  379.             case 1:
  380.             {
  381.                 System.out.println("Podaj slowo do wstawienia");
  382.                 String s = sc.next();
  383.                 System.out.println("Podaj tlumaczenie, 0 - koniec");
  384.                 ArrayList<String> meanings = new ArrayList<>();
  385.                 String e;
  386.                 while(true)
  387.                 {
  388.                     e = sc.next();
  389.                     if(e.equals("0"))
  390.                       break;
  391.                     meanings.add(e);
  392.                 }
  393.                 t.insert(s, meanings);
  394.                 break;
  395.             }
  396.             case 2:
  397.             {
  398.                 System.out.println("Podaj slowo do tlumaczenia");
  399.                 String s = sc.next();
  400.                 System.out.println(t.find(s));
  401.                
  402.                 break;
  403.             }
  404.             case 3:
  405.             {
  406.                 System.out.println("podaj slowo do skasowania");
  407.                 String s = sc.next();
  408.                 t.remove(s);
  409.                 break;
  410.             }
  411.             case 4:
  412.             {
  413.                
  414.                 t.binaryWrite();
  415.                 break;
  416.             }
  417.             case 5:
  418.             {
  419.                 t.binaryRead();
  420.                 break;
  421.             }
  422.             case 6:
  423.             {
  424.                 t.printTree();
  425.                 break;
  426.             }
  427.             default:
  428.             {
  429.                 opt = 0;
  430.                 break;
  431.             }
  432.        
  433.         }      
  434.      
  435.      }
  436.      sc.close();
  437.  }
  438. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement