How to Compare Two Binary Trees in Python

How to Compare Two Binary Trees in Python

How Is Our Tree?

our tree is something like this:

No alt text provided for this image
click to see the full code


for simplicity we skipped the definition of insert method, but we will provide the full code at the end, for now lets focus on the title

What Are We Going to Do?

we are goint to make a method named equal which checks if those two binary trees are equal or not, we want to use it like this:

first we define a tree with three nodes

No alt text provided for this image
click to see the full code


then we create another tree with the exact same nodes

No alt text provided for this image
click to see the full code


now we call our magic equal method to check the equality

No alt text provided for this image
click to see the full code


and the result will of course be True

Lets Do The Magic:

No alt text provided for this image
click to see the full code

first we define a method named equal which is going to be exposed outside this method has two inputs the famous self and the other, first one as you know refers to the object and the other is going to refer to the second tree

inside of this method we are calling a private static method named __equal.

No alt text provided for this image
click to see the full code

the __ at the begining makes this private and the fact that we are calling __equal with the calss name not the objects name makes it a static method.

this is where the actual logic happens, this static method is the one we called in the equal method earlier, this method gets two inputs named self_root and other_root which like the names implies they are the root node of the two trees

No alt text provided for this image
click to see the full code


in the first scenario both nodes are None and the result will be True in this situation.

No alt text provided for this image
click to see the full code

in the second scenario neither of them is None, for this we use a pre-order traversal and first check the value of the roots and then check the values of left and right sub-trees and create a boolean experssion to return.

No alt text provided for this image
click to see the full code

at the end we return False, we get here if one of our nodes is None and the other one is not, so the result is False under this circumstance.

an Edge Case:

what if we pass None to our equal Method?

No alt text provided for this image
click to see the full code

in this case we will face a beautifull AttributeError

No alt text provided for this image
click to see the full code

caused by this line:

No alt text provided for this image
click to see the full code

in order to fix this we have to add a condition to our equal method like this:

No alt text provided for this image
click to see the full code


The Full Code:

No alt text provided for this image
click to see the full code

hope it was useful guys, to see the full code, click here

要查看或添加评论,请登录

Mohammad Javad Ramezanpour的更多文章

  • Magic of Hashtables

    Magic of Hashtables

    Hashtables, also known as dictionaries, are fundamental data structures used in programming for efficient storage and…

  • why can't we use lists as the key of a dictionary?!

    why can't we use lists as the key of a dictionary?!

    how should a dictionaries key be? a dictionaries key should be a hashable and immutable object! what are these two? 1-…

社区洞察

其他会员也浏览了