Question

BST_Node *BST_harmonize(BST_Node *root, int semitones, double time_shift) { /* Let's play with no...

BST_Node *BST_harmonize(BST_Node *root, int semitones, double time_shift)
{
/* Let's play with notes, because we can.
*
* This function traverses the BST, and for each existing
* note, inserts a new, modified note (i.e. it will add sounds
* to an already existing song, based on the notes it already has)
*
* The new note has the followin properties:
* - The frequency is shifted by the specified number of semitones
* (A semitone is the difference between one note and the
* immediately next one in the musical scale - ot what is the
* same, the difference in pitch between a white key and the
* black key immediately next to it in a piano)
* - It plays in the same *bar* as the original note
* - But its *index* is shifted by the specified time_shift
* (this value is between 0 and 1, but you have to check
* that the final index remains between 0 and 1)
*
* Both the 'semitones' and 'time_shift' parameter can be
* positive or negative. A typical value for semitones
* could be 4 or 7, corresponding to musical 3rds or
* musical 5ths - this should produce some interesting
* harmony! but you can play with this function and try
* various things for fun.
*
* In practice, figuring out the frequency of the note
* you have to insert is easy. For instance, suppose
* you have an existing note in the BST with
*
* freq=440.0, at bar=10, index=.25
*
* Now suppose the user specified
*
* semitones=4
* time_shift=.1
*
* Then you go to the note_freq[] array, find the index
* of the entry for frequency 440.0 (let's say it's
* j), then go to note_freq[j+4] to find the frequency
* of the note you need to add.
*
* NOTE: If the value of j+semitones is < 0 or > 99
* then there is no note with the frequency you
* want. In that case you don't insert a new
* note.
*
* You then add a new note with that frequency at
* bar=10 (same bar!)
* index=.25 + .1 (the original index plus the time_shift)
*
* NOTE: If the resulting index is less than 0, or >= 1,
* then you DO NOT insert the new note.
*
* This function is crunchy - and if you don't think it
* through before you start implementing it you'll run into
* all kinds of trouble.
*
* This is the problem solving exercise for A2, so don't
* look for people on Piazza to give you answers, or tell you
* what to do, or verify you're doing the right thing.
*
* It's up to you how to solve this, and if you want an opinion,
* you can come to visit us during office hours!
*
* Expected result: The BST will have about twice as many notes
* as before, and the new notes are shifted in pitch and
* in time as specified by the parameters.
*/
  
/*** TO DO:
* Implement this function
****/
  
return NULL;

}

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

# include <stdio.h>
# include <malloc.h>

struct nodess
{
   int data;
   struct nodess *left;
   struct nodess *right;
}*rooot;

void finding(int items,struct nodess **parm,struct nodess **loc)
{
   struct nodess *pointr,*pointrsave;

   if(rooot==NULL)
   {
       *loc=NULL;
       *parm=NULL;
       return;
   }
   if(items==rooot->data)
   {
       *loc=rooot;
       *parm=NULL;
       return;
   }
   if(items<rooot->data)
       pointr=rooot->left;
   else
       pointr=rooot->right;
   pointrsave=rooot;

   while(pointr!=NULL)
   {
       if(items==pointr->data)
       { *loc=pointr;
           *parm=pointrsave;
           return;
       }
       pointrsave=pointr;
       if(items<pointr->data)
           pointr=pointr->left;
       else
           pointr=pointr->right;
   }
   *loc=NULL;
   *parm=pointrsave;
}

void insert(int items)
{ struct nodess *temp,*parmnt,*loction;
   finding(items,&parmnt,&loction);
   if(loction!=NULL)
   {
       printf("items already present");
       return;
   }

   temp=(struct nodess *)malloc(sizeof(struct nodess));
   temp->data=items;
   temp->left=NULL;
   temp->right=NULL;

   if(parmnt==NULL)
       rooot=temp;
   else
       if(items<parmnt->data)
           parmnt->left=temp;
       else
           parmnt->right=temp;
}


void case_a(struct nodess *parm,struct nodess *loc )
{
   if(parm==NULL)
       rooot=NULL;
   else
       if(loc==parm->left)
           parm->left=NULL;
       else
           parm->right=NULL;
}

void case_b(struct nodess *parm,struct nodess *loc)
{
   struct nodess *child;
   if(loc->left!=NULL)
       child=loc->left;
   else
       child=loc->right;

   if(parm==NULL )
       rooot=child;
   else
       if( loc==parm->left)
           parm->left=child;
       else
           parm->right=child;
}

void case_c(struct nodess *parm,struct nodess *loc)
{
   struct nodess *pointr,*pointrsave,*suc,*parmsuc;
   pointrsave=loc;
   pointr=loc->right;
   while(pointr->left!=NULL)
   {
       pointrsave=pointr;
       pointr=pointr->left;
   }
   suc=pointr;
   parmsuc=pointrsave;

   if(suc->left==NULL && suc->right==NULL)
       case_a(parmsuc,suc);
   else
       case_b(parmsuc,suc);

   if(parm==NULL)
       rooot=suc;
   else
       if(loc==parm->left)
           parm->left=suc;
       else
           parm->right=suc;

   suc->left=loc->left;
   suc->right=loc->right;
}
int del(int items)
{
   struct nodess *parmnt,*loction;
   if(rooot==NULL)
   {
       printf("Tree empty");
       return 0;
   }

   finding(items,&parmnt,&loction);
   if(loction==NULL)
   {
       printf("items not present in tree");
       return 0;
   }

   if(loction->left==NULL && loction->right==NULL)
       case_a(parmnt,loction);
   if(loction->left!=NULL && loction->right==NULL)
       case_b(parmnt,loction);
   if(loction->left==NULL && loction->right!=NULL)
       case_b(parmnt,loction);
   if(loction->left!=NULL && loction->right!=NULL)
       case_c(parmnt,loction);
   free(loction);
}

int preorder(struct nodess *pointr)
{
   if(rooot==NULL)
   {
       printf("Tree is empty");
       return 0;
   }
   if(pointr!=NULL)
   {
       printf("%d ",pointr->data);
       preorder(pointr->left);
       preorder(pointr->right);
   }
}

void inorder(struct nodess *pointr)
{
   if(rooot==NULL)
   {
       printf("Tree is empty");
       return;
   }
   if(pointr!=NULL)
   {
       inorder(pointr->left);
       printf("%d ",pointr->data);
       inorder(pointr->right);
   }
}

void postorder(struct nodess *pointr)
{
   if(rooot==NULL)
   {
       printf("Tree is empty");
       return;
   }
   if(pointr!=NULL)
   {
       postorder(pointr->left);
       postorder(pointr->right);
       printf("%d ",pointr->data);
   }
}

void display(struct nodess *pointr,int level)
{
   int i;
   if ( pointr!=NULL )
   {
       display(pointr->right, level+1);
       printf("\n");
       for (i = 0; i < level; i++)
           printf(" ");
       printf("%d", pointr->data);
       display(pointr->left, level+1);
   }
}
main()
{
   int choice,num;
   rooot=NULL;
   while(1)
   {
       printf("\n");
       printf("1.Insert\n");
       printf("2.Delete\n");
       printf("3.Inorder Traversal\n");
       printf("4.Preorder Traversal\n");
       printf("5.Postorder Traversal\n");
       printf("6.Display\n");
       printf("7.Quit\n");
       printf("Enter your choice : ");
       scanf("%d",&choice);

       switch(choice)
       {
       case 1:
           printf("Enter the number to be inserted : ");
           scanf("%d",&num);
           insert(num);
           break;
       case 2:
           printf("Enter the number to be deleted : ");
           scanf("%d",&num);
           del(num);
           break;
       case 3:
           inorder(rooot);
           break;
       case 4:
           preorder(rooot);
           break;
       case 5:
           postorder(rooot);
           break;
       case 6:
           display(rooot,1);
           break;
       case 7:
break;
       default:
           printf("Wrong choice\n");
       }
   }
}

Add a comment
Know the answer?
Add Answer to:
BST_Node *BST_harmonize(BST_Node *root, int semitones, double time_shift) { /* Let's play with no...
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
  • C# public int IndexOf(T element) { for (var i = 0; i < Count; i++) {...

    C# public int IndexOf(T element) { for (var i = 0; i < Count; i++) { if (data[i].Equals(element)) return i; } return -1; } You must complete the Vector<T> implementation by providing the following functionality: void Insert(int index, T item) Inserts a new element into the data structure at the specified index. This will involve four parts (Note a part may be more than a single line of code): o If Count already equals Capacity (eg the currently allocated space...

  • C programming Let's try to use the template below to implement a Set with double hashing....

    C programming Let's try to use the template below to implement a Set with double hashing. Here we assume the Set contains names with 3 characters long. Since it is a Set, we do not need to have an explicit value for each key. We will use a token value of 1 if a name belongs to the Set. Let's reuse the exact hash functions we have seen in example in Part 7 of the lecture notes, page 3. As...

  • Java Programming: The following is my code: public class KWSingleLinkedList<E> {    public void setSize(int size)...

    Java Programming: The following is my code: public class KWSingleLinkedList<E> {    public void setSize(int size)    {        this.size = size;    }    /** Reference to list head. */    private Node<E> head = null;    /** The number of items in the list */    private int size = 0;       /** Add an item to the front of the list.    @param item The item to be added    */    public void addFirst(E...

  • CSC 142 Music Player You will complete this project by implementing one class. Afterwards, your program...

    CSC 142 Music Player You will complete this project by implementing one class. Afterwards, your program will play music from a text file. Objectives Working with lists Background This project addresses playing music. A song consists of notes, each of which has a length (duration) and pitch. The pitch of a note is described with a letter ranging from A to G.   7 notes is not enough to play very interesting music, so there are multiple octaves; after we reach...

  • SCREENSHOTS OF CODE ONLY!! PLEASE DON'T POST TEXT!! C++ CODE! PLEASE DON'T REPOST OLD POSTS! Objective...

    SCREENSHOTS OF CODE ONLY!! PLEASE DON'T POST TEXT!! C++ CODE! PLEASE DON'T REPOST OLD POSTS! Objective To gain experience with the operations involving binary search trees. This data structure as linked list uses dynamic memory allocation to grow as the size of the data set grows. Unlike linked lists, a binary search tree is very fast to insert, delete and search. Project Description When an author produce an index for his or her book, the first step in this process...

  • Q1. Write a recursive function in C++ void printNumberedChars(string str, int i=0) { ... } which...

    Q1. Write a recursive function in C++ void printNumberedChars(string str, int i=0) { ... } which prints each character of the given string str on a new line, with each line numbered 1, 2, 3, …. For example calling the function with printNumberedChars("hello"); should output the following: 1. h 2. e 3. l 4. l 5. o Q2. Write a recursive function in C++ int sumArray(const int* arr, unsigned int size) { ... } which takes an array of integers,...

  • can this code be provided? Project #3 Introduction As you wrap up with JavaScript, let's put...

    can this code be provided? Project #3 Introduction As you wrap up with JavaScript, let's put together everything you've learned to make the classic game "Hangman" in a webpage. If you are unfamiliar with the rules of Hangman, the game works like this: A gallows is drawn on a surface, and Player 1 chooses a word that player 2 must guess. Empty dashes for each letter in the word are drawn next to the gallows (so a 7-letter word means...

  • Thank you!!! 4.1 [2 pts. int rollO Define a function roll() that takes no arguments, and...

    Thank you!!! 4.1 [2 pts. int rollO Define a function roll() that takes no arguments, and returns a random integer from 1 to 6. Before proceeding further, we recommend you write a short main method to test it out and make sure it gives you the right values. Comment this main out after you are satisfied that roll() works. Remenber:texibook Seciion 49 is on randormness, and Chapier 5is on functions. int getKandomNumber return 4 l chosen by fair dice roll....

  • Please Modify TestPart2 to test the correctness and efficiency of FasterDefaultList. Thanks import java.util.List; import java.util.AbstractList;...

    Please Modify TestPart2 to test the correctness and efficiency of FasterDefaultList. Thanks import java.util.List; import java.util.AbstractList; import java.util.Map; import java.util.HashMap; public class DumbDefaultList<T> extends AbstractList<T> { Map<Integer,T> map; public DumbDefaultList() { map = new HashMap<Integer,T>(); } public int size() { return Integer.MAX_VALUE; } public T get(int i) { return map.get(i); } public T set(int i, T x) { return map.put(i, x); } public void add(int i, T x) { Map<Integer, T> map2 = new HashMap<Integer,T>(); for (Integer k : map.keySet())...

  • This project is divided into 3 parts: Part 1. Create a new project and download the arrayList and...

    This project is divided into 3 parts: Part 1. Create a new project and download the arrayList and unorderedArrayList templates that are attached. Create a header file for your unorderedSet template and add it to the project. An implementation file will not be needed since the the new class will be a template. Override the definitions of insertAt, insertEnd, and replaceAt in the unorderedSet template definition. Implement the template member functions so that all they do is verify that the...

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