-
Notifications
You must be signed in to change notification settings - Fork 0
/
LowestCommonAncestorTest.java
107 lines (97 loc) · 3.33 KB
/
LowestCommonAncestorTest.java
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import static org.junit.Assert.*;
import org.junit.Test;
public class LowestCommonAncestorTest {
@Test
public void test() {
LowestCommonAncestor test = new LowestCommonAncestor();
assertEquals("First Test", "HelloWorld", test.helloWorld());;
}
@Test
public void testCreateHead(){
LowestCommonAncestor lca = new LowestCommonAncestor();
lca.createHead('a',5);
assertEquals("Create a head", 'a', lca.returnKey(lca.head));
}
@Test
public void testAddNode(){
LowestCommonAncestor lca = new LowestCommonAncestor();
lca.createHead('d',0);
lca.addNode('a',1);
assertEquals("Add node A", 'a', lca.returnKey(lca.head.left));
lca.addNode('h',2);
assertEquals("Add node h", 'h', lca.returnKey(lca.head.right));
lca.addNode('b',3);
assertEquals("Add node b", 'b', lca.returnKey(lca.head.left.right));
lca.addNode('f',4);
assertEquals("Add node f", 'f', lca.returnKey(lca.head.right.left));
lca.addNode('k',5);
assertEquals("Add node k", 'k', lca.returnKey(lca.head.right.right));
}
@Test
public void testFindNode(){
LowestCommonAncestor lca = new LowestCommonAncestor();
lca.createHead('d',0);
assertEquals("Find head Node", 'd', lca.returnKey(lca.findNode('d')));
lca.addNode('a',1);
assertEquals("Find left Node", 'a', lca.returnKey(lca.findNode('a')));
lca.addNode('h',2);
assertEquals("Find right Node", 'h', lca.returnKey(lca.findNode('h')));
lca.addNode('b',3);
assertEquals("Find b Node", 'b', lca.returnKey(lca.findNode('b')));
lca.addNode('f',4);
assertEquals("Find f Node", 'f', lca.returnKey(lca.findNode('f')));
lca.addNode('l',5);
lca.addNode('c',6);
lca.addNode('v',7);
assertEquals("Find v Node", 'v', lca.returnKey(lca.findNode('v')));
}
@Test
public void testDepth(){
LowestCommonAncestor lca = new LowestCommonAncestor();
lca.createHead('d',0);
assertEquals("Depth of head", 0, lca.depth('d'));
lca.addNode('a',1);
assertEquals("Depth of a", 1, lca.depth('a'));
lca.addNode('b',2);
assertEquals("Depth of b", 2, lca.depth('b'));
lca.addNode('c',3);
assertEquals("Depth of c", 3, lca.depth('c'));
}
@Test
public void testLCA(){
LowestCommonAncestor lca = new LowestCommonAncestor();
lca.createHead('f',0);
assertEquals("Should return head", lca.findNode('f'), lca.lowestCommonAncestor(lca.findNode('f'), lca.findNode('f'), null));
lca.addNode('c',1);
lca.addNode('k',2);
assertEquals("Should return head", lca.findNode('f'), lca.lowestCommonAncestor(lca.findNode('f'), lca.findNode('c'), lca.findNode('k')));
lca.addNode('a',3);
lca.addNode('d',4);
assertEquals("Should return c", lca.findNode('c'), lca.lowestCommonAncestor(lca.findNode('f'), lca.findNode('a'), lca.findNode('d')));
lca.addNode('v',5);
assertEquals("Should return head", lca.findNode('f'), lca.lowestCommonAncestor(lca.findNode('f'), lca.findNode('a'), lca.findNode('v')));
}
@Test
public void testPrint(){
LowestCommonAncestor lca = new LowestCommonAncestor();
lca.createHead('e',0);
lca.addNode('b', 1);
lca.addNode('f', 1);
lca.addNode('a', 1);
lca.addNode('g', 1);
String tree = lca.prettyPrintKeys();
String result =
"-e\n" +
" |-b\n" +
" | |-a\n" +
" | | |-null\n" +
" | | -null\n" +
" | -null\n" +
" -f\n" +
" |-null\n" +
" -g\n" +
" |-null\n" +
" -null\n";
assertEquals("Print Tree", result, tree);
}
}