What does it mean for two binary trees to be isomorphic? I've been looking online and I can't seem to find a clear explanation.
As far as I understand, two trees are isomorphic if they have the same shape. So I'm guessing two identical trees which can contain different values in the nodes.
Two trees are identical when they have same data and arrangement of data is also same. To identify if two trees are identical, we need to traverse both trees simultaneously, and while traversing we need to compare data and children of the trees.
Binary search tree is a data structure that quickly allows us to maintain a sorted list of numbers. It is called a binary tree because each tree node has a maximum of two children. It is called a search tree because it can be used to search for the presence of a number in O(log(n)) time.
Two nodes connected to the same node which are same distance from the root vertex in a rooted tree are called siblings.
Isomorphic comes from the Greek "same shape" (like isobar is points with the same air pressure and polygon means "many sided") so your understanding is correct. But don't make the mistake of assuming shape in this case is a physical shape (like the tree has one root, one left node and one right node; see below for example). Mathematicians have their own language which only sometimes bears a passing relationship to English :-)
It's not just binary trees. In mathematics, two structures are isomorphic if their properties are preserved regardless of their expression (you can have a function that translates A to B and another from B to A without loss of information).
For your particular case, it's the information in the tree that's preserved. For example, if that information is the sorted elements {1,2,3}
, then the tree doesn't have to be the same physical shape at all - the following two would be isomorphic:
2 1
/ \ \
1 3 2
\
3
A sorted linked list (or sorted array, for that matter) is also isomorphic to those since, in that case, no information would be lost in the transformations between the two.
If the binary tree was used in a manner where sort order was irrelevant (i.e., a "bag" sort of container), then the information would just be the contents in any order, and all the following would be isomorphic (that second last one's just a bag, the last is a list):
2 1 2 3 +---+ +---+ +---+
/ \ \ / \ +-------+ | 3 |->| 1 |->| 2 |
1 3 2 1 2 | 1,3,2 | +---+ +---+ +---+
\ / \ +-------+
3 3 1
Of course, an unsorted tree may be considered to be a bit of a waste depending on your needs, but that's not relevant to this particular discussion.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With