Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -0,0 +1,118 @@
package org.ogo.test.createdatastructure.createbinarytree;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we do not need a separate file for this; just keep in the same file

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do


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;
import java.rmi.RemoteException;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.ogo.client.OGO;

public class NewBinaryTreeTest {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we please do a different Data Structure

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other BinaryTreeTest file is disabled so I add some new tests to see if OGO works for Binary tree data structure.


@BeforeAll
public static void initQueryEngine()
throws RemoteException, InterruptedException, IOException, NotBoundException {
Thread.sleep(4000);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need to sleep?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sleep command gives time for Neo4JGraphQueryEngine to start before test runs. It also exists in all other test files.

OGO.inMemory = false;
OGO.init();
OGO.setWhiteList("ogo/test");
}

@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`";
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why don't we do Node.class?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will change hardcode class name to use reflection.

assertTrue(queryBool(root, "MATCH (n:" + nodeClass + " {value: 4}) RETURN TRUE"));
}

@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, "MATCH (n:" + nodeClass + ") RETURN COUNT(n)"));
}

@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,
"MATCH (n:"
+ nodeClass
+ ") WHERE NOT (n)-[:left]->() AND NOT (n)-[:right]->() RETURN COUNT(n)"));
}

@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,
"MATCH (a:"
+ nodeClass
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need + in other tests/files or do we use String.format?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will change to String.format

+ ")<-[:left]-(b:"
+ nodeClass
+ ") MATCH (c:"
+ nodeClass
+ ")-[:right]->(d:"
+ nodeClass
+ ") WITH COLLECT(a.value<b.value AND d.value>c.value) AS m RETURN ALL(n in m WHERE n=true)"));
}

@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,
"MATCH (parent:"
+ nodeClass
+ ")-[:left|right]->(child:"
+ nodeClass
+ " {value: 2}) RETURN parent.value");
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,
"MATCH p=shortestPath((r:"
+ nodeClass
+ " {value: 4})-[*]->(n:"
+ nodeClass
+ " {value: 1})) RETURN length(p)"));
}
}
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;
}
}