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...
I have the following code #include <stdio.h> #include<string.h> #define BUFLEN 128 typedef struct { int numPhrases;...
I have the following code #include <stdio.h> #include<string.h> #define BUFLEN 128 typedef struct { int numPhrases; }SyncInfo; char buffer[BUFLEN] ; char *phrases[] = {"educated", "educated cat", "educated lion", "serious person" , "serious panda","curious student","curious art student", "obnoxious web developer"}; char localBuffer[BUFLEN]; int allVowelsPresent; void *checker(void *param) { int a=0, e=0, i=0, o = 0, u= 0 ; int* n = (int*)param; // typecasting a void* to int* //printf("%d\n",*n); for (int q=0; q< (*n); ++q) { // dereferencing to get the...
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...
#include<stdio.h> #include<stdlib.h> struct listNode{ int data; struct listNode *nextptr; }; typedef struct listNode node; void insert(node*);...
#include<stdio.h> #include<stdlib.h> struct listNode{ int data; struct listNode *nextptr; }; typedef struct listNode node; void insert(node*); void showList(node*); void printListBackwards(node *); int main(void) { node *list1; printf("\n Create a sorted list.."); printf("\n Enter values for the first list (-999 to end):"); list1=(node*)malloc(sizeof(node*)); //Allocate memory for the list node insert(list1); //insert values by calling the function insert showList(list1); //display values entered by user printf("\n After recursively reversing the list is :\n"); printListBackwards(list1); //print the values in reverse order using the function...
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....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT