77 *
88 * @param <T> type of value stored in nodes.
99 */
10- public class Tree <T > {
10+ public class RootedTree <T > {
1111 private T value = null ;
1212 private int depth = 0 ;
13- private final ArrayList <Tree <T >> ancestors = new ArrayList <>();
13+ private final ArrayList <RootedTree <T >> ancestors = new ArrayList <>();
14+ private final ArrayList <RootedTree <T >> children = new ArrayList <>();
1415
1516 /**
1617 * Exception which can be thrown by lowestCommonAncestor function if two
@@ -29,7 +30,7 @@ public static class NodesNotInSameTreeException extends Exception {}
2930 * @return lower common ancestor
3031 * @throws NodesNotInSameTreeException if nodes don't have common root
3132 */
32- public static <S > Tree <S > lowestCommonAncestor (Tree <S > node1 , Tree <S > node2 ) throws NodesNotInSameTreeException {
33+ public static <S > RootedTree <S > lowestCommonAncestor (RootedTree <S > node1 , RootedTree <S > node2 ) throws NodesNotInSameTreeException {
3334 if (node1 == node2 ) return node1 ;
3435 else if (node1 .depth < node2 .depth ) return lowestCommonAncestor (node2 , node1 );
3536 else if (node1 .depth > node2 .depth ) {
@@ -67,7 +68,7 @@ else if(node1.depth > node2.depth) {
6768 * Creates tree with root only.
6869 *
6970 */
70- public Tree () {
71+ public RootedTree () {
7172
7273 }
7374
@@ -76,11 +77,12 @@ public Tree() {
7677 *
7778 * @param value value to be stored in root
7879 */
79- public Tree (T value ) {
80+ public RootedTree (T value ) {
8081 this .value = value ;
8182 }
8283
83- private Tree (Tree <T > parent ) {
84+ private RootedTree (RootedTree <T > parent ) {
85+ parent .children .add (this );
8486 this .ancestors .add (parent );
8587 this .depth = parent .depth + 1 ;
8688 int dist = 0 ;
@@ -94,7 +96,7 @@ private Tree(Tree<T> parent) {
9496 }
9597 }
9698
97- public Tree <T > setValue (T value ) {
99+ public RootedTree <T > setValue (T value ) {
98100 this .value = value ;
99101 return this ;
100102 }
@@ -106,8 +108,8 @@ public Tree<T> setValue(T value) {
106108 *
107109 * @return added child
108110 */
109- public Tree <T > addChild () {
110- return new Tree <>(this );
111+ public RootedTree <T > addChild () {
112+ return new RootedTree <>(this );
111113 }
112114
113115 /**
@@ -118,7 +120,7 @@ public Tree<T> addChild() {
118120 * @param value value to be stored in new child
119121 * @return added child
120122 */
121- public Tree <T > addChild (T value ) {
123+ public RootedTree <T > addChild (T value ) {
122124 return addChild ().setValue (value );
123125 }
124126
@@ -131,5 +133,35 @@ public T getValue() {
131133 return value ;
132134 }
133135
136+ /**
137+ * Finds subtree with given value in the root.
138+ *
139+ * @param value value to be find
140+ * @return subtree with given value in the root
141+ */
142+ public RootedTree <T > find (T value ) {
143+ if (this .value == null ) {
144+ if (value == null )
145+ return this ;
146+ }
147+ else if (this .value .equals (value ))
148+ return this ;
149+ for (RootedTree <T > child : children ) {
150+ RootedTree <T > res = child .find (value );
151+ if (res != null )
152+ return res ;
153+ }
154+ return null ;
155+ }
156+ 157+ /**
158+ * Returns true if tree contains a node with given value
159+ *
160+ * @param value to be checked
161+ * @return true if tree contains node with given value, false otherwise
162+ */
163+ public boolean contains (T value ) {
164+ return find (value ) != null ;
165+ }
134166
135167}
0 commit comments