11package com .jwetherell .algorithms .data_structures ;
22
3+ import java .util .List ;
34import java .util .ArrayList ;
45
56/**
67 * Structure for storing rooted tree which allows to find lowest common ancestor.
7- *
8+ * <p>
89 * @param <T> type of value stored in nodes.
10+ * <br>
11+ * @author Szymon Stankiewicz <[email protected] > 12+ * @author Justin Wetherell <[email protected] > 913 */
1014public class RootedTree <T > {
15+
16+ private final List <RootedTree <T >> ancestors = new ArrayList <RootedTree <T >>();
17+ private final List <RootedTree <T >> children = new ArrayList <RootedTree <T >>();
18+
1119 private T value = null ;
1220 private int depth = 0 ;
13- private final ArrayList <RootedTree <T >> ancestors = new ArrayList <>();
14- private final ArrayList <RootedTree <T >> children = new ArrayList <>();
1521
1622 /**
1723 * Exception which can be thrown by lowestCommonAncestor function if two
1824 * nodes are in different trees.
1925 *
2026 */
21- public static class NodesNotInSameTreeException extends Exception {}
27+ public static class NodesNotInSameTreeException extends Exception {
28+ private static final long serialVersionUID = -5366787886097250564L ;
29+ }
2230
2331 /**
2432 * Finds lower common ancestor of two nodes.
@@ -31,9 +39,11 @@ public static class NodesNotInSameTreeException extends Exception {}
3139 * @throws NodesNotInSameTreeException if nodes don't have common root
3240 */
3341 public static <S > RootedTree <S > lowestCommonAncestor (RootedTree <S > node1 , RootedTree <S > node2 ) throws NodesNotInSameTreeException {
34- if (node1 == node2 ) return node1 ;
35- else if (node1 .depth < node2 .depth ) return lowestCommonAncestor (node2 , node1 );
36- else if (node1 .depth > node2 .depth ) {
42+ if (node1 == node2 )
43+ return node1 ;
44+ else if (node1 .depth < node2 .depth )
45+ return lowestCommonAncestor (node2 , node1 );
46+ else if (node1 .depth > node2 .depth ) {
3747 int diff = node1 .depth - node2 .depth ;
3848 int jump = 0 ;
3949 while (diff > 0 ) {
@@ -43,12 +53,12 @@ else if(node1.depth > node2.depth) {
4353 diff /= 2 ;
4454 }
4555 return lowestCommonAncestor (node1 , node2 );
46- }
47- else {
56+ } else {
4857 try {
4958 int step = 0 ;
50- while (1 <<(step +1 ) <= node1 .depth ) step ++;
51- while (step >= 0 ) {
59+ while (1 <<(step +1 ) <= node1 .depth )
60+ step ++;
61+ while (step >= 0 ) {
5262 if (step < node1 .ancestors .size () && node1 .ancestors .get (step ) != node2 .ancestors .get (step )) {
5363 node1 = node1 .ancestors .get (step );
5464 node2 = node2 .ancestors .get (step );
@@ -61,16 +71,13 @@ else if(node1.depth > node2.depth) {
6171 }
6272
6373 }
64-
6574 }
6675
6776 /**
6877 * Creates tree with root only.
6978 *
7079 */
71- public RootedTree () {
72-
73- }
80+ public RootedTree () { }
7481
7582 /**
7683 * Cretes tree with root (storing value) only.
@@ -109,7 +116,7 @@ public RootedTree<T> setValue(T value) {
109116 * @return added child
110117 */
111118 public RootedTree <T > addChild () {
112- return new RootedTree <>(this );
119+ return new RootedTree <T >(this );
113120 }
114121
115122 /**
@@ -140,15 +147,15 @@ public T getValue() {
140147 * @return subtree with given value in the root
141148 */
142149 public RootedTree <T > find (T value ) {
143- if (this .value == null ) {
144- if (value == null )
150+ if (this .value == null ) {
151+ if (value == null )
145152 return this ;
146153 }
147- else if (this .value .equals (value ))
154+ else if (this .value .equals (value ))
148155 return this ;
149- for (RootedTree <T > child : children ) {
156+ for (RootedTree <T > child : children ) {
150157 RootedTree <T > res = child .find (value );
151- if (res != null )
158+ if (res != null )
152159 return res ;
153160 }
154161 return null ;
@@ -163,5 +170,4 @@ else if(this.value.equals(value))
163170 public boolean contains (T value ) {
164171 return find (value ) != null ;
165172 }
166-
167173}
0 commit comments