Advertisement
Guest User

Untitled

a guest
Mar 5th, 2015
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. // Notkun: StringTreeNode t2 = insertIntoTree(t1,s);
  2. // Fyrir: t1 er tvíleitartré, má vera tómt.
  3. // Eftir: t2 er tvíleitartré sem inniheldur öll gildin
  4. // úr t1 og einnig hnút með strengnum s.
  5. public static StringTreeNode insertIntoTree( StringTreeNode t, String s )
  6. {
  7. if( t == null )
  8. t = new StringTreeNode(s, null, null);
  9. else if( s.compareTo( t.val ) < 0 )
  10. t.left = insertIntoTree( t.left, s);
  11. else if( s.compareTo( t.val ) > 0 )
  12. t.right = insertIntoTree( t.right, s );
  13. else if( s.compareTo( t.val) == 0 )
  14. t.right = insertIntoTree( t.right, s);
  15. return t;
  16. }
  17.  
  18. // Notkun: String s = minInTree(t);
  19. // Fyrir: t er tvíleitartré, ekki tómt.
  20. // Eftir: s er minnsta gildið í t.
  21. public static String minInTree( StringTreeNode t )
  22. {
  23. if(t.left == null) return t.val;
  24. return minInTree(t.left);
  25. }
  26.  
  27. // Notkun: deleteMinInTree(t);
  28. // Fyrir: t er tvíleitartré, ekki tómt.
  29. // Eftir: búið er að eyða minnsta gildi úr tréinu t.
  30. public static StringTreeNode deleteMinInTree( StringTreeNode t )
  31. {
  32. if(t.left != null){
  33.  
  34. t.left = deleteMinInTree(t.left);
  35. return t;
  36. }
  37. else return t.right;
  38.  
  39. }
  40.  
  41.  
  42. // Notkun: StringTreeNode t2 = deleteFromTree(t1,s);
  43. // Fyrir: t1 er tvíleitartré, má vera tómt.
  44. // Eftir: t2 er tvíleitartré sem inniheldur öll gildin
  45. // úr t1 nema hvað búið er að fjarlægja einn hnút
  46. // með gildinu s ef einhver slíkur hnútur var til
  47. // staðar.
  48. public static StringTreeNode deleteFromTree( StringTreeNode t, String s )
  49. {
  50. if( t == null ) return null;
  51. if( s.compareTo( t.val ) < 0 )
  52. t.left = deleteFromTree( t.left, s );
  53. else if( s.compareTo( t.val ) > 0 )
  54. t.right = deleteFromTree( t.right, s);
  55. else if( t.left != null && t.right != null )
  56. {
  57. StringTreeNode x = new StringTreeNode(minInTree(t.right), t.left, t.right);
  58. t = x;
  59. t.right = deleteMinInTree( t.right );
  60. } else
  61. t = ( t.left != null ) ? t.left : t.right;
  62. return t;
  63. }
  64.  
  65. // EKKI BREYTA HÉR FYRIR NEÐAN
  66.  
  67. private static int is( StringTreeNode t, int i, String[] a )
  68. {
  69. if( t==null ) return i;
  70. if( t.left!=null ) i = is(t.left,i,a);
  71. if( !a[i].equals(t.val) ) throw new Error();
  72. return is(t.right,i+1,a);
  73. }
  74.  
  75. private static boolean is( StringTreeNode t, String[] a )
  76. {
  77. try
  78. {
  79. int i = is(t,0,a);
  80. return i==a.length;
  81. }
  82. catch( Error e )
  83. {
  84. return false;
  85. }
  86. }
  87.  
  88. public static String insertAndDelete()
  89. {
  90. StringTreeNode t;
  91. String res = "Villa";
  92. t = null;
  93. t = insertIntoTree(t,"a");
  94. if( !is(t,new String[]{"a"}) ) return "A";
  95. t = insertIntoTree(t,"b");
  96. if( !is(t,new String[]{"a","b"}) ) return "B";
  97. t = insertIntoTree(t,"c");
  98. if( !is(t,new String[]{"a","b","c"}) ) return "C";
  99. t = insertIntoTree(t,"x");
  100. if( !is(t,new String[]{"a","b","c","x"}) ) return "X";
  101. t = insertIntoTree(t,"p");
  102. if( !is(t,new String[]{"a","b","c","p","x"}) ) return "P";
  103. t = insertIntoTree(t,"p");
  104. if( !is(t,new String[]{"a","b","c","p","p","x"}) ) return "P2";
  105. t = deleteFromTree(t,"a");
  106. if( !is(t,new String[]{"b","c","p","p","x"}) ) return "DA";
  107. t = deleteFromTree(t,"p");
  108. if( !is(t,new String[]{"b","c","p","x"}) ) return "DP";
  109. return "Engin villa";
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement