15,506,384 members
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 8:42am
v2
Patrice T 3-Jan-21 12:19pm
And you have a question ?
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.
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
so i can start the proper code above code is not complete

## Solution 1

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[^]