Question

/* * struct for a single node in a binary tree. data contains the int

* stored in this node. left and right contain pointers to the left and

* right subtrees respectively. *

* All of the ints stored in the left subtree is smaller than data.

* All of the ints stored in the right subtree is larger than data.

*/

struct node {

int data;

struct node *left;

struct node *right;

};

typedef struct node node;

Write a recursive function treeTrim which deletes all of the tree nodes on level n or lower. current contains the current level. The function returns a pointer to the node that the parent of tree should now point to (this will be either tree or NULL node* treeTrim(node «tree, int n, int current) ( W1 Write the previous function without passing the current level (hint: you need to change n when going down levels) node, treeTrin(node tree, int n)t

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

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
struct node
{
int data;
struct node* left;
struct node* right;
};

int isBSTUtil(struct node* node, int min, int max);
int isBST(struct node* node)
{
return(isBSTUtil(node, INT_MIN, INT_MAX));
}
int isBSTUtil(struct 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
}

struct node* newNode(int data)
{
struct node* node = (struct node*)
malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;

return(node);
}
int main()
{
struct node *root = newNode(4);
root->left = newNode(2);
root->right = newNode(5);
root->left->left = newNode(1);
root->left->right = newNode(3);

if(isBST(root))
printf("Is BST");
else
printf("Not a BST");

getchar();
return 0;
}

using NULL pointers
#include <bits/stdc++.h>
using namespace std;

struct Node
{
int data;
struct Node* left, *right;
};
bool isBST(Node* root, Node* l=NULL, Node* r=NULL)
{
// Base condition
if (root == NULL)
return true;
if (l != NULL and root->data < l->data)
return false;
if (r != NULL and root->data > r->data)
return false;
return isBST(root->left, l, root) and
isBST(root->right, root, r);
}

struct Node* newNode(int data)
{
struct Node* node = new Node;
node->data = data;
node->left = node->right = NULL;
return (node);
}
int main()
{
struct Node *root = newNode(3);
root->left = newNode(2);
root->right = newNode(5);
root->left->left = newNode(1);
root->left->right = newNode(4);

if (isBST(root))
cout << "Is BST";
else
cout << "Not a BST";

return 0;
}

Add a comment
Know the answer?
Add Answer to:
/* * struct for a single node in a binary tree. data contains the int *...
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
  • CODE IN C Objectives: Queue operations. Data structure: typedef struct { int width; int heig...

    CODE IN C Objectives: Queue operations. Data structure: typedef struct { int width; int height; }Rect; typedef struct node { Rect* r; struct node* next; }Node; typedef struct { Node* head; Node* tail; }Queue; Specification: In this lab six Queue-related operation functions need to be implemented by using the given function prototypes and data structures. 1. List* createQueue(void); This function initializes an empty “Queue” with the “Queue” data structure and returns an empty queue. 2. int enQueue(List*); This function receives...

  • By definition, the height of a node in a binary tree is the number of edges...

    By definition, the height of a node in a binary tree is the number of edges along the longest path from the node to any leaf. Assume the following node structure struct TreeNode! int data; node Type right; // points to right child node Type "left; // points to left child }; Write a recursive function that takes a pointer to a node in a binary tree and returns its height. Note: the height of a leaf node is o...

  • CODE IN C Objectives: Queue operations. Data structure: typedef struct { int width; int height; }Rect;...

    CODE IN C Objectives: Queue operations. Data structure: typedef struct { int width; int height; }Rect; typedef struct node { Rect* r; struct node* next; }Node; typedef struct { Node* head; Node* tail; }Queue; Specification: In this lab six Queue-related operation functions need to be implemented by using the given function prototypes and data structures. 1. List* createQueue(void); This function initializes an empty “Queue” with the “Queue” data structure and returns an empty queue. 2. int enQueue(List*); This function receives...

  • By definition, the height of a node in a binary tree is the number of edges...

    By definition, the height of a node in a binary tree is the number of edges along the longest path from the node to any leaf. Assume the following node structure struct TreeNode int data; node Type right; // points to right child node Type "Left; // points to left child ) Write a recursive function that takes a pointer to a node in a binary tree and returns its height. Note: the height of a leaf node is 0...

  • PROMPT: Consider a binary tree (not necessarily a binary search tree) with node structure. QUESTION: Prove that findMax works by mathematical induction. struct Node int val; struct Node * left; struc...

    PROMPT: Consider a binary tree (not necessarily a binary search tree) with node structure. QUESTION: Prove that findMax works by mathematical induction. struct Node int val; struct Node * left; struct Node* right; The following function findMax returns the largest value 'val in the tree; and returns -1 if the tree is empty. You may assume that all the values 'val' in the tree are nonnegative. struct Node * findMax(struct Node root) if (rootNULL) return -1; maxval = root->val; maxval...

  • In a binary tree, the balance ratio of node v, bal(v), is the number of nodes...

    In a binary tree, the balance ratio of node v, bal(v), is the number of nodes in the left subtree of node v divided by the sum of the number of nodes in the right and left subtrees of node v. bal(a leaf node) = ½. A tree T is said to be ε-balanced if, for all nodes v in T, ½ - ε < bal(v) < ½ + ε. Design an efficient recursive algorithm that determines whether a binary...

  • Given the following linked list structure called node: struct node { int val; struct node *...

    Given the following linked list structure called node: struct node { int val; struct node * ptrNext; }; Assume we have a single list created from this structure with a head pointer called ptrFirst which is declared in the global scope. a. Write a complete C function called CountEven to count all the even values in this singly linked list of arbitrary number of nodes using an iterative (non-recursive) approach. The function takes as parameter the pointer to the starting...

  • Coding Language: C++ Function Header: vector<vector<int>> printFromButtom(TreeNode* root) {} Definition for a binary tree node: struct...

    Coding Language: C++ Function Header: vector<vector<int>> printFromButtom(TreeNode* root) {} Definition for a binary tree node: struct TreeNode { int val; TreeNode *left; TreeNode *right; }; The Problem Complete the printFromButtom function that accepts a BST TreeNode and returns the nodes' value from left to right, level by level from leaf to root. This function will return vector<vector int which similar to a 2-D array. Function std: reverse (myvector.begin myVector en might be helpful. Definition for a binary tree node: struct...

  • 1) (10 pts) Write a recursive function that counts and returns the number of nodes in...

    1) (10 pts) Write a recursive function that counts and returns the number of nodes in a binary tree with the root root, that store an even value. Please use the struct shown and function prototype shown below. (For example, if the tree rooted at root stored 2, 3, 4, 8, 13, 18 and 20, the function should return 5, since there are five even values [2,4,8,18,20] stored in the tree. typedef struct node { int data; struct node* left;...

  • Using C Please comment Part 1: BST Create a link based Binary Search tree composed of a Node and a Tree struct. You should have a header file, BST.h, with the following: o Node struct containing...

    Using C Please comment Part 1: BST Create a link based Binary Search tree composed of a Node and a Tree struct. You should have a header file, BST.h, with the following: o Node struct containing left, right, and parent pointers, in addition to holding an Data struct value Tree struct containing a pointer to the root of the tree A function declaration for a function that allocates a tree, and initializes the root to NULL o o o A...

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