Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. package aed_gabriel;
  2.  
  3.  
  4. public class AvlTree
  5. {
  6. private AvlNode root;
  7.  
  8. public AvlNode getRoot(){
  9. return root;
  10. }
  11.  
  12. public AvlTree( )
  13. {
  14. root = null;
  15. }
  16.  
  17.  
  18. public void add( Pais p )
  19. {
  20. root = insert( p, root );
  21. }
  22.  
  23.  
  24. public void remove( Pais p )
  25. {
  26. System.out.println( "Sorry, remove unimplemented" );
  27. }
  28.  
  29.  
  30.  
  31.  
  32.  
  33. public Comparable find( Comparable x )
  34. {
  35. return elementAt( find( x, root ) );
  36. }
  37.  
  38.  
  39.  
  40. private AvlNode find( Comparable x, AvlNode t )
  41. {
  42. System.out.println("sdadas");
  43.  
  44. while( t != null )
  45. // System.out.printf("search %s with %s\n", ((Pais) t.pais).getNome(),((Pais) x).getNome() );
  46. if( x.compareTo(t.pais ) < 0 )
  47. t = t.left;
  48. else if( x.compareTo(t.pais ) > 0 )
  49. t = t.right;
  50. else
  51. return t; // Match
  52.  
  53. return null; // No match
  54. }
  55.  
  56.  
  57.  
  58.  
  59. public void printTree( )
  60. {
  61. if( root==null )
  62. System.out.println( "root==null" );
  63. else
  64. printTree( root );
  65. }
  66.  
  67.  
  68. private Comparable elementAt( AvlNode t )
  69. {
  70. if(t != null){
  71. return t.pais;
  72. }
  73. else return null;
  74.  
  75. }
  76.  
  77.  
  78. private AvlNode insert( Comparable x, AvlNode t )
  79. {
  80. if( t == null )
  81. t = new AvlNode( x, null, null );
  82. else if( x.compareTo(t.pais ) < 0 )
  83. {
  84. t.left = insert( x, t.left );
  85. if( altura( t.left ) - altura( t.right ) == 2 )
  86. if( x.compareTo(t.left.pais ) < 0 )
  87. t = rotateWithLeftChild( t );
  88. else
  89. t = doubleWithLeftChild( t );
  90. }
  91. else if( x.compareTo(t.pais ) > 0 )
  92. {
  93. t.right = insert( x, t.right );
  94. if( altura( t.right ) - altura( t.left ) == 2 )
  95. if( x.compareTo(t.right.pais ) > 0 )
  96. t = rotateWithRightChild( t );
  97. else
  98. t = doubleWithRightChild( t );
  99. }
  100. else
  101.  
  102. t.altura = max( altura( t.left ), altura( t.right ) ) + 1;
  103. return t;
  104. }
  105.  
  106.  
  107.  
  108.  
  109.  
  110. private void printTree( AvlNode t )
  111. {
  112. if( t != null )
  113. {
  114. printTree( t.left );
  115. System.out.printf("%s\t\t\t\t%s\n" ,((Pais) t.pais).getNome(),((Pais) t.pais).getSigla() );
  116. printTree( t.right );
  117. }
  118. }
  119.  
  120.  
  121. private static int altura( AvlNode t )
  122. {
  123. if ( t == null) return -1;
  124.  
  125. int altura = t.altura;
  126.  
  127. return altura;
  128. }
  129.  
  130.  
  131. private static int max( int a, int b )
  132. {
  133. return a > b ? a : b;
  134. }
  135.  
  136.  
  137. private static AvlNode rotateWithLeftChild( AvlNode k2 )
  138. {
  139. AvlNode k1 = k2.left;
  140. k2.left = k1.right;
  141. k1.right = k2;
  142. k2.altura = max( altura( k2.left ), altura( k2.right ) ) + 1;
  143. k1.altura = max( altura( k1.left ), k2.altura ) + 1;
  144. return k1;
  145. }
  146.  
  147.  
  148. private static AvlNode rotateWithRightChild( AvlNode k1 )
  149. {
  150. AvlNode k2 = k1.right;
  151. k1.right = k2.left;
  152. k2.left = k1;
  153. k1.altura = max( altura( k1.left ), altura( k1.right ) ) + 1;
  154. k2.altura = max( altura( k2.right ), k1.altura ) + 1;
  155. return k2;
  156. }
  157.  
  158. private static AvlNode doubleWithLeftChild( AvlNode k3 )
  159. {
  160. k3.left = rotateWithRightChild( k3.left );
  161. return rotateWithLeftChild( k3 );
  162. }
  163.  
  164.  
  165. private static AvlNode doubleWithRightChild( AvlNode k1 )
  166. {
  167. k1.right = rotateWithLeftChild( k1.right );
  168. return rotateWithRightChild( k1 );
  169. }
  170.  
  171.  
  172.  
  173.  
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement