1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 package net.sf.jtreemap.ktreemap;
34
35 /**
36 * Tree builder for a KTreeMap.
37 *
38 * @author Laurent Dutheil
39 */
40
41 public abstract class TreeMapNodeBuilder {
42 private TreeMapNode root;
43
44 /**
45 * Add a branch to the tree. <BR>
46 * If the parent is null, the build node become the root if and only if the
47 * tree have no root yet. If the parent is null and if the root is already
48 * build, the node will NOT be added to the tree.
49 *
50 * @param value Value of the branch
51 * @param parent father of the branch
52 * @return the created node
53 */
54 public TreeMapNode buildBranch(Object value, TreeMapNode parent) {
55 TreeMapNode node = new TreeMapNode(value);
56 if (parent != null) {
57 parent.add(node);
58 } else if (this.root == null) {
59 this.root = node;
60 }
61 return node;
62 }
63
64 /**
65 * add a leaf to the tree. <BR>
66 * If the parent is null, the build node become the root if and only if the
67 * tree have no root yet. If the parent is null and if the root is already
68 * build, the node will NOT be added to the tree.
69 *
70 * @param value Value of the leaf
71 * @param parent father of the leaf
72 * @return the created node
73 */
74 public TreeMapNode buildLeaf(Object value, TreeMapNode parent) {
75 TreeMapNode node = new TreeMapNode(value, getWeight(value));
76 if (parent != null) {
77 parent.add(node);
78 } else if (this.root == null) {
79 this.root = node;
80 }
81 return node;
82 }
83
84 /**
85 * get the build tree.
86 *
87 * @return the root of the tree
88 */
89 public TreeMapNode getRoot() {
90 return this.root;
91 }
92
93 /**
94 * Return the weight of an Object. <BR>
95 * This method have to be extended
96 *
97 * @param value the object (generaly, the TreeMapNode.getValue())
98 * @return the weight of the value
99 */
100 public abstract double getWeight(Object value);
101 }
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117