Question

In: Computer Science

I need the following C code converted to java or C++ #include <stdio.h> #include <stdlib.h> typedef...

I need the following C code converted to java or C++

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

typedef struct node {
struct node *left;
struct node *right;
long data;

long leftSize;
} node;

void btreeInsert(node *new, node **rootptr) {
node *parent = NULL, *cursor;
/* Find parent */
cursor = *rootptr;
while (cursor != NULL) {
parent = cursor;
if (new->data < cursor->data) {
cursor->leftSize += 1;
cursor = cursor->left;
} else {
cursor = cursor->right;
}
}
/* Insert node below parent */
if (parent == NULL) {
*rootptr = new;
} else if (new->data < parent->data) {
parent->left = new;
} else {
parent->right = new;
}
}

node *readTree(FILE *dataFile) {
node *root = NULL, *new;
long element;
while (fscanf(dataFile, "%ld\n", &element) == 1) {
new = malloc(sizeof(node));
new->data = element;
new->right = NULL;
new->left = NULL;
new->leftSize = 0;
btreeInsert(new, &root);
}
return root;
}

long kthOrder(int k, node *root) {
node *current;
current = root;
while (k != current->leftSize + 1) {
if (k <= current->leftSize) {
current = current->left;
} else {
k -= current->leftSize + 1;
current = current->right;
}
}
return current->data;
}

void reportKthOrderStats(FILE *positionFile, node *root) {
int position;
while (fscanf(positionFile, "%i\n", &position) == 1) {
printf("%ld\n", kthOrder(position, root));
}
}

int main(int argc, const char * argv[]) {
const char *data, *range;
FILE *dataFile, *rangeFile;
node *root;

data = argv[1];
range = argv[2];
dataFile = fopen(data, "r");
rangeFile = fopen(range, "r");

root = readTree(dataFile);
reportKthOrderStats(rangeFile, root);

return (0);
}

Solutions

Expert Solution

#include<iostream.h>

#include<bits/stdc++.h>

using namespace std;

struct node{

struct node *left;

struct node *right;

long data;

long leftSize;

}node;

void btreeInsert(node *new,node**rootptr)

{

node *parent=NULL*cursor;

cursor=*rootptr;

while(cursor!=NULL)

{

parent=cursor;

if (new->data < cursor->data) {
cursor->leftSize += 1;
cursor = cursor->left;
} else {
cursor = cursor->right;
}
}
/* Insert node below parent */
if (parent == NULL) {
*rootptr = new;
} else if (new->data < parent->data) {
parent->left = new;
} else {
parent->right = new;
}
}

node *readTree(FILE *dataFile) {
node *root = NULL, *new;
long element;

while(cout<<"datafile"<<element==1)

{

new = malloc(sizeof(node));
new->data = element;
new->right = NULL;
new->left = NULL;
new->leftSize = 0;
btreeInsert(new, &root);

}

return root;

}

long kthOrder(int k, node *root) {
node *current;
current = root;
while (k != current->leftSize + 1) {
if (k <= current->leftSize) {
current = current->left;
} else {
k -= current->leftSize + 1;
current = current->right;
}
}
return current->data;
}

void reportKthOrderStats(FILE *positionFile, node *root) {
int position;
while ((cout<<"positionFile"<<position)==1) {
cout<<kthOrder(position,root);
}
}

int main(int argc, const char * argv[]) {
const char *data, *range;
FILE *dataFile, *rangeFile;
node *root;

data = argv[1];
range = argv[2];
dataFile = fopen(data, "r");
rangeFile = fopen(range, "r");

root = readTree(dataFile);
reportKthOrderStats(rangeFile, root);

return 0;


Related Solutions

I NEED THIS CODE FOR C++ USING MONITORS PLEASE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include...
I NEED THIS CODE FOR C++ USING MONITORS PLEASE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h> #define THREADS 10 // Number of Thread //bridge declared with array of character and integer value void Bridge(char array[], int value); // Global Variable int North = 1; //For North Number int South = 1; //For South Number pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; // Setting Up MUTEX for lock //Thread for North farmer void NorthFarmer(){ pthread_mutex_lock(&mutex1); char array[5] = "North"; // North printf("%s Tunbridge...
#include<stdlib.h> #include<stdio.h> typedef struct node {    void* dataPtr;    struct node* next; } QUEUE_NODE; typedef...
#include<stdlib.h> #include<stdio.h> typedef struct node {    void* dataPtr;    struct node* next; } QUEUE_NODE; typedef struct {    QUEUE_NODE* front;    QUEUE_NODE* rear;    int count; } QUEUE; //Prototype Declarations QUEUE* createQueue(void); QUEUE* destroyQueue(QUEUE* queue); bool dequeue(QUEUE* queue, void** itemPtr); bool enqueue(QUEUE* queue, void* itemPtr); bool queueFront(QUEUE* queue, void** itemPtr); bool queueRear(QUEUE* queue, void** itemPtr); int queueCount(QUEUE* queue); bool emptyQueue(QUEUE* queue); bool fullQueue(QUEUE* queue); /*================= createQueue ================ Allocates memory for a queue head node from dynamic memory and returns...
Can you translate this C code into MIPS assembly? #include <stdio.h> #include <math.h> #include <stdlib.h> double...
Can you translate this C code into MIPS assembly? #include <stdio.h> #include <math.h> #include <stdlib.h> double fact (double); void main () { int angle_in_D; double term, angle_in_R; float sine = 0; unsigned int i = 1; double sign = 1; int n = 1000; printf ("Please enter an angle (Unit: Degree): "); scanf ("%d", &angle_in_D); angle_in_R = angle_in_D * M_PI / 180.0; do { term = pow(-1,(i-1)) * pow (angle_in_R, (2*i - 1)) / fact (2*i - 1); sine =...
how would i change the for loops to while loops in the code below #include<stdio.h> #include<stdlib.h>...
how would i change the for loops to while loops in the code below #include<stdio.h> #include<stdlib.h> int main() { int seed; // Taking seed value as input from the user printf("Enter a seed value (0 to quit): \n"); scanf("%d", &seed); // Running the loop until user enters 0 to quit // count array will count frequency of 0 , 1 , 2 ,3 int count[4]; for (int i = 0; i < 4; i++) count[i] = 0; while (seed !=...
Use C language , pointer limit use //#include <stdio.h> //#include <stdlib.h> //#include <time.h> For example, I...
Use C language , pointer limit use //#include <stdio.h> //#include <stdlib.h> //#include <time.h> For example, I have random array [4,2,7,1,9,8,0]. Sort the array's index but cannot change the position of item in the array, if you copy the array to a new array, you also cannot change the item's position in array. The index now is[0,1,2,3,4,5,6] The output should be[6,3,1,0,2,5,4]
Please implement the 5 questions in source code: #include <stdio.h> #include <stdlib.h> #include <math.h> int main(...
Please implement the 5 questions in source code: #include <stdio.h> #include <stdlib.h> #include <math.h> int main( int argc, char* argv[] ) { // Size of vectors int n = 10000; // Input vectors double *restrict a; double *restrict b; // Output vector double *restrict c; // Size, in bytes, of each vector size_t bytes = n*sizeof(double); /* Q1: Allocate memory for vector a (10 points)*/ /* Q2: Allocate memory for vector b (10 points)*/ /* Q3: Allocate memory for vector...
Includes you will need: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <signal.h> #include <sys/wait.h> Create a...
Includes you will need: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <signal.h> #include <sys/wait.h> Create a .c file that does the following: 1. Use fork() to create a 2nd process, using the if checks we’ve seen to determine if you’re the child or the parent. Print a message from each process saying who is who. (APUE chapter 1/7) • Note: The parent should save the pid_t of the child (returned from fork()) for later. 2. Have the child register a...
Please debug the code and answer the questions: #include <stdio.h> typedef struct node { int value;...
Please debug the code and answer the questions: #include <stdio.h> typedef struct node { int value; struct node *next; } node; int ll_has_cycle(node *first) { node * head = first; while (head->next) { head = head->next; if (head == first) return 1; } return 0; } void test_ll_has_cycle(void) { int i,j; node nodes[5]; for(i=0; i < sizeof(nodes)/sizeof(node); i++) { nodes[i].next = NULL; nodes[i].value = i; } nodes[0].next = &nodes[1]; nodes[1].next = &nodes[2]; nodes[2].next = &nodes[1]; printf("Checking first list for cycles....
please fix code #include <stdio.h> #include <stdlib.h> #include <string.h> // function declarations int getValidJerseyNumber(); int getValidRating();...
please fix code #include <stdio.h> #include <stdlib.h> #include <string.h> // function declarations int getValidJerseyNumber(); int getValidRating(); int main() { // declaring variables int size = 5; int jerseyNo[size]; int rating[size]; int i = 0, jno, rate; char option; /* Getting the inputs entered by the user * and populate the values into arrays */ for (i = 0; i < size; i++) { printf("Enter player %d's jersey number:", i + 1); jerseyNo[i] = getValidJerseyNumber(); printf("Enter player %d's rating:\n", i +...
(12) Explain what will be output of the following program? #include <stdio.h> #include <stdlib.h> #include <pthread.h>...
(12) Explain what will be output of the following program? #include <stdio.h> #include <stdlib.h> #include <pthread.h> #define NUM_THREADS 3 /* create thread argument struct for thr_func() */ typedef struct _thread_data_t {   int tid;   double stuff; } thread_data_t; /* thread function */ void *thr_func(void *arg) {   thread_data_t *data = (thread_data_t *)arg;   printf("hello from thr_func, thread id: %d\n", data->tid);   pthread_exit(NULL); } int main(int argc, char **argv) {   pthread_t thr[NUM_THREADS];   int i, rc;   thread_data_t thr_data[NUM_THREADS];   /* create threads */   for (i = 0;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT