Question

In: Computer Science

Find the largest subtree occurring in two or more places within a tree in c++ programing

Find the largest subtree occurring in two or more places within
a tree in c++ programing

Solutions

Expert Solution

To find the largest subtree in two or more places within the tree, for this let us consider an tee:

            50
         /      \
        10       60
       /  \     /   \
      5   20   70    70
               / \   / \
             65  80 65  80

from this tree, the largest subtree comparing the two tree is 60, in CPP, here the code:

#include <bits/stdc++.h> 
using namespace std; 

/* A binary tree node has data, pointer to left 
child and a pointer to right child */
struct Node 
{ 
        int data; 
        Node* left, * right; 
}; 

/* Helper function that allocates a new node with 
the given data and NULL left and right pointers. */
Node* newNode(int data) 
{ 
        Node* node = new Node; 
        node->data = data; 
        node->left = node->right = NULL; 
        return (node); 
} 

// Sets maxSize to size of largest subtree with 
// identical left and right. maxSize is set with 
// size of the maximum sized subtree. It returns 
// size of subtree rooted with current node. This 
// size is used to keep track of maximum size. 
int largestSubtreeUtil(Node* root, string& str, 
                                        int& maxSize, Node*& maxNode) 
{ 
        if (root == NULL) 
                return 0; 

        // string to store structure of left and 
        // right subtrees 
        string left = "", right = ""; 

        // traverse left subtree and finds its size 
        int ls = largestSubtreeUtil(root->left, left, 
                                                        maxSize, maxNode); 

        // traverse right subtree and finds its size 
        int rs = largestSubtreeUtil(root->right, right, 
                                                        maxSize, maxNode); 

        // if left and right subtrees are similar 
        // update maximum subtree if needed (Note that 
        // left subtree may have a bigger value than 
        // right and vice versa) 
        int size = ls + rs + 1; 
        if (left.compare(right) == 0) 
        { 
        if (size > maxSize) 
        { 
                        maxSize = size; 
                        maxNode = root; 
        } 
        } 

        // append left subtree data 
        str.append("|").append(left).append("|"); 

        // append current node data 
        str.append("|").append(to_string(root->data)).append("|"); 

        // append right subtree data 
        str.append("|").append(right).append("|"); 

        return size; 
} 

// function to find the largest subtree 
// having identical left and right subtree 
int largestSubtree(Node* node, Node*& maxNode) 
{ 
        int maxSize = 0; 
        string str = ""; 
        largestSubtreeUtil(node, str, maxSize, maxNode); 

        return maxSize; 
} 

/* Driver program to test above functions*/
int main() 
{ 
        /* Let us construct the following Tree 
                                50 
                        /        \ 
                        10       60 
                        / \      / \ 
                        5 20 70 70 
                                / \ / \ 
                                65 80 65 80 */
        Node* root = newNode(50); 
        root->left = newNode(10); 
        root->right = newNode(60); 
        root->left->left = newNode(5); 
        root->left->right = newNode(20); 
        root->right->left = newNode(70); 
        root->right->left->left = newNode(65); 
        root->right->left->right = newNode(80); 
        root->right->right = newNode(70); 
        root->right->right->left = newNode(65); 
        root->right->right->right = newNode(80); 

        Node *maxNode = NULL; 
        int maxSize = largestSubtree(root, maxNode); 

        cout << "Largest Subtree is rooted at node "
                << maxNode->data << "\nand its size is "
                << maxSize; 

        return 0; 
} 

Once the execute the program, the output will come:


Related Solutions

C programing language A file "data.txt" contains only integers. Write a code to find average of...
C programing language A file "data.txt" contains only integers. Write a code to find average of all values and print the average How would you use execlp function to execute "ps –e –a –l" command char *dt = "The five boxing wizards jump quickly"; write a program to count frequency of each letter, ignore case. Print the letter and frequency of each letter. // 1A: . Ask the user to enter a password string, store it in pass. Password should...
There are at least five (probably more) places in the attached C# program code that could...
There are at least five (probably more) places in the attached C# program code that could be optimized. Find at least five things in the Program.cs file that can be changed to make the program as efficient as possible. List five problems you found with the code (Put your list either in comments within the code or in a separate Word or text document), and then optimize the code in the attached code below to make it as efficient as...
In C++: Write a binary search tree and include the functions to find the number of...
In C++: Write a binary search tree and include the functions to find the number of nodes and the height of the tree. Test your code in main. Print the post-order, in-order and pre-order traversal.
• Write a C++ program to find the largest umber, smallest number and sum of all...
• Write a C++ program to find the largest umber, smallest number and sum of all the element of a given array of 20 integers • Note − Declare array to 20 numbers − Input values for 20 array elements − Find largest number − Find smallest number − Find sum of all numbers • Display the results
Write a C++ program to find K largest elements in a given array of integers. For...
Write a C++ program to find K largest elements in a given array of integers. For eeample, if K is 3, then your program should ouput the largest 3 numbers in teh array. Your program is not supposed to use any additional array.
Use c programing. Declare a new employee struct. Prompt the user to enter values for two-member...
Use c programing. Declare a new employee struct. Prompt the user to enter values for two-member data of the struct. Store struct data in employee struct. Use the calculate(raise_function) to increase employee salary and store the result in employee structure. Use print_ structure to print employee structure.
in C programing language Two numbers entered from the command line (example: calculate.exe 2 + 2)...
in C programing language Two numbers entered from the command line (example: calculate.exe 2 + 2) Add, subtract, divide and multiply by (+, -, /, *) operators Write the C program that makes the calculation using switch-case. hint: operator [2][0] comes from index .
The sum of two numbers is 34. a)Find the largest possible product of these numbers.
  1-The sum of two numbers is 34.    a)Find the largest possible product of these numbers.    b)What would be the largest possible product if the sum if the two numbers were "k"? 2-Sixty meters of fencing are used to fence a rectangular garden.    a)Find the dimensions that will give that maximum area.    b)What would be the maximum area if "k" feet of fencing were used in terms of "k"? THANK YOU
Find the z-score to the nearest two decimal places that: a) has 99.85% of the distribution's...
Find the z-score to the nearest two decimal places that: a) has 99.85% of the distribution's area to the right b) has .15% of the distribution's area to the left c) has 15% of the distribution's area to the right d) has 22.4% of the distribution's area to the left
a. For each of the following, answer correct to two decimal places Find the area under...
a. For each of the following, answer correct to two decimal places Find the area under the curve y=3x^3+2 Find the area between the curve y=2/x and the x-axis for x between -5 and -4 Find the area between the curve y=x^2-3x and the x-axis for x between 2 and 7 Find the area bounded by the curves y=6x^3+1 and y=6x+1 b. The birth rate in a certain city is described by the following function . The city's death rate...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT