Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A. Implement the code below, document, test and submit a full report.C++ B. Desc

ID: 3889559 • Letter: A

Question

A. Implement the code below, document, test and submit a full report.C++

B. Describe how you could implement this using an array instead of the node class.C++

C .Write a program to store a tree as an array. In your test program, be sure to print the array values, and to print the values in tree­­­ “format”.Implement (Write a function to add an element to an existing tree.)C++

//Josephus Problem
#include <iostream>
#include <cstdlib>

using namespace std;

class node
{
public:

int item; node* next;
    node(int x, node* t)
      { item = x; next = t; }
};

typedef node *link;

int main(int argc, char *argv[])
{ int i, N = atoi(argv[1]), M = atoi(argv[2]);
    link t = new node(1, 0); t->next = t;
    link x = t;
    for (i = 2; i <= N; i++)
      x = (x->next = new node(i, t));
    while (x != x->next)
      {
        for (i = 1; i < M; i++) x = x->next;
        x->next = x->next->next;
      }
    cout << x->item << endl;
}

Explanation / Answer

the code has done

#include <iostream>
#include <cstdlib>

using namespace std;

class node
{
public:

int item; node* next;
node(int x, node* t)
{ item = x; next = t; }
};

typedef node *link;

int main(int argc, char *argv[])
{ int i, N = atoi(argv[1]), M = atoi(argv[2]);
link t = new node(1, 0); t->next = t;
link x = t;
for (i = 2; i <= N; i++)
x = (x->next = new node(i, t));
while (x != x->next)
{
for (i = 1; i < M; i++) x = x->next;
x->next = x->next->next;
}
cout << x->item << endl;
}


Binary tree

#include<stdio.h>

typedef struct node
{
struct node*left;
struct node*right;
char data;
}node;

node* insert(char c[],int n)
{ node*tree=NULL;
if(c[n]!='')
{
tree=(node*)malloc(sizeof(node));
tree->left=insert(c,2*n+1);
tree->data=c[n];
tree->right=insert(c,2*n+2);
}
return tree;
}
//traverse the tree in inorder
void inorder(node*tree)
{
if(tree!=NULL)
{
inorder(tree->left);
printf("%c ",tree->data);
inorder(tree->right);
}
}

void main()
{
node*tree=NULL;
char c[]={'A','B','C','D','E','F','','G','','','','','','','','','','','','',''};
tree=insert(c,0);
inorder(tree);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote