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 6163af3

Browse files
Implementation of lowest common ancestor.
1 parent 833341a commit 6163af3

File tree

4 files changed

+184
-3
lines changed

4 files changed

+184
-3
lines changed

‎.travis.yml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
before_script:
2+
- sudo apt-get install ant-optional
23
- export OPTS="-server -Xmx3072M"
34
- export JAVA_OPTS="${JAVA_OPTS} ${OPTS}"
45
- export ANT_OPTS="${ANT_OPTS} ${OPTS}"
@@ -9,12 +10,12 @@ language: java
910

1011
jdk:
1112
- oraclejdk8
12-
- oraclejdk7
13+
# - oraclejdk7
1314
# - oraclejdk6
1415

1516
# - openjdk8
1617
- openjdk7
17-
- openjdk6
18+
# - openjdk6
1819

1920
env:
2021
- TEST_SUITE=run_tests
@@ -27,4 +28,3 @@ env:
2728
# - TEST_SUITE=sorts
2829

2930
script: "ant $TEST_SUITE"
30-

‎README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ This is a collection of algorithms and data structures which I've implement over
6363
* [Suffix Trie [backed by a Trie]](src/com/jwetherell/algorithms/data_structures/SuffixTrie.java)
6464
* [Ternary Search Tree](src/com/jwetherell/algorithms/data_structures/TernarySearchTree.java)
6565
* [Treap](src/com/jwetherell/algorithms/data_structures/Treap.java)
66+
* [Tree](src/com/jwetherell/algorithms/data_structures/Tree.java)
6667
* [Tree Map (associative array) [backed by an AVL Tree]](src/com/jwetherell/algorithms/data_structures/TreeMap.java)
6768
* [Trie](src/com/jwetherell/algorithms/data_structures/Trie.java)
6869
* [Trie Map (associative array) [backed by a Trie]](src/com/jwetherell/algorithms/data_structures/TrieMap.java)
@@ -156,6 +157,8 @@ This is a collection of algorithms and data structures which I've implement over
156157
* [Edmonds Karp](src/com/jwetherell/algorithms/graph/EdmondsKarp.java)
157158
* Matching
158159
- [Turbo Matching](src/com/jwetherell/algorithms/graph/TurboMatching.java)
160+
* [Lowest common ancestor in tree](src/com/jwetherell/algorithms/data_structures/Tree.java)
161+
159162

160163
## Search
161164
* Get index of value in array
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package com.jwetherell.algorithms.data_structures;
2+
3+
import java.util.ArrayList;
4+
5+
/**
6+
* Structure for storing rooted tree which allows to find lowest common ancestor.
7+
*
8+
* @param <T> type of value stored in nodes.
9+
*/
10+
public class Tree<T> {
11+
private T value = null;
12+
private int depth = 0;
13+
private final ArrayList<Tree<T>> ancestors = new ArrayList<>();
14+
15+
/**
16+
* Exception which can be thrown by lowestCommonAncestor function if two
17+
* nodes are in different trees.
18+
*
19+
*/
20+
public static class NodesNotInSameTreeException extends Exception {}
21+
22+
/**
23+
* Finds lower common ancestor of two nodes.
24+
*
25+
* Complexity O(log n) where n is the height of the tree.
26+
*
27+
* @param node1 first node
28+
* @param node2 second node
29+
* @return lower common ancestor
30+
* @throws NodesNotInSameTreeException if nodes don't have common root
31+
*/
32+
public static <S> Tree<S> lowestCommonAncestor(Tree<S> node1, Tree<S> node2) throws NodesNotInSameTreeException {
33+
if(node1 == node2) return node1;
34+
else if(node1.depth < node2.depth) return lowestCommonAncestor(node2, node1);
35+
else if(node1.depth > node2.depth) {
36+
int diff = node1.depth - node2.depth;
37+
int jump = 0;
38+
while(diff > 0) {
39+
if(diff % 2 == 1)
40+
node1 = node1.ancestors.get(jump);
41+
jump++;
42+
diff /= 2;
43+
}
44+
return lowestCommonAncestor(node1, node2);
45+
}
46+
else {
47+
try {
48+
int step = 0;
49+
while(1<<(step+1) <= node1.depth) step++;
50+
while(step >= 0) {
51+
if(step < node1.ancestors.size() && node1.ancestors.get(step) != node2.ancestors.get(step)) {
52+
node1 = node1.ancestors.get(step);
53+
node2 = node2.ancestors.get(step);
54+
}
55+
step--;
56+
}
57+
return node1.ancestors.get(0);
58+
} catch (Exception e) {
59+
throw new NodesNotInSameTreeException();
60+
}
61+
62+
}
63+
64+
}
65+
66+
/**
67+
* Creates tree with root only.
68+
*
69+
*/
70+
public Tree() {
71+
72+
}
73+
74+
/**
75+
* Cretes tree with root (storing value) only.
76+
*
77+
* @param value value to be stored in root
78+
*/
79+
public Tree(T value) {
80+
this.value = value;
81+
}
82+
83+
private Tree(Tree<T> parent) {
84+
this.ancestors.add(parent);
85+
this.depth = parent.depth + 1;
86+
int dist = 0;
87+
while(true) {
88+
try {
89+
this.ancestors.add(this.ancestors.get(dist).ancestors.get(dist));
90+
dist++;
91+
} catch (Exception e){
92+
break;
93+
}
94+
}
95+
}
96+
97+
public Tree<T> setValue(T value) {
98+
this.value = value;
99+
return this;
100+
}
101+
102+
/**
103+
* Creates new child for this node and returns it.
104+
*
105+
* Complexity O(log depth)
106+
*
107+
* @return added child
108+
*/
109+
public Tree<T> addChild() {
110+
return new Tree<>(this);
111+
}
112+
113+
/**
114+
* Creates new child (storing value) for this node and returns it.
115+
*
116+
* Complexity O(log depth)
117+
*
118+
* @param value value to be stored in new child
119+
* @return added child
120+
*/
121+
public Tree<T> addChild(T value) {
122+
return addChild().setValue(value);
123+
}
124+
125+
/**
126+
* Returns value stored in node.
127+
*
128+
* @return node's value.
129+
*/
130+
public T getValue() {
131+
return value;
132+
}
133+
134+
135+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.jwetherell.algorithms.data_structures;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.*;
6+
7+
public class TreeTest {
8+
9+
@Test
10+
public void largeTreeTest() throws Tree.NodesNotInSameTreeException {
11+
Tree<Object> root = new Tree<>();
12+
Tree<Object> left = root.addChild();
13+
Tree<Object> middle = root.addChild();
14+
Tree<Object> right = root.addChild();
15+
16+
//long path
17+
Tree<Object> v = left;
18+
for(int i = 0; i<1000; i++)
19+
v = v.addChild();
20+
Tree<Object> leftRight = left.addChild();
21+
22+
assertEquals(Tree.lowestCommonAncestor(v, leftRight), left);
23+
24+
for(int i = 0; i<2000; i++) {
25+
leftRight = leftRight.addChild();
26+
27+
assertEquals(Tree.lowestCommonAncestor(v, leftRight), left);
28+
}
29+
30+
assertEquals(Tree.lowestCommonAncestor(middle, right), root);
31+
assertEquals(Tree.lowestCommonAncestor(root, right), root);
32+
assertEquals(Tree.lowestCommonAncestor(root, root), root);
33+
34+
Tree<Object> root2 = new Tree<>();
35+
boolean thrownException = false;
36+
try {
37+
Tree.lowestCommonAncestor(v, root2);
38+
} catch (Tree.NodesNotInSameTreeException e) {
39+
thrownException = true;
40+
}
41+
assertTrue(thrownException);
42+
}
43+
}

0 commit comments

Comments
(0)

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