Write a program that takes the tree created in PRG-1 and remove the two data items 37, and 54 and rebuild the new tree.

icon
Related questions
Question

prg-1 

#include <iostream>
using namespace std;

struct Node {
    int data;
    Node* left;
    Node* right;
};

Node* newNode(int data) {
    Node* node = new Node;
    node->data = data;
    node->left = NULL;
    node->right = NULL;
    return node;
}

void printInorder(Node* node) {
    if (node == NULL)
        return;
    printInorder(node->left);
    cout << node->data << " ";
    printInorder(node->right);
}

void printPreorder(Node* node) {
    if (node == NULL)
        return;
    cout << node->data << " ";
    printPreorder(node->left);
    printPreorder(node->right);
}

void printPostorder(Node* node) {
    if (node == NULL)
        return;
    printPostorder(node->left);
    printPostorder(node->right);
    cout << node->data << " ";
}

int main() {
    Node* root = newNode(70);
    root->left = newNode(60);
    root->left->left = newNode(58);
    root->left->right = newNode(62);
    root->left->left->left = newNode(41);
    root->right = newNode(80);
    root->right->left = newNode(73);
    root->right->right = newNode(82);
    root->right->right->right = newNode(90);

    cout << "Inorder traversal: ";
    printInorder(root);
    cout << endl;

    cout << "Preorder traversal: ";
    printPreorder(root);
    cout << endl;

    cout << "Postorder traversal: ";
    printPostorder(root);
    cout << endl;

    return 0;
}

 

Write a program that takes the tree created in PRG-1 and remove the two
data items 37, and 54 and rebuild the new tree.
Transcribed Image Text:Write a program that takes the tree created in PRG-1 and remove the two data items 37, and 54 and rebuild the new tree.
Expert Solution
steps

Step by step

Solved in 5 steps with 3 images

Blurred answer