Question

Haskell coding problem please do the last 3

Problem One (Non-Polymorphic Type Inference): Give the type of the following expressions. We assume that we can have Integer

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

#include <stdio.h>
#include <stdlib.h>

/structure of the node -> data|left|right/
struct node
{
int data;
struct node* left;
struct node* right;
};
struct node *TEMP,*ROOT; //gobal node pointers

/function to count leaves of left and right subtrees recursively/

unsigned int countLeaves(struct node* node)
{
if(node == NULL)
return 0;
if(node->left == NULL && node->right==NULL) // leaves have left and right pointers of node set to NULL
return 1;   
else
return countLeaves(node->left)+
countLeaves(node->right);   
}
  
/function to create new node/
struct node* newNode(int data)
{
/*initially left and right ointers are ste to NULL */
struct node* node = (struct node*) malloc(sizeof(struct node)); //dynamic allocation of memory
node->data = data;
node->left = NULL;
node->right = NULL;
  
return(node);
}

/* Recursive function to check is a node is present. This is used to check if parent
node to which the child is to be attached is present*/
int isPresent(struct node* root, int x) {
if (root != NULL) {

// check if current node has the element we are looking for
if (root->data == x) {
TEMP=root;
return 1;
} else {
// check if the sub trees
return isPresent(root->left, x) || isPresent(root->right, x);
}
}
return 0;
}
  
  
/to traverse the tree level by level and dispaly the data/
void displayLevelOrder(struct node* root)
{
int h = height(root); //to determine number of levels
int i;
for (i = 1; i <= h; i++)
{
  
dispCurrentLevel(root, i);
printf("\n");
}
}
  
/* Print nodes at a given level */
void dispCurrentLevel(struct node* root, int level)
{
if (root == NULL)
return;
if (level == 1)
printf("%d\t",root->data);
else if (level > 1)
{
dispCurrentLevel(root->left, level-1);
dispCurrentLevel(root->right, level-1);
}
}
  
int height(struct node* node)
{
if (node == NULL)
return 0;
else
{
/* compute the height of left and right subtrees recursively */
int lheight = height(node->left);
int rheight = height(node->right);
  
/* larger one is used*/
if (lheight > rheight)
return(lheight + 1);
else return(rheight + 1);
}
}

void createTree()
{
int num_c,num_p;
int temp;
char ch;
  
  
printf("enter the data of the child node to be inserted: \n");
scanf("%d",&num_c);
  
printf("enter the data of the parent node to be inserted: \n");
scanf("%d",&num_p);
  
temp=isPresent(ROOT,num_p);//check if parent is present
  
/if parent node is not found./
if(temp==0)
{
printf("there is no parent node with data %d",num_p);
  
}
/if parent node is found/
else
{
  
printf("insert L or R ?: \n");//prompt user whetehr to attach the node to left or rigth of parent node
scanf("%c",&ch);// neglect '\n' when user presses ENTER
scanf("%c",&ch);
if(ch=='L' && TEMP->left==NULL)
{
TEMP->left=newNode(num_c);//attaching newnode to left of parent
printf("inserted node %d to the LEFT of parent %d\n",num_c,num_p);
}
else if(ch=='R' && TEMP->right==NULL)
{
TEMP->right=newNode(num_c);//attaching newnode to right of parent
printf("inserted node %d to the RIGHT of parent %d\n",num_c,num_p);
}
else
{
printf("Required position is not empty");
  
}
  
}
  
  
  
}
  

int main()
{
  
  
int ch,num;
printf("enter the data of root node:\n");
scanf("%d",&num);
ROOT=newNode(num);
printf("press 1 to keep inserting new nodes to tree else press 0\n");
scanf("%d",&ch);
while(ch){
  
createTree();
printf("press 1 to keep inserting new nodes to tree else press 0\n");
scanf("%d",&ch);
}
  
printf("LEVEL ORDER TRAVERSAL\n");
displayLevelOrder(ROOT);
  
/get leaf count/
printf("Leaf count of the tree is %d", countLeaves(ROOT));
  
getchar();
return 0;
}

Add a comment
Know the answer?
Add Answer to:
Haskell coding problem please do the last 3 Problem One (Non-Polymorphic Type Inference): Give the type of the followin...
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
  • Haskell coding problem please do problem two Here is problem one for the types Problem Two (Polymorphic Type Inference...

    Haskell coding problem please do problem two Here is problem one for the types Problem Two (Polymorphic Type Inference): Give the type of the following functions in the space indicated. Assume that we have only the types given in Problem One, plus Haskell's built-in types Integer and Bool. Assume that all arithmetic operators take only Integer, e.g., () is Integer -> Integer -> Integer. test :: test x xs = (Cons x (Cons (x+1) xs)) second second xs = f...

  • The last 3 cases are tests .cpp files to test the code. The language of this...

    The last 3 cases are tests .cpp files to test the code. The language of this code must be C++ because that is the only I am familiar with. Soreland, a software company, is planning on releasing its first C++ compiler with the hopes of putting MiniSoft's Visual C++ out of business (that reminds me of another story). Consequently, Soreland has contracted with the Fruugle Corporation to design and build part of their compiler. Of course, since Fruugle gets all...

  • Question 1 An array is NOT: A - Made up of different data types. B - Subscripted by integers. C -...

    Question 1 An array is NOT: A - Made up of different data types. B - Subscripted by integers. C - A consecutive group of memory chunks. D - None of the choices. Question 2 How many times is the body of the loop executed? int i=1; while(true) { cout << i; if(++i==5) break; } A - Forever B - 4 C - 5 D - 6 E - 0 Question 3 What is wrong with the following piece of...

  • **DO IT AS PYTHON PLEASE** The Trifid Cipher General Problem Description The Trifid cipher (not to be confused with the...

    **DO IT AS PYTHON PLEASE** The Trifid Cipher General Problem Description The Trifid cipher (not to be confused with the creatures from the classic science-fiction film "The Day of the Triffids") is an algorithm that enciphers a plaintext message by encoding each letter as a three-digit number and then breaking up and rearranging the digits from each letter's encoded form. For this assignment, you will create a set of Python functions that can encode messages using this cipher (these functions...

  • Requirements Print a range Write a bag member function with two parameters. The two parameters are...

    Requirements Print a range Write a bag member function with two parameters. The two parameters are Items x and y. The function should write to the console all Items in the bag that are between the first occurrence of x and the first occurrence of y. You may assume that items can be compared for equality using ==. Use the following header for the function: void print_value_range(const Item& x, const Item& y); print_value_range can be interpreted in a number of...

  • JAVA Problem: With the recent news about data breaches and different organizations having their clients’ information...

    JAVA Problem: With the recent news about data breaches and different organizations having their clients’ information being exposed, it is becoming more and more important to find ways to protect our sensitive data. In this program we will develop a simple tool that helps users generate strong passwords, encrypt and decrypt data using some cyphering techniques. You will need to create two classes. The first class is your driver for the application and contains the main method. Name this class...

  • Please provide original Answer, I can not turn in the same as my classmate. thanks In...

    Please provide original Answer, I can not turn in the same as my classmate. thanks In this homework, you will implement a single linked list to store a list of computer science textbooks. Every book has a title, author, and an ISBN number. You will create 2 classes: Textbook and Library. Textbook class should have all above attributes and also a “next” pointer. Textbook Type Attribute String title String author String ISBN Textbook* next Textbook Type Attribute String title String...

  • I need help implemeting the remove_repetitions() Here is a brief outline of an algorithm: A node...

    I need help implemeting the remove_repetitions() Here is a brief outline of an algorithm: A node pointer p steps through the bag For each Item, define a new pointer q equal to p While the q is not the last Item in the bag If the next Item has data equal to the data in p, remove the next Item Otherwise move q to the next Item in the bag. I also need help creating a test program _____________________________________________________________________________________________________________________________________________________ #ifndef...

  • PLEASE HURRY. Below is the prompt for this problem. Use the code for bag1.cxx, bag1.h and...

    PLEASE HURRY. Below is the prompt for this problem. Use the code for bag1.cxx, bag1.h and my code for bag.cpp. Also I have provided errors from linux for bag.cpp. Please use that code and fix my errors please. Thank you The goal of assignment 3 is to reinforce implementation of container class concepts in C++. Specifically, the assignment is to do problem 3.5 on page 149 of the text. You need to implement the set operations union, intersection, and relative...

  • I've posted 3 classes after the instruction that were given at start You will implement and...

    I've posted 3 classes after the instruction that were given at start You will implement and test a PriorityQueue class, where the items of the priority queue are stored on a linked list. The material from Ch1 ~ 8 of the textbook can help you tremendously. You can get a lot of good information about implementing this assignment from chapter 8. There are couple notes about this assignment. 1. Using structure Node with a pointer point to Node structure to...

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