KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > examples > binarytree > Tree


1 /*
2 * ################################################################
3 *
4 * ProActive: The Java(TM) library for Parallel, Distributed,
5 * Concurrent computing with Security and Mobility
6 *
7 * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8 * Contact: proactive-support@inria.fr
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
25 * Initial developer(s): The ProActive Team
26 * http://www.inria.fr/oasis/ProActive/contacts.html
27 * Contributor(s):
28 *
29 * ################################################################
30 */

31 package org.objectweb.proactive.examples.binarytree;
32
33 public class Tree {
34
35     private String JavaDoc key;
36     private String JavaDoc value;
37     private Tree left;
38     private Tree right;
39     private TreeDisplay display;
40     private Integer JavaDoc graphicDepth;
41
42     public Tree() {
43     }
44
45     public Tree(String JavaDoc key, String JavaDoc 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 JavaDoc key, String JavaDoc value, boolean AC) {
56     int res = key.compareTo(this.key);
57     if (res == 0) {
58         // Same key --> Modify the current value
59
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         // key < this.key --> store left
65
if (left != null) {
66         left.insert(key, value, AC);
67         } else {
68         display.displayMessage("[" + key + "] Creating left");
69         // Create the new node
70
try {
71             left =
72             (Tree) org.objectweb.proactive.ProActive.newActive(this.getClass().getName(), new Object JavaDoc[] { key, value, display });
73         } catch (Exception JavaDoc e) {
74             e.printStackTrace();
75         }
76         // Enabled Automatic Continuations
77
if (AC) {
78             try {
79             org.objectweb.proactive.ProActive.enableAC(org.objectweb.proactive.ProActive.getStubOnThis());
80             } catch (java.io.IOException JavaDoc 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 JavaDoc[] { key, value, display });
95         } catch (Exception JavaDoc e) {
96             e.printStackTrace();
97         }
98         // Enabled Automatic Continuations
99
if (AC) {
100             try {
101             org.objectweb.proactive.ProActive.enableAC(org.objectweb.proactive.ProActive.getStubOnThis());
102             } catch (java.io.IOException JavaDoc e) {
103             display.displayMessage("Automatic Continuations error!!!",
104                            java.awt.Color.red);
105             }
106         }
107         }
108     }
109     }
110
111     public ObjectWrapper search(String JavaDoc 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 JavaDoc getKey() {
137     return key;
138     }
139
140     public java.util.ArrayList JavaDoc getKeys() {
141     java.util.ArrayList JavaDoc keys = new java.util.ArrayList JavaDoc();
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 JavaDoc 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     // Change Automatic Continuations state
175
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 JavaDoc 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 JavaDoc e) {
196         display.displayMessage("Automatic Continuations error!!!",
197                    java.awt.Color.red);
198     }
199     }
200 }
201
Popular Tags