Skip to content

Commit ee50ec0

Browse files
committed
Test cases for binary tree added
1 parent 9ea1280 commit ee50ec0

2 files changed

Lines changed: 130 additions & 0 deletions

File tree

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package org.ogo.test.createdatastructure.createbinarytree;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
import static org.ogo.client.OGO.query;
6+
import static org.ogo.client.OGO.queryBool;
7+
import static org.ogo.client.OGO.queryLong;
8+
9+
import java.io.IOException;
10+
import java.rmi.NotBoundException;
11+
import java.rmi.RemoteException;
12+
import org.junit.jupiter.api.BeforeAll;
13+
import org.junit.jupiter.api.Test;
14+
import org.ogo.client.OGO;
15+
16+
public class NewBinaryTreeTest {
17+
18+
@BeforeAll
19+
public static void initQueryEngine()
20+
throws RemoteException, InterruptedException, IOException, NotBoundException {
21+
Thread.sleep(4000);
22+
OGO.inMemory = false;
23+
OGO.init();
24+
OGO.setWhiteList("ogo/test");
25+
}
26+
27+
@Test
28+
public void checkContains() throws RemoteException {
29+
int[] arr = new int[] {1, 2, 3, 4, 5, 6, 7};
30+
BinaryTree tree = new BinaryTree();
31+
Node root = tree.createTree(arr, 0, arr.length);
32+
String nodeClass = "`org.ogo.test.createdatastructure.createbinarytree.Node`";
33+
assertTrue(queryBool(root, "MATCH (n:" + nodeClass + " {value: 4}) RETURN TRUE"));
34+
}
35+
36+
@Test
37+
public void checkSize() throws RemoteException {
38+
int[] arr = new int[] {1, 2, 3, 4, 5, 6, 7};
39+
BinaryTree tree = new BinaryTree();
40+
Node root = tree.createTree(arr, 0, arr.length);
41+
String nodeClass = "`org.ogo.test.createdatastructure.createbinarytree.Node`";
42+
assertEquals(7L, queryLong(root, "MATCH (n:" + nodeClass + ") RETURN COUNT(n)"));
43+
}
44+
45+
@Test
46+
public void checkLeafCount() throws RemoteException {
47+
// Leaves in {1,2,3,4,5,6,7} tree are: 1, 3, 5, 7 → 4 leaves
48+
int[] arr = new int[] {1, 2, 3, 4, 5, 6, 7};
49+
BinaryTree tree = new BinaryTree();
50+
Node root = tree.createTree(arr, 0, arr.length);
51+
String nodeClass = "`org.ogo.test.createdatastructure.createbinarytree.Node`";
52+
assertEquals(
53+
4L,
54+
queryLong(
55+
root,
56+
"MATCH (n:"
57+
+ nodeClass
58+
+ ") WHERE NOT (n)-[:left]->() AND NOT (n)-[:right]->() RETURN COUNT(n)"));
59+
}
60+
61+
@Test
62+
public void checkBSTInvariant() throws RemoteException {
63+
// Every left child must be smaller than its parent, every right child must be larger
64+
int[] arr = new int[] {1, 2, 3, 4, 5, 6, 7};
65+
BinaryTree tree = new BinaryTree();
66+
Node root = tree.createTree(arr, 0, arr.length);
67+
String nodeClass = "`org.ogo.test.createdatastructure.createbinarytree.Node`";
68+
assertTrue(
69+
queryBool(
70+
root,
71+
"MATCH (a:"
72+
+ nodeClass
73+
+ ")<-[:left]-(b:"
74+
+ nodeClass
75+
+ ") MATCH (c:"
76+
+ nodeClass
77+
+ ")-[:right]->(d:"
78+
+ nodeClass
79+
+ ") WITH COLLECT(a.value<b.value AND d.value>c.value) AS m RETURN ALL(n in m WHERE n=true)"));
80+
}
81+
82+
@Test
83+
public void checkParent() throws RemoteException {
84+
// Parent of node with value 2 should be the root node with value 4
85+
int[] arr = new int[] {1, 2, 3, 4, 5, 6, 7};
86+
BinaryTree tree = new BinaryTree();
87+
Node root = tree.createTree(arr, 0, arr.length);
88+
String nodeClass = "`org.ogo.test.createdatastructure.createbinarytree.Node`";
89+
Object[] result =
90+
query(
91+
root,
92+
"MATCH (parent:"
93+
+ nodeClass
94+
+ ")-[:left|right]->(child:"
95+
+ nodeClass
96+
+ " {value: 2}) RETURN parent.value");
97+
assertEquals(1, result.length, "Expected exactly one parent");
98+
assertEquals(4, result[0], "Parent of node 2 should be node 4");
99+
}
100+
101+
@Test
102+
public void checkDepth() throws RemoteException {
103+
// Shortest path from root (value=4) to leaf (value=1) should be 2 hops: 4->2->1
104+
int[] arr = new int[] {1, 2, 3, 4, 5, 6, 7};
105+
BinaryTree tree = new BinaryTree();
106+
Node root = tree.createTree(arr, 0, arr.length);
107+
String nodeClass = "`org.ogo.test.createdatastructure.createbinarytree.Node`";
108+
assertEquals(
109+
2L,
110+
queryLong(
111+
root,
112+
"MATCH p=shortestPath((r:"
113+
+ nodeClass
114+
+ " {value: 4})-[*]->(n:"
115+
+ nodeClass
116+
+ " {value: 1})) RETURN length(p)"));
117+
}
118+
}

src/test/java/org/ogo/test/createdatastructure/createbinarytree/Node.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,16 @@ public class Node {
1010
left = null;
1111
right = null;
1212
}
13+
14+
Node(int val) {
15+
value = val;
16+
left = null;
17+
right = null;
18+
}
19+
20+
Node(Node leftNode, Node rightNode, int val) {
21+
value = val;
22+
left = leftNode;
23+
right = rightNode;
24+
}
1325
}

0 commit comments

Comments
 (0)