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

#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...
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 +...
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....
(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;...
C program. librarys that are in use; stdio.h, stdlib.h, time.h, stddef.h, ctype.h, math.h, string.h. Need to...
C program. librarys that are in use; stdio.h, stdlib.h, time.h, stddef.h, ctype.h, math.h, string.h. Need to rewrite the functions linked in the code: "return add_move_lib ( board, col, colour ) ;" "return board_full_lib ( board ) ;" "return display_board_lib ( board ) ; // TASKS // board_full() display_board() add_move() // adds a token of the given value (1 or 2) to the board at the // given column (col between 1 and COLS inclusive) // Returns 0 if successful, -1...
#include <stdio.h> typedef struct Coordinates { int x; int y; } Coordinate; typedef union uCoordinates {...
#include <stdio.h> typedef struct Coordinates { int x; int y; } Coordinate; typedef union uCoordinates { int x; int y; } uCoordinate; // TODO - Populate 4 different Coordinates with any numbers between 1 and 99 for x & y values // using coordinate1, coordinate2, coordinate3, & coordinate4 as Coordinate names // TODO - Print to screen the x & y values of each coordinate // TODO - Replace the following with your name: Ethin Svoboda int main() { //...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT