I have a graph as shown below:
/ \. /. \
ss3 ss4 ss5
I want to start from the root node and then check the count of incoming and outgoing relation, if incoming = 0 and outgoing != 0 , then delete the node and relation (in the above example delete ss1 and ss2 and relation) if incoming = 0 and outgoing = 0 (means its a leaf node) - delete only the node. Once S1 and its relations are deleted then take ss1 (in the example above) and perform the same check and keep deleting. The above graph is just an example: the graph will have different nodes each time.
Also, if there are shared nodes (example: parents are 2 (one parent from this graph and one from another graph), then the given node and its children should not be deleted. only the relation to this node from the given graph should be deleted.
I am confused with your "deletion" logic. Are you evaluating each node independently, only considering the incoming and outgoing relations counts for that node? Can you explain how you came to the concluding that ss1 and ss2 need to be deleted, and what relation are you deleting if a node has multiple relationships outgoing?
My first impression is that cypher is not the tool for this. Depending on your explanation, maybe apoc for finding the nodes in the tree and cypher for deleting based on the 'delete' rules will work.
This is a good candidate for a custom procedure, which will allow you to traverse the tree from the root node and apply all the updates you require.