Haskell, where it says import Debug.Trace f- search takes the following paramete
ID: 3595112 • Letter: H
Question
Haskell, where it saysimport Debug.Trace f- search takes the following parameters 1. A function to check if the search is done - this function takes: a. the global information for the problerm b. the partial solution - the function returns a bool indicating if solution is complete 2. A function to find the valid next states to go to - this function takes: a. the global information for the problerm b. a partial solution -the funtion returns a list of valid next states 3. Global information for the problem 4. A partial solution - a partial solution is a list of states in reverse search returns a list of all full solutions reachable from the partial solution - full solutions will be in forward order search :: (Show a, Show b) => p trace "search " show --search p) False = undefined -Your code goes here search
Explanation / Answer
The C++ function to check whether the given tree is Binary Search Tree is as follows:
bool isBST(struct node* root)
{
static struct node *prev = NULL;
// traverse the tree in inorder fashion and keep track of the previous node
if (root)
{
if (!isBST(root->left))
return false;
// Allows only distinct valued nodes
if (prev != NULL && root->data <= prev->data)
return false;
prev = root;
return isBST(root->right);
}
return true;
}
Code to copy:
// ConsoleApplication28.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
using namespace std;
/* A binary tree node has data, pointer to left child
and a pointer to right child */
struct node
{
int data;
struct node* left;
struct node* right;
};
bool isBST(struct node* root)
{
static struct node *prev = NULL;
// traverse the tree in inorder fashion and keep track of the previous node
if (root)
{
if (!isBST(root->left))
return false;
// Allows only distinct valued nodes
if (prev != NULL && root->data <= prev->data)
return false;
prev = root;
return isBST(root->right);
}
return true;
}
/*This function allocates a new node with the
given data and NULL left and right pointers. */
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);
}
/* Driver program to test above functions*/
int main()
{
struct node *root = newNode(7);
root->left = newNode(5);
root->right = newNode(10);
root->left->left = newNode(1);
root->left->right = newNode(6);
root->right->left = newNode(9);
root->right->right = newNode(8);
if (isBST(root))
cout<<"The given tree is a BST";
else
cout<<"The given tree is not a BST";
getchar();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.