Disallow the insertion of a parent node into a child node

I implemented a program node that allows the addition of child nodes. The functionality I want to implement is to disable adding a parent node to a child node. How to implement it ?

image

Thanks