Javatpoint Logo
Javatpoint Logo

Find Duplicate Subtrees in Binary Tree

Duplicate trees generally refer to identical subtrees in a large data structure subtree. The discovery of duplicate subtrees in a binary tree can be one of the most valuable insights in various domains, such as data compression, genetics, and several others. In this article, we will dwell on the enlightening topic of finding all the duplicate subtrees in a binary tree; we will explore different methodologies, algorithms, and techniques in which we can find them.

They may appear redundant at once, but they can offer multiple advantages, and they are: -

  1. Duplicate subtrees allow the maximum and efficient use of the storage and memory medium, which is brilliant today.
  2. By recognizing the effort and leverage, certain operations in the tree can be improved by the introduction of duplicate subtrees.
  3. They provide an excellent opportunity for data compression as they can be used in situations where trees are used to be using trees as structured data.
  4. Duplicate subtrees can quickly help us identify any common patterns that appear on the tree and can be helpful for optimization.
  5. If we need to make changes or modify a specific subtree, we can modify or update one part of the duplicate subtree, and it will be done.

Implementation

Output:

Find Duplicate Subtrees in Binary Tree

Step-by-step implementation of the code

  1. The code begins by including all the necessary header files to carry out the program's various input and output operations.
  2. A structure 'node' is defined that will represent the nodes in a binary tree. It comprises a data value and a pointer to the left and right child.
  3. Next, we move on and define an in-order function that is responsible for the in-order traversal of the binary tree. It generally takes two parameters, one of them is an unordered map, and the other one is the current node being visited.
  4. Inside the inorder function, it first checks a basic condition where it verifies if the current node is NULL, and if this is the case, it returns an empty string. Otherwise, it initializes a string with 'str'.
  5. After we finish constructing the subtree string, the code checks if the current string already exists as a key, and if it does, it means that the subtree has been there before.
  6. We create a 'printAllDups' function, a wrapper function that sets up the unordered map 'm' and is responsible for calling the print function.
  7. Next, we define a 'newNode' function to help us allocate memory to the new node and store value in the tree.
  8. In the program's primary function, the root of the binary tree is subjected to a NULL value, and the nodes are connected to the binary tree structure.

Example 2)

Output:

Find Duplicate Subtrees in Binary Tree

Step-by-step implementation of the code

  1. The code begins by importing the necessary Java packages.
  2. The code defines a class named 'Binary Tree' and holds the major code of the program.
  3. A structure 'node' is defined that will represent the nodes in a binary tree. It comprises a data value and a pointer to the left and right child.
  4. Next, we move on and define an in-order function that is responsible for the in-order traversal of the binary tree. It generally takes two parameters, one of them is an unordered map, and the other one is the current node being visited.
  5. Inside the inorder function, it first checks a basic condition where it verifies if the current node is NULL, and if this is the case, it returns an empty string. Otherwise, it initializes a string with 'str'.
  6. After we finish constructing the subtree string, the code checks if the current string already exists as a key, and if it does, it means that the subtree has been there before.
  7. We create a 'printAllDups' function, a wrapper function that sets up the unordered map 'm' and is responsible for calling the print function.
  8. Next, we define a 'newNode' function to help us allocate memory to the new node and store value in the tree.
  9. In the program's primary function, the root of the binary tree is subjected to a NULL value, and the nodes are connected to the binary tree structure.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA