Question

In: Computer Science

Write a function named mirror_tree that accepts a pointer to the root of a binary tree...

Write a function named mirror_tree that accepts a pointer to the root of a binary tree of integers. Your function should rearrange the nodes into a mirror tree of the original tree. The mirror tree has the left and right subtrees reversed for each node.

Constraints: You must implement your function recursively and without using loops. Do not construct any new BST objects in solving this problem (though you may create as many NODE* pointer variables as you like). Do not use any auxiliary data structures to solve this problem (no array, vector, stack, queue, string, etc).

Assume that you are using the NODE structure as defined below:

struct NODE {
int Key;
NODE* Left;
NODE* Right;
};

If you run mirror_tree on a BST, is your new tree still a BST?

Code to edit:

/ util.h

#include <iostream>
#include "bst.h"
using namespace std;

// mirror_tree:
//
// Your function should rearrange the nodes into a mirror tree
// of the original tree. The mirror tree has the left and right
// subtrees reversed for each node.
//
void mirror_tree(NODE* node) {
// TO DO: Write this function.
}

Solutions

Expert Solution

// C++ program to convert a binary tree 
// to its mirror 
#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; 
        struct Node* left; 
        struct Node* right; 
}; 

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


/* Change a tree so that the roles of the left and 
        right pointers are swapped at every node. 

So the tree... 
        4 
        / \ 
        2 5 
        / \ 
1 3 

is changed to... 
        4 
        / \ 
        5 2 
                / \ 
        3 1 
*/
void mirror(struct Node* node) 
{ 
        if (node == NULL) 
                return; 
        else
        { 
                struct Node* temp; 
                
                /* do the subtrees */
                mirror(node->left); 
                mirror(node->right); 
        
                /* swap the pointers in this node */
                temp     = node->left; 
                node->left = node->right; 
                node->right = temp; 
        } 
} 

/* Helper function to print 
Inorder traversal.*/
void inOrder(struct Node* node) 
{ 
        if (node == NULL) 
                return; 
        
        inOrder(node->left); 
        cout << node->data << " "; 
        inOrder(node->right); 
} 


// Driver Code 
int main() 
{ 
        struct Node *root = newNode(1); 
        root->left = newNode(2); 
        root->right = newNode(3); 
        root->left->left = newNode(4); 
        root->left->right = newNode(5); 
        
        /* Print inorder traversal of the input tree */
        cout << "Inorder traversal of the constructed"
                << " tree is" << endl; 
        inOrder(root); 
        
        /* Convert tree to its mirror */
        mirror(root); 
        
        /* Print inorder traversal of the mirror tree */
        cout << "\nInorder traversal of the mirror tree"
                << " is \n"; 
        inOrder(root); 
        
        return 0; 
} 


Related Solutions

CODE IN JAVA** I(a). Given a pointer to the root of a binary tree write a...
CODE IN JAVA** I(a). Given a pointer to the root of a binary tree write a routine that will mark (use a negative number like -999 for the info field) every node in the tree that currently has only a left son. You can assume the root of the tree has both a right and left son. When finished tell me how many nodes had only a left son as well as how many nodes are in the tree in...
Implement a function named printRange that, given the pointer to the root of a BST, a...
Implement a function named printRange that, given the pointer to the root of a BST, a low key value, and a high key value, prints in sorted order all records whose key values fall between the two given keys (inclusive). Function printRange should visit as few nodes in the BST as possible. Here is the start code (in Java 8) for this problem. Input Format Three lines. The first line includes the number of keys to be put in the...
write a function named as cubeCalculator that takes an integer pointer as function and return its...
write a function named as cubeCalculator that takes an integer pointer as function and return its cube value , you are required to compute the cube of a number using pointer notation , return the result as an integer value , use c++
write a function that takes as input the root of a general tree and returns a...
write a function that takes as input the root of a general tree and returns a binary tree generated by the conversion process illustrated in java
3. Write a function named "countNonAlpha" that accepts a string. It will return the number of...
3. Write a function named "countNonAlpha" that accepts a string. It will return the number of non-alphabet characters (excluding blanks) in the string. For example, if the string is "Hello, World!", it will return 2 for ',' and '!" in the string. 4. Write a function named "deleteZeros" that takes two arguments: a. an array of integer values; b. an integer for the number of elements in the array; The function will return the number of zeros that it has...
Write a function, named isMultipleOfFive that accepts integer argument. When the function is called, it should...
Write a function, named isMultipleOfFive that accepts integer argument. When the function is called, it should display if the argument "is a multiple of 5" or "is not a multiple of 5".
(Binary Tree): Write a recursive implementation of the function, singleParent, that returns the number of the...
(Binary Tree): Write a recursive implementation of the function, singleParent, that returns the number of the nodes in a binary tree that have only one child. Convert it to an iterative version. in C++
Write a boolean function that is given a binary tree and returns true if and only...
Write a boolean function that is given a binary tree and returns true if and only if the tree has an odd number of nodes. An empty tree is considered to have an even number of nodes. Notes: The function should have just one argument, a pointer to the root. No global variables may be used. No additional functions may be defined. You may not count the number of nodes.
Python: Write a function named calc_odd_sum that accepts a positive integer as the argument and returns...
Python: Write a function named calc_odd_sum that accepts a positive integer as the argument and returns the sum of all the odd numbers that are less than or equal to the input number. The function should return 0 if the number is not positive. For example, if 15 is passed as argument to the function, the function should return the result of 1+3+5+7+9+11+13+15. If -10 is passes, it shall return 0. You shall run the program test the result at...
(IN C) Write the code to manage a Binary Tree. Each node in the binary tree...
(IN C) Write the code to manage a Binary Tree. Each node in the binary tree includes an integer value and string. The binary tree is sorted by the integer value. The functions include: • Insert into the binary tree. This function will take in as parameters: the root of the tree, the integer value, and the string. Note that this function requires you to create the node. • Find a node by integer value: This function takes in two...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT