@@ -772,9 +772,7 @@ you've given the previous video a watch if you haven't done so. There should be
772
772
a link in the description below.
773
773
774
774
All the source code you see today can be found on Github at github dot com slash
775
- william fiset slash algorithms. Alright, buckle up because even though we'll
776
- only be looking at around 150 lines of code there's a lot going on,
777
- let's jump in.
775
+ william fiset slash algorithms.
778
776
779
777
So here we are in the source code for identifying isomorphic trees written
780
778
in Java. Let's start by taking a look at an example of what this code does in
@@ -791,31 +789,29 @@ fundamentally different trees.
791
789
792
790
---
793
791
794
- treesAreIsomorphic
795
-
796
- The first interesting method is the treesAreIsomorphic method which takes as
792
+ Going back up to the treesAreIsomorphic method, you'll see that it takes as
797
793
input two undirected trees stored as adjacency lists: tree1 and tree2.
798
794
799
- Just so that we don't have to worry about it, I check if either trees are empty
800
- and throw an exception.
795
+ Just so that we don't have to worry about it, the first thing I do is check if
796
+ either trees are empty and throw an exception.
801
797
802
- After that, we begin the whole tree serialzation process, it's pretty simple,
798
+ After that, we begin the whole tree serialization process, it's pretty simple,
803
799
start by finding the center or centers of both trees.
804
800
805
801
Then encode the first tree into a string. The encode method takes as input a
806
- rooted tree instead of an undirected tree so make sure you root the tree first.
807
- After the encode method has finished processing it will return a unique string
802
+ rooted tree instead of an undirected tree, so make sure you root the tree first.
803
+ After the encode method has finished processing, it will return a unique string
808
804
for tree1.
809
805
810
806
Now the next thing we want to do is encode the second tree so that we can
811
807
compare it to the first one. However, if the second tree has 2 centers we
812
808
don't know which center node in the second tree is the correct one, so we need
813
809
to try both.
814
810
For this, iterate over both centers and root the tree comparing the
815
- encoded result with the first tree . If there's any match then we
811
+ encoded result with the first tree. If there's any match then we
816
812
know that the trees are isomorphic.
817
- Awesome so that's the algorithm from a bird's eye view, now let's dig into the
818
- details and figure out what the heck is going on inside the findTreeCenters,
813
+ Awesome, so that's the algorithm from a bird's eye view, now let's dig into the
814
+ details and figure out what is going on inside the findTreeCenters,
819
815
rootTree and encode methods.
820
816
821
817
---
0 commit comments