Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c00a62e

Browse files
Code cleanup in Lowest Common Ancestor
1 parent 39d4f2d commit c00a62e

File tree

5 files changed

+236
-225
lines changed

5 files changed

+236
-225
lines changed
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
package com.jwetherell.algorithms.data_structures;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* Structure for storing rooted tree which allows to find lowest common ancestor.
8+
* <p>
9+
* @param <T> type of value stored in nodes.
10+
* <br>
11+
* @author Szymon Stankiewicz <dakurels@gmail.com>
12+
* @author Justin Wetherell <phishman3579@gmail.com>
13+
*/
14+
public class LowestCommonAncestor<T> {
15+
16+
/**
17+
* Exception which can be thrown by lowestCommonAncestor function if two
18+
* nodes are in different trees.
19+
*
20+
*/
21+
public static class NodesNotInSameTreeException extends Exception {
22+
private static final long serialVersionUID = -5366787886097250564L;
23+
}
24+
25+
/**
26+
* Finds lower common ancestor of two nodes.
27+
*
28+
* Complexity O(log n) where n is the height of the tree.
29+
*
30+
* @param node1 first node
31+
* @param node2 second node
32+
* @return lower common ancestor
33+
* @throws NodesNotInSameTreeException if nodes don't have common root
34+
*/
35+
public static <S> RootedTree<S> lowestCommonAncestor(RootedTree<S> node1, RootedTree<S> node2) throws NodesNotInSameTreeException {
36+
if (node1 == node2)
37+
return node1;
38+
else if (node1.depth < node2.depth)
39+
return lowestCommonAncestor(node2, node1);
40+
else if (node1.depth > node2.depth) {
41+
int diff = node1.depth - node2.depth;
42+
int jump = 0;
43+
while (diff > 0) {
44+
if (diff % 2 == 1)
45+
node1 = node1.ancestors.get(jump);
46+
jump++;
47+
diff /= 2;
48+
}
49+
return lowestCommonAncestor(node1, node2);
50+
} else {
51+
try {
52+
int step = 0;
53+
while (1<<(step+1) <= node1.depth)
54+
step++;
55+
while (step >= 0) {
56+
if(step < node1.ancestors.size() && node1.ancestors.get(step) != node2.ancestors.get(step)) {
57+
node1 = node1.ancestors.get(step);
58+
node2 = node2.ancestors.get(step);
59+
}
60+
step--;
61+
}
62+
return node1.ancestors.get(0);
63+
} catch (Exception e) {
64+
throw new NodesNotInSameTreeException();
65+
}
66+
67+
}
68+
}
69+
70+
public static final class RootedTree<T> {
71+
72+
private final List<RootedTree<T>> ancestors = new ArrayList<RootedTree<T>>();
73+
private final List<RootedTree<T>> children = new ArrayList<RootedTree<T>>();
74+
75+
private T value = null;
76+
private int depth = 0;
77+
78+
/**
79+
* Creates tree with root only.
80+
*
81+
*/
82+
public RootedTree() { }
83+
84+
/**
85+
* Creates tree with root (storing value) only.
86+
*
87+
* @param value value to be stored in root
88+
*/
89+
public RootedTree(T value) {
90+
this.value = value;
91+
}
92+
93+
private RootedTree(RootedTree<T> parent) {
94+
parent.children.add(this);
95+
this.ancestors.add(parent);
96+
this.depth = parent.depth + 1;
97+
int dist = 0;
98+
while (true) {
99+
try {
100+
this.ancestors.add(this.ancestors.get(dist).ancestors.get(dist));
101+
dist++;
102+
} catch (Exception e){
103+
break;
104+
}
105+
}
106+
}
107+
108+
public RootedTree<T> setValue(T value) {
109+
this.value = value;
110+
return this;
111+
}
112+
113+
/**
114+
* Creates new child for this node and returns it.
115+
*
116+
* Complexity O(log depth)
117+
*
118+
* @return added child
119+
*/
120+
public RootedTree<T> addChild() {
121+
return new RootedTree<T>(this);
122+
}
123+
124+
/**
125+
* Creates new child (storing value) for this node and returns it.
126+
*
127+
* Complexity O(log depth)
128+
*
129+
* @param value value to be stored in new child
130+
* @return added child
131+
*/
132+
public RootedTree<T> addChild(T value) {
133+
return addChild().setValue(value);
134+
}
135+
136+
/**
137+
* Returns value stored in node.
138+
*
139+
* @return node's value.
140+
*/
141+
public T getValue() {
142+
return value;
143+
}
144+
145+
/**
146+
* Finds subtree with given value in the root.
147+
*
148+
* @param value value to be find
149+
* @return subtree with given value in the root
150+
*/
151+
public RootedTree<T> find(T value) {
152+
if (this.value == null) {
153+
if (value == null)
154+
return this;
155+
} else if (this.value.equals(value))
156+
return this;
157+
for (RootedTree<T> child: children) {
158+
final RootedTree<T> res = child.find(value);
159+
if (res != null)
160+
return res;
161+
}
162+
return null;
163+
}
164+
165+
/**
166+
* Returns true if tree contains a node with given value
167+
*
168+
* @param value to be checked
169+
* @return true if tree contains node with given value, false otherwise
170+
*/
171+
public boolean contains(T value) {
172+
return find(value) != null;
173+
}
174+
}
175+
}

‎src/com/jwetherell/algorithms/data_structures/RootedTree.java‎

Lines changed: 0 additions & 173 deletions
This file was deleted.

‎test/com/jwetherell/algorithms/data_structures/RootedTreeTest.java‎

Lines changed: 0 additions & 49 deletions
This file was deleted.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /