Question

Remove the second largest item from a BST. Write the complete code for this assuming you...

Remove the second largest item from a BST. Write the complete code for this assuming you have integer data in a node. C++ code only.

int remove_second_L(node *& root)

{

if(!root)

return 0;

// Implement the rest thank you

}

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

Hi i am answering your question giving you the Program to find second largest element in a BST ,Hope this will be helpful to you kindly try to delete the Element yourself and if you found any difficulty i ll do accordingly,Happy to help:)

code:-

#include<iostream>
using namespace std;

struct Node
{
   int key;
   Node *left, *right;
};

Node *Create_New_Node(int item)
{
   Node *temp = new Node;
   temp->key = item;
   temp->left = temp->right = NULL;
   return temp;
}

void Second_Largest_Element(Node *root, int &c)
{

   if (root == NULL || c >= 2)
       return;

   Second_Largest_Element(root->right, c);
   c++;
   if (c == 2)
   {
       cout << "2nd largest element is "
           << root->key << endl;
       root->key == NULL;
       return;
   }

   Second_Largest_Element(root->left, c);
}

void Sec_Large(Node *root)
{
   int c = 0;
   Second_Largest_Element(root, c);
}

Node* Insert_Node(Node* node, int key)
{
   if (node == NULL) return Create_New_Node(key);
   if (key < node->key)
       node->left = Insert_Node(node->left, key);
   else if (key > node->key)
       node->right = Insert_Node(node->right, key);
   return node;
}

int main()
{
   Node *root = NULL;
   root = Insert_Node(root, 50);
   Insert_Node(root, 30);
   Insert_Node(root, 20);
   Insert_Node(root, 40);
   Insert_Node(root, 70);
   Insert_Node(root, 60);
   Insert_Node(root, 80);

   Sec_Large(root);

   return 0;
}

output:-

Add a comment
Know the answer?
Add Answer to:
Remove the second largest item from a BST. Write the complete code for this assuming you...
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
  • 1. Construct a Binary Search Tree 50 Write code to implement a BST. Implement an add) method and ...

    C++ program 1. Construct a Binary Search Tree 50 Write code to implement a BST. Implement an add) method and a remove) method. Use the following input to construct a BST: 50,20,75,98,80,31,150, 39, 23,11,77 20 75 31 98 0 (150 Hint on removing If the node to be removed is: a leaf node a node with a left child only a node with a right child only a node with both children Then take this action replace with null replace...

  • Hello, I've been working on this for a while and I can't figure the rest out....

    Hello, I've been working on this for a while and I can't figure the rest out. Need asap if anyone can help. maxBSt,minBST,isBST, and inOrder package lab7; import java.util.ArrayList; import java.util.LinkedList; import java.util.Queue; public class TreeExercise {    /*    * Construct BST from preorder traversal    */    public static Node<Integer> consBSTfromPreOrder(int[] arr, int start, int end)    {                       if(start > end) return null;               Node<Integer> root = new Node<Integer>(arr[start],...

  • BST JAVA FILE import java.util.*; public class BST <E extends Comparable <E>> {    private TreeNode<E>...

    BST JAVA FILE import java.util.*; public class BST <E extends Comparable <E>> {    private TreeNode<E> overallRoot;    public BST() {        overallRoot = null;    }    // ************ ADD ************ //    public void add(E addThis) {        if (overallRoot == null) {            overallRoot = new TreeNode<>(addThis);        } else {            add(overallRoot, addThis);        }    }    private TreeNode<E> add(TreeNode<E> node, E addThis) {        if...

  • In this assignment, you will add several methods to the Binary Search Tree. You should have compl...

    In this assignment, you will add several methods to the Binary Search Tree. You should have completed the following three methods in the lab: public void insert(Key key, Value value) public Value get(Key key) public void inorder(Node root) For this assignment, you will implement the following: public void remove(Node root, Key key) public Key getMin(Node n) public Key getMax(Node n) public int height(Node n) The main method contains the statements to check whether your implementation works. You need to change...

  • Consider the partial implementation of a Binary Search Tree (BST) class. For simplicity, each Node stores...

    Consider the partial implementation of a Binary Search Tree (BST) class. For simplicity, each Node stores only the key. Add a public member function to class BST that returns the largest absolute value in the tree. The language is C++ Want the height #4 Coding [6 points] Consider the partial implementation of a Binary Search Tree (BST) class. For simplicity, each Node stores only the key. Add a public member function to class BST that returns the height of the...

  • package hw3; import java.util.LinkedList; /* *********************************************************************** * A simple BST with int keys and no values...

    package hw3; import java.util.LinkedList; /* *********************************************************************** * A simple BST with int keys and no values * * Complete each function below. * Write each function as a separate recursive definition (do not use more than one helper per function). * Depth of root==0. * Height of leaf==0. * Size of empty tree==0. * Height of empty tree=-1. * * TODO: complete the functions in this file. * DO NOT change the Node class. * DO NOT change the name...

  • Can you complete this program using same code please. DO not change any code. Thank you...

    Can you complete this program using same code please. DO not change any code. Thank you In this problem you are given the root of a BST and you want to return the summation of all the values in the BST. Remember that the summation of a BST is the summation of all the nodes in the left subtree + the summation of all the nodes in the right subtree + the value of the current node Expected behavior: root...

  • Write a function int levelSearch(Node* root, int key) that takes as input the root node of...

    Write a function int levelSearch(Node* root, int key) that takes as input the root node of a Binary Search tree and a key. The function searches the key in the BST and returns the level of the found node. If the key is not found in the tree, return -1. The level starts at 0 for the root node and increases from top to bottom. We have defined the following node C++ Node class for you: class Node { public:         ...

  • using java to write,show me the output. please write some common. You CAN NOT use inbuild...

    using java to write,show me the output. please write some common. You CAN NOT use inbuild functions for Tree ADT operations. using code below to finsih public class Main {    public static void main(String[] args) {        BinaryTree tree = new BinaryTree(); tree.root = new Node(1); tree.root.left = new Node(2); tree.root.right = new Node(3); tree.root.left.left = new Node(4); tree.root.left.right = new Node(5); tree.root.right.left = new Node(6); tree.root.right.right = new Node(7); tree.root.left.left.left = new Node(8); tree.root.left.left .right= new Node(9);...

  • Having code issues wth my C++ program. My program checks if two binary trees are similar...

    Having code issues wth my C++ program. My program checks if two binary trees are similar and if they're not they return false. My program is return true with different binary trees. Could use some help thanks #include <iostream> #include <string> using namespace std; //Struct of Nodes struct BinarySearchTree { int data; BinarySearchTree *left; BinarySearchTree *right; }; // Inserting nodes into BST BinarySearchTree* insert( BinarySearchTree* node, int val) { if (node == NULL) { BinarySearchTree *newNode = new BinarySearchTree(); newNode->data...

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