How can I check that my binary tree doens’t contain duplicates?
Do you have an algorithm?
Please write the pseudocode
EDIT: or (better) using math property.
This is my Tree over an alphabet A: (a,U,V) where a \in A and U and V is respectively the left child and the right child.
If the tree is ordered like a binary search tree using a relation of ordering < (riflessive, antisymmetric, transitive, total) I can express that T=(a,U,V) is ordered without duplicates IFONLY \forall u \in flatten(U) and \forall v \in flatten(V). u < a< v and u \neq a and a \neq v and i’ve to check the property recursively for the tree U and tree tree V. But the question is: How can i check that the tree doesn’t contains duplicates if the tree is not ordered (or is not a binary search tree)?
Apologise for my first not clear question
EDIT2: flatten is the function that return the set of the elements of the parameter tree
There is an O(n log n) obvious way, you collect the elements, sort the list and find duplicates. The problem is at least as hard as finding duplicates in an array see this. Therefore, if you rely only on comparisons, you cannot achieve better than O(n log n).