Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.ogo.test.createdatastructure.createbinarytree;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.ogo.client.OGO.query;
import static org.ogo.client.OGO.queryBool;
import static org.ogo.client.OGO.queryLong;

import java.io.IOException;
import java.rmi.NotBoundException;
Expand All @@ -13,17 +15,20 @@
import org.ogo.client.OGO;
import org.ogo.util.Profile;

@Disabled
// @Disabled
public class BinaryTreeTest {

/**
* @since 1.0.0
* @version 1.0.0
*/
String nodeClass = "`" + Node.class.getName() + "`";

@BeforeAll
public static void initQueryEngine()
throws RemoteException, InterruptedException, IOException, NotBoundException {
Thread.sleep(4000);
OGO.inMemory = false;
OGO.init();
OGO.setWhiteList("ogo/test");
}
Expand All @@ -32,35 +37,22 @@ public static void initQueryEngine()
* @since 1.0.0
* @version 1.0.0
*/
@Disabled
@Profile
@Test
public void addTwoNodes() throws RemoteException {
String nodeClass = "`org.ogo.test.createDataStructure.createBinaryTree.Node`";
OGO.inMemory = true;
Object[] objects =
query(
"CREATE (a:"
+ nodeClass
+ " {value: 10}), (b:"
+ nodeClass
+ " {value: 20}), (c:"
+ nodeClass
+ " {value: 30}), (d:"
+ nodeClass
+ " {value: 40}), (e:"
+ nodeClass
+ " {value: 50}), (f:"
+ nodeClass
+ " {value: 60}) MERGE (f)<-[:right]-(e)<-[:right]-(c)-[:left]->(a)-[:right]->(b)"
+ " MERGE (e)-[:left]->(d) RETURN c");
String.format(
"CREATE (a:%s {value: 10}), (b:%s {value: 20}), (c:%s {value: 30}), (d:%s {value: 40}), (e:%s {value: 50}), (f:%s {value: 60}) MERGE (f)<-[:right]-(e)<-[:right]-(c)-[:left]->(a)-[:right]->(b) MERGE (e)-[:left]->(d) RETURN c",
nodeClass, nodeClass, nodeClass, nodeClass, nodeClass, nodeClass));
OGO.inMemory = false;
Object[] result =
query(
"MATCH (n:"
+ nodeClass
+ " {value: 30})-[*2]->(m:"
+ nodeClass
+ " {value:40}) RETURN m.value");
String.format(
"MATCH (n:%s {value: 30})-[*2]->(m:%s {value:40}) RETURN m.value",
nodeClass, nodeClass));

assertTrue(result.length == 1 && result[0] instanceof Integer && (Integer) result[0] == 40);
}
Expand All @@ -69,22 +61,101 @@ public void addTwoNodes() throws RemoteException {
* @since 1.0.0
* @version 1.0.0
*/
@Disabled
@Profile
@Test
public void testInvariant() throws RemoteException {
int[] arr = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
BinaryTree tree = new BinaryTree();
Node root = tree.createTree(arr, 0, arr.length);
String nodeClass = "`org.ogo.test.createDataStructure.createBinaryTree.Node`";
OGO.inMemory = false;
assertTrue(
queryBool(
root,
"MATCH (a)<-[:left]-(b:"
+ nodeClass
+ ") MATCH (c:"
+ nodeClass
+ ")-[:right]->(d) WITH COLLECT(a.value<b.value AND d.value>c.value) AS m RETURN"
+ " ALL(n in m WHERE n=true)"));
String.format(
"MATCH (a)<-[:left]-(b:%s) MATCH (c:%s)-[:right]->(d) WITH COLLECT(a.value<b.value AND d.value>c.value) AS m RETURN ALL(n in m WHERE n=true)",
nodeClass, nodeClass)));
}

@Test
public void checkContains() throws RemoteException {
int[] arr = new int[] {1, 2, 3, 4, 5, 6, 7};
BinaryTree tree = new BinaryTree();
Node root = tree.createTree(arr, 0, arr.length);
// String nodeClass = "`org.ogo.test.createdatastructure.createbinarytree.Node`";
assertTrue(queryBool(root, String.format("MATCH (n:%s {value: 4}) RETURN TRUE", nodeClass)));
}

@Test
public void checkSize() throws RemoteException {
int[] arr = new int[] {1, 2, 3, 4, 5, 6, 7};
BinaryTree tree = new BinaryTree();
Node root = tree.createTree(arr, 0, arr.length);
// String nodeClass = "`org.ogo.test.createdatastructure.createbinarytree.Node`";
assertEquals(7L, queryLong(root, String.format("MATCH (n:%s) RETURN COUNT(n)", nodeClass)));
}

@Test
public void checkLeafCount() throws RemoteException {
// Leaves in {1,2,3,4,5,6,7} tree are: 1, 3, 5, 7 → 4 leaves
int[] arr = new int[] {1, 2, 3, 4, 5, 6, 7};
BinaryTree tree = new BinaryTree();
Node root = tree.createTree(arr, 0, arr.length);
// String nodeClass = "`org.ogo.test.createdatastructure.createbinarytree.Node`";
assertEquals(
4L,
queryLong(
root,
String.format(
"MATCH (n:%s) WHERE NOT (n)-[:left]->() AND NOT (n)-[:right]->() RETURN COUNT(n)",
nodeClass)));
}

@Test
public void checkBSTInvariant() throws RemoteException {
// Every left child must be smaller than its parent, every right child must be larger
int[] arr = new int[] {1, 2, 3, 4, 5, 6, 7};
BinaryTree tree = new BinaryTree();
Node root = tree.createTree(arr, 0, arr.length);
// String nodeClass = "`org.ogo.test.createdatastructure.createbinarytree.Node`";
assertTrue(
queryBool(
root,
String.format(
"MATCH (a:%s)<-[:left]-(b:%s) MATCH (c:%s)-[:right]->(d:%s) WITH COLLECT(a.value<b.value AND d.value>c.value) AS m RETURN ALL(n in m WHERE n=true)",
nodeClass, nodeClass, nodeClass, nodeClass)));
}

@Test
public void checkParent() throws RemoteException {
// Parent of node with value 2 should be the root node with value 4
int[] arr = new int[] {1, 2, 3, 4, 5, 6, 7};
BinaryTree tree = new BinaryTree();
Node root = tree.createTree(arr, 0, arr.length);
// String nodeClass = "`org.ogo.test.createdatastructure.createbinarytree.Node`";
Object[] result =
query(
root,
String.format(
"MATCH (parent:%s)-[:left|right]->(child:%s {value: 2}) RETURN parent.value",
nodeClass, nodeClass));
assertEquals(1, result.length, "Expected exactly one parent");
assertEquals(4, result[0], "Parent of node 2 should be node 4");
}

@Test
public void checkDepth() throws RemoteException {
// Shortest path from root (value=4) to leaf (value=1) should be 2 hops: 4->2->1
int[] arr = new int[] {1, 2, 3, 4, 5, 6, 7};
BinaryTree tree = new BinaryTree();
Node root = tree.createTree(arr, 0, arr.length);
// String nodeClass = "`org.ogo.test.createdatastructure.createbinarytree.Node`";
assertEquals(
2L,
queryLong(
root,
String.format(
"MATCH p=shortestPath((r:%s {value: 4})-[*]->(n:%s {value: 1})) RETURN length(p)",
nodeClass, nodeClass)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,16 @@ public class Node {
left = null;
right = null;
}

Node(int val) {
value = val;
left = null;
right = null;
}

Node(Node leftNode, Node rightNode, int val) {
value = val;
left = leftNode;
right = rightNode;
}
}