Question

c++ What is the definition of the public method setRootData for binary tree? Implement it using...

c++

What is the definition of the public method setRootData for binary tree? Implement it using a recursive helper in a way that the resulting binary tree stayed balanced.

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

Answer   public method means a method which we can access every .

Set method is used for access the private attribute.

#include<bits/stdc++.h>

#include<iostream>

using namespace std;

  class node

{

    public:

    int data;

    node* left;

    node* right;

  node(int data)

    {

        this->data = data;

        this->left = NULL;

        this->right = NULL;

    }

};

  

int isBSTUtil(node* node, int min, int max);

  

int isBST(node* node)

{

    return(isBSTUtil(node, INT_MIN, INT_MAX));

}

  int isBSTUtil(node* node, int min, int max)

{

    

    if (node==NULL)

        return 1;

  if (node->data < min || node->data > max)

        return 0;

   return

        isBSTUtil(node->left, min, node->data-1) && // Allow only distinct values

        isBSTUtil(node->right, node->data+1, max); // Allow only distinct values

}

  /* main code*/

int main()

{

    node *root = new node(4);

    root->left = new node(2);

    root->right = new node(5);

    root->left->left = new node(1);

    root->left->right = new node(3);

        if(isBST(root))

        cout<<"Is BST";

    else

        cout<<"Not a BST";

              return 0;

}

  

Add a comment
Know the answer?
Add Answer to:
c++ What is the definition of the public method setRootData for binary tree? Implement it using...
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
  • C++ (Using Binary Search Trees) other methods will result in downvote Implement the binary search tree...

    C++ (Using Binary Search Trees) other methods will result in downvote Implement the binary search tree methods (bst.cpp) for the binary search tree provided in the header file. Test your implementation with the included test. bst.h bst_test.cpp Note: Your implementation must correspond to declarations in the header file, and pass the test. Do not modify these two. I will compile your code against these. If the compilation fails, you will get down vote. bst.h #ifndef BINARY_SEARCH_TREE_H #define BINARY_SEARCH_TREE_H #include <string>...

  • C++ ONLY This question has 2 parts (a, and b). The partial definition of tree with...

    C++ ONLY This question has 2 parts (a, and b). The partial definition of tree with up to 3-nodes for each node is given below. This is not a binary search tree! class Tree3 { public : Tree3() {} private : class Node { public : int Val ; Node * Left ; Node * Middle ; Node * Right ; }; }; Node * Root { nullptr }; a) A Node is considered balanced if the sum of the...

  • in python 11.1 Binary Search Tree In this assignment, you will implement a Binary Search Tree...

    in python 11.1 Binary Search Tree In this assignment, you will implement a Binary Search Tree You will also need to implement a Node class. This class will not be tested, but is needed to implement the BST. Your BST must implement the following methods. You are free to implement additional helper methods. It is recommended you create your own helper methods Constructor: Creates an Empty Tree String Method: Returns the string "Empty Tree" for an empty tree. Otherwise, returns...

  • This is for java programming. We need a public method for our Binary Search Tree ADT...

    This is for java programming. We need a public method for our Binary Search Tree ADT that returns a reference to the information in the node with the "smallest" value in the tree. The signature of the method is public T min() a. Design an iterative version of the method. b. Design a recursive version of the method. c. Which approach is better? Explain.

  • C++. Test if a Tree is Balanced Use the given Tree class and implement a function...

    C++. Test if a Tree is Balanced Use the given Tree class and implement a function to test if the binary tree is balanced or not. An empty tree is height-balanced. A non-empty binary tree T is balanced if: 1) Left subtree of T is balanced 2) Right subtree of T is balanced 3) The difference between heights of left subtree and right subtree is not more than 1. #include <iostream> using namespace std; template<class ItemType> class Tree { private:...

  • C++. Test if a Tree is Balanced Use the given Tree class and implement a function...

    C++. Test if a Tree is Balanced Use the given Tree class and implement a function to test if the binary tree is balanced or not. An empty tree is height-balanced. A non-empty binary tree T is balanced if: 1) Left subtree of T is balanced 2) Right subtree of T is balanced 3) The difference between heights of left subtree and right subtree is not more than 1. Please MAKE SURE TO CLEARLY SHOW THE CODING. AND YOUR OUTPUT....

  • C++ (b) Consider a binary search tree of positive integers without duplicates. Implement (write out) a...

    C++ (b) Consider a binary search tree of positive integers without duplicates. Implement (write out) a program to find the second greatest element in the tree. The Second function is a member function of class BinarySearchTree. The function returns zero if the tree is empty or has only one element, otherwise it returns the value of the second greatest element. Based on the classes provided below, write the implementation of Second in the solution box, including any helper functions (if...

  • Recall from Assignment 2 the definition of a binary tree data structure: either an empty tree,...

    Recall from Assignment 2 the definition of a binary tree data structure: either an empty tree, or a node with two children that are trees. Let T(n) denote the number of binary trees with n nodes. For example T(3) 5 because there are five binary trees with three nodes: (a) Using the recursive definition of a binary tree structure, or otherwise, derive a recurrence equation for T(n). (8 marks) A full binary tree is a non-empty binary tree where every...

  • IN JAVA 2 A Binary Search Tree The goal of this lab is to gain familiarity...

    IN JAVA 2 A Binary Search Tree The goal of this lab is to gain familiarity with simple binary search trees. 1. Begin this lab by implementing a simple class that represents a "node” in a binary search tree, as follows. public class MyTreeNode<t extends Comparable<T>> { public T data; public MyTreeNode<T> leftchild; public MyTreeNode<T> rightChild; public MyTreeNode<T> parent; 2. Have the second member of your pair type in the code for the simple binary search tree interface. public interface...

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