Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
write a function treeType(Node *) in c++ When root node of a data structure is passed it checks following things
•Is it a Tree?
•Is it a Binary Tree?
•Is it a BST?

What I have tried:

C++
#include <iostream>
#include <climits>
using namespace std;
 
// Data structure to store a Binary Search Tree node
struct Node
{
    int data;
    Node *left, *right;
};
 
// Function to create a new binary tree node having given key
Node* newNode(int key)
{
    Node* node = new Node;
    node->data = key;
    node->left = node->right = nullptr;
 
    return node;
}
 
// Recursive function to insert a key into BST
Node* insert(Node* root, int key)
{
    // if the root is null, create a new node and return it
    if (root == nullptr)
        return newNode(key);
 
    // if given key is less than the root node, recur for left subtree
    if (key < root->data)
        root->left = insert(root->left, key);
 
    // if given key is more than the root node, recur for right subtree
    else
        root->right = insert(root->right, key);
 
    return root;
}
 
// Function to determine if given Binary Tree is a BST or not by keeping a
// valid range (starting from [MIN_VALUE, MAX_VALUE]) and keep shrinking
// it down for each node as we go down recursively
bool isBST(Node* node, int minKey, int maxKey)
{
    // base case
    if (node == NULL)
        return true;
 
    // if node's value fall outside valid range
    if (node->data < minKey || node->data > maxKey)
        return false;
 
    // recursively check left and right subtrees with updated range
    return isBST(node->left, minKey, node->data) &&
            isBST(node->right, node->data, maxKey);
}
 
// Function to determine if given Binary Tree is a BST or not
void isBST(Node* root)
{
    if (isBST(root, INT_MIN, INT_MAX))
        printf("This is a BST.");
    else
        printf("This is NOT a BST!");
}
 
int main()
{
    Node* root = nullptr;
    int keys[] = { 15, 10, 20, 8, 12, 16, 25 };
 
    for (int key : keys)
        root = insert(root, key);
 
    // swap nodes
    swap(root->left, root->right);
    isBST(root);
 
    return 0;
}
Posted
Updated 3-Jan-21 7:42am
v2
Comments
Patrice T 3-Jan-21 12:19pm    
And you have a question ?
muhammad mushtaq2 3-Jan-21 12:25pm    
yes
this is my question
write a function treeType(Node *) in c++ When root node of a data structure is passed it checks following things
•Is it a Tree?
•Is it a Binary Tree?
•Is it a BST?
Richard MacCutchan 3-Jan-21 12:42pm    
No, that is just a repeat of your original post. You need to ask a specific question in relation to the code that you have posted.
muhammad mushtaq2 3-Jan-21 13:19pm    
i just need the the logic to how can i check that data structure passed is a tree an if it is a tree than is it binary tree
muhammad mushtaq2 3-Jan-21 13:20pm    
so i can start the proper code above code is not complete

1 solution

We are more than willing to help those that are stuck: but that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900