Download code

Jump to: navigation, search

Back to Generic_tree_(Java)

Download for Windows: single file, zip

Download for UNIX: single file, zip, tar.gz, tar.bz2

Tree.java

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Generic_tree_(Java)?oldid=18925
14 */
15 
16 import java.util.Arrays;
17 import java.util.List;
18 import java.util.ArrayList;
19 abstract class Tree<E> {
20 	public abstract <R> R visit(Visitor<? super E, R> v);
21 
22 	public interface Visitor<E, R> {
23 		public R leaf(E elt);
24 		public R branch(List<? extends R> children);
25 	}
26 
27 	public static <T> Tree<T> branch(final List<? extends Tree<T>> children) {
28 		// return anonymous subclass for a branch:
29 		return new Tree<T>() {
30 			@Override
31 			public <R> R visit(Visitor<? super T, R> v) {
32 				List<R> result = new ArrayList<R>();
33 				for (Tree<T> tree : children) {
34 					result.add(tree.visit(v));
35 				}
36 				return v.branch(result);
37 			}
38 		};
39 	}
40 	public static <T> Tree<T> leaf(final T e) {
41 		// return anonymous subclass for a leaf:
42 		return new Tree<T>() {
43 			@Override
44 			public <R> R visit(Visitor<? super T, R> v) {
45 				return v.leaf(e);
46 			}
47 		};
48 	}
49 	static class TreeClient {
50 		public static String toString(Tree<?> t) {
51 			// return an anonymous implementation of a visitor:
52 			return t.visit(new Tree.Visitor<Object, String>() {
53 				public String leaf(Object e) {
54 					return e.toString();
55 				}
56 				public String branch(List<? extends String> children) {
57 					String result = "(";
58 					for (String c : children) {
59 						result += c + " ";
60 					}
61 					return result.trim() + ")";
62 				}
63 			});
64 		}
65 
66 		public static double sum(Tree<? extends Number> t) {
67 			// return an anonymous implementation of a visitor:
68 			return t.visit(new Tree.Visitor<Number, Double>() {
69 				public Double leaf(Number e) {
70 					return e.doubleValue();
71 				}
72 				public Double branch(List<? extends Double> children) {
73 					Double res = 0.0;
74 					for (Double d : children) {
75 						res += d;
76 					}
77 					return res;
78 				}
79 			});
80 		}
81 		public static void main(String[] args) {
82 			Tree<Integer> t = Tree.branch(Arrays.asList(Tree.branch(Arrays
83 					.asList(Tree.leaf(1), Tree.leaf(2))), Tree.leaf(3)));
84 			System.out.println(toString(t)); // --> ((1,2),3)
85 			System.out.println(sum(t)); // --> 6.0
86 		}
87 	}
88 }


hijacker
hijacker
hijacker
hijacker