1 31 package org.objectweb.proactive.examples.binarytree; 32 33 public class Tree { 34 35 private String key; 36 private String value; 37 private Tree left; 38 private Tree right; 39 private TreeDisplay display; 40 private Integer graphicDepth; 41 42 public Tree() { 43 } 44 45 public Tree(String key, String value, TreeDisplay display) { 46 this.left = null; 47 this.right = null; 48 this.key = key; 49 this.value = value; 50 this.display = display; 51 display.displayMessage("[" + key + "] Created with value " 52 + value, java.awt.Color.blue); 53 } 54 55 public void insert(String key, String value, boolean AC) { 56 int res = key.compareTo(this.key); 57 if (res == 0) { 58 display.displayMessage("[" + key + "] Replacing " 60 + this.value + " with " + value); 61 this.value = value; 62 } else if (res < 0) { 63 display.displayMessage("[" + key + "] trying left"); 64 if (left != null) { 66 left.insert(key, value, AC); 67 } else { 68 display.displayMessage("[" + key + "] Creating left"); 69 try { 71 left = 72 (Tree) org.objectweb.proactive.ProActive.newActive(this.getClass().getName(), new Object [] { key, value, display }); 73 } catch (Exception e) { 74 e.printStackTrace(); 75 } 76 if (AC) { 78 try { 79 org.objectweb.proactive.ProActive.enableAC(org.objectweb.proactive.ProActive.getStubOnThis()); 80 } catch (java.io.IOException e) { 81 display.displayMessage("Automatic Continuations error!!!", 82 java.awt.Color.red); 83 } 84 } 85 } 86 } else { 87 display.displayMessage("[" + key + "] trying right"); 88 if (right != null) { 89 right.insert(key, value, AC); 90 } else { 91 display.displayMessage("[" + key + "] Creating right"); 92 try { 93 right = 94 (Tree) org.objectweb.proactive.ProActive.newActive(this.getClass().getName(), new Object [] { key, value, display }); 95 } catch (Exception e) { 96 e.printStackTrace(); 97 } 98 if (AC) { 100 try { 101 org.objectweb.proactive.ProActive.enableAC(org.objectweb.proactive.ProActive.getStubOnThis()); 102 } catch (java.io.IOException e) { 103 display.displayMessage("Automatic Continuations error!!!", 104 java.awt.Color.red); 105 } 106 } 107 } 108 } 109 } 110 111 public ObjectWrapper search(String key) { 112 display.displayMessage("[" + this.key + "] Searching for " + key); 113 if (key == null) 114 return new ObjectWrapper("null");; 115 116 int res = key.compareTo(this.key); 117 if (res == 0) { 118 display.displayMessage("[" + this.key + "] Found " + key); 119 return new ObjectWrapper(value); 120 } 121 if (res < 0) 122 return (left != null) ? left.search(key) : new ObjectWrapper("null"); 123 else 124 return (right != null) ? right.search(key) : new ObjectWrapper("null"); 125 } 126 127 public void delete() { 128 if (right != null) 129 right.delete(); 130 if (left != null) 131 left.delete(); 132 (org.objectweb.proactive.ProActive.getBodyOnThis()).terminate(); 133 } 134 135 136 public String getKey() { 137 return key; 138 } 139 140 public java.util.ArrayList getKeys() { 141 java.util.ArrayList keys = new java.util.ArrayList (); 142 if (key != null) 143 keys.add(key); 144 if (right != null) 145 keys.addAll(right.getKeys()); 146 if (left != null) 147 keys.addAll(left.getKeys()); 148 return keys; 149 } 150 151 public String getValue() { 152 return value; 153 } 154 155 public Tree getLeft() { 156 return left; 157 } 158 159 public Tree getRight() { 160 return right; 161 } 162 163 public int depth() { 164 int rightDepth = 0, leftDepth = 0; 165 if (right != null) 166 rightDepth = right.depth(); 167 if (left != null) 168 leftDepth = left.depth(); 169 if (leftDepth < rightDepth) 170 return ++rightDepth; 171 return ++leftDepth; 172 } 173 174 public void enableAC() { 176 try { 177 org.objectweb.proactive.ProActive.enableAC(org.objectweb.proactive.ProActive.getStubOnThis()); 178 if (right != null) 179 right.enableAC(); 180 if (left != null) 181 left.enableAC(); 182 } catch (java.io.IOException e) { 183 display.displayMessage("Automatic Continuations error!!!", 184 java.awt.Color.red); 185 } 186 } 187 188 public void disableAC() { 189 try { 190 org.objectweb.proactive.ProActive.disableAC(org.objectweb.proactive.ProActive.getStubOnThis()); 191 if (right != null) 192 right.disableAC(); 193 if (left != null) 194 left.disableAC(); 195 } catch (java.io.IOException e) { 196 display.displayMessage("Automatic Continuations error!!!", 197 java.awt.Color.red); 198 } 199 } 200 } 201 | Popular Tags |