Question

Write a RECURSIVE function, swapSubtrees, that swaps all of the left and right subtrees of a...

  1. Write a RECURSIVE function, swapSubtrees, that swaps all of the left and right subtrees of a binary tree.

void swapSubtrees(TreeNode *p)

need In C++ language please

0 0
Add a comment Improve this question Transcribed image text
Answer #1

FUNCTION TO SWAP LEFT AND RIGHT SUBTREES OF A BINARY TREE:

void swapSubtrees(TreeNode *p){
    if(p == NULL)
        return;
    swapSubtrees(p->left);
    swapSubtrees(p->right);
    // Swap subtrees
    TreeNode* temp = p->left;
    p->left = p->right;
    p->right = temp;
}

C++ CODE TO DEMONSTRATE ABOVE FUNCTION:

#include<iostream>
using namespace std;
/*This structure represents a TreeNode or sub-tree in the BST*/
struct TreeNode{
    int data;
    TreeNode *left, *right;
};
/*Function prototypes*/
void preorderTraversal(TreeNode *);
void inorderTraversal(TreeNode *);
void postorderTraversal(TreeNode *);
void swapSubtrees(TreeNode *);
/*Main function of the program*/
int main(){
    // Create a binary tree
    TreeNode *tree = NULL;
    tree = new TreeNode;
    tree->data = 1;
    tree->left = new TreeNode;
    tree->left->data = 2;
    tree->right = new TreeNode;
    tree->right->left = tree->right->right = NULL;
    tree->right->data = 3;
    tree->left->left = new TreeNode;
    tree->left->left->data = 4;
    tree->left->left->left = tree->left->left->right = NULL;
    tree->left->right = new TreeNode;
    tree->left->right->left = tree->left->right->right = NULL;
    tree->left->right->data = 5;
    cout << "Before Swapping subtrees:\n";
    cout << "Preorder:\t";
    preorderTraversal(tree);
    cout << "\nInorder:\t";
    inorderTraversal(tree);
    cout << "\nPostorder:\t";
    postorderTraversal(tree);
    
    // Swap subtrees
    swapSubtrees(tree);
    
    cout << "\n\nAfter Swapping subtrees:\n";
    cout << "Preorder:\t";
    preorderTraversal(tree);
    cout << "\nInorder:\t";
    inorderTraversal(tree);
    cout << "\nPostorder:\t";
    postorderTraversal(tree);
    return 0;
}
// Function to swap left and right subtree of binary tree
void swapSubtrees(TreeNode *p){
    if(p == NULL)
        return;
    swapSubtrees(p->left);
    swapSubtrees(p->right);
    // Swap subtrees
    TreeNode* temp = p->left;
    p->left = p->right;
    p->right = temp;
}
void preorderTraversal(TreeNode *tree){
    if(tree != NULL){
        printf("%d\t", tree->data);
        preorderTraversal(tree->left);
        preorderTraversal(tree->right);
    }
}
void inorderTraversal(TreeNode *tree){
    if(tree != NULL){
        inorderTraversal(tree->left);
        printf("%d\t", tree->data);
        inorderTraversal(tree->right);
    }
}
void postorderTraversal(TreeNode *tree){
    if(tree != NULL){
        postorderTraversal(tree->left);
        postorderTraversal(tree->right);
        printf("%d\t", tree->data);
    }
}

OUTPUT:

BINARY TREE USED IN ABOVE OUTPUT BEFORE SWAPPING SUBTREES:

BINARY TREE USED IN ABOVE OUTPUT AFTER SWAPPING SUBTREES:

FOR ANY HELP JUST DROP A COMMENT

Add a comment
Know the answer?
Add Answer to:
Write a RECURSIVE function, swapSubtrees, that swaps all of the left and right subtrees of a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary...

    Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary tree. Add this function to the class binaryTreeType and create a program to test this function. #include <iostream> using namespace std; //Definition of the Node template <class elemType> struct nodeType { elemType info; nodeType<elemType> *lLink; nodeType<elemType> *rLink; }; //Definition of the class template <class elemType> class binaryTreeType { public: //Overload the assignment operator. const binaryTreeType<elemType>& operator=(const binaryTreeType<elemType>&) { if (this != &otherTree) //avoid self-copy...

  • 2. A regular binary tree is a binary tree whose internal nodes all have two subtrees...

    2. A regular binary tree is a binary tree whose internal nodes all have two subtrees (left and right). In other words, all their nodes have either zero subtrees (in which case they are leaves) or two subtrees (in which case they are internal nodes). Suppose that you have a boolean function that tells you, for each node of the tree, whether it is a leaf or not (call it: leaf(n), for node n). a) Write a recursive function that...

  • 1. Write a recursive function that returns the sum of all even integers in a LinkedBinaryTree. Yo...

    please explain each line of code! ( in python ) 1. Write a recursive function that returns the sum of all even integers in a LinkedBinaryTree. Your function should take one parameter, root node. You may assume that the tree only contains integers. You may not call any methods from the LinkedBinaryTree class. Specifically, you should traverse the tree in your function def binary tree even sum (root): Returns the sum of al1 even integers in the binary tree 2....

  • 1. Given the two binary trees below: 14 16 Write a method called swapSubtrees, which swaps all of...

    in java ..write all complete program from a- e 1. Given the two binary trees below: 14 16 Write a method called swapSubtrees, which swaps all of the left and right subtrees in the above binary trees. Add this method to the class BinaryTree and create a program to test this method for these 2 trees. Show the original trees and the resulting trees. Note: To test your algorithm, first create a binary search tree. Write a method called singleParent,...

  • (15pt) Assume a Node class with three data members: data, left, and right. Write a recursive...

    (15pt) Assume a Node class with three data members: data, left, and right. Write a recursive method that prints the values of a BST in decreasing order to the stdout. Your method receives a pointer to the root of the tree: void BSTree: print (Node *p)

  • Coding Language: C++ Function Header: vector<vector<int>> printFromButtom(TreeNode* root) {} Definition for a binary tree node: struct...

    Coding Language: C++ Function Header: vector<vector<int>> printFromButtom(TreeNode* root) {} Definition for a binary tree node: struct TreeNode { int val; TreeNode *left; TreeNode *right; }; The Problem Complete the printFromButtom function that accepts a BST TreeNode and returns the nodes' value from left to right, level by level from leaf to root. This function will return vector<vector int which similar to a 2-D array. Function std: reverse (myvector.begin myVector en might be helpful. Definition for a binary tree node: struct...

  • Consider the class specifications for the Binary Tree class and Binary Search Tree class in the...

    Consider the class specifications for the Binary Tree class and Binary Search Tree class in the attached files // BinaryTree.h #include <iostream> using namespace std; //Definition of the Node template <class elemType> struct TreeNode { elemType data; TreeNode<elemType> *left; TreeNode<elemType> *right; }; //Definition of class Binary Tree template <class elemType> class BinaryTree { protected: TreeNode<elemType> *root; public: BinaryTree(); BinaryTreel const BinaryTree<elemType>& otherTree); BinaryTree(); bool is Empty() const; virtual boot search(const elemType& searchItem) const = 0; virtual void insert(const elemType& insertItem)...

  • The code is in JAVA public class CheckBST {   //method to implement public static boolean isValidBST(TreeNode...

    The code is in JAVA public class CheckBST {   //method to implement public static boolean isValidBST(TreeNode root) { } public static void main(String[] args) { TreeNode a = new TreeNode(1); TreeNode b = new TreeNode(2); TreeNode c = new TreeNode(3); a.left = b; a.right = c; System.out.println(isValidBST(a)); TreeNode d = new TreeNode(2); TreeNode e = new TreeNode(1); TreeNode f = new TreeNode(3); d.left = e; d.right = f; System.out.println(isValidBST(d)); } } TreeNode.java class TreeNode { int val; TreeNode left; TreeNode...

  • A balanced binary tree is a binary tree structure in which the left and right subtrees of every node differ

    Data structures C++1- A balanced binary tree is a binary tree structure in which the left and right subtrees of every node differ in height by no more than 1 Out of the following choices, which is the minimum set of nodes, if removed, will make the BST balanced?2- Which of the following is true for Red-Black Trees ? Select all choices that apply! Select one or more: a. For each node in the tree, all paths from that node to any leaf nodes contain...

  • 1) (10 pts) Write a recursive function that counts and returns the number of nodes in...

    1) (10 pts) Write a recursive function that counts and returns the number of nodes in a binary tree with the root root, that store an even value. Please use the struct shown and function prototype shown below. (For example, if the tree rooted at root stored 2, 3, 4, 8, 13, 18 and 20, the function should return 5, since there are five even values [2,4,8,18,20] stored in the tree. typedef struct node { int data; struct node* left;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT