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

90.2K

Verified Solution

Question

Programming

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

#include
#include

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);
}

Answer & Explanation Solved by verified expert
3.9 Ratings (382 Votes)
include include using namespace std struct node struct node left struct node right long data long leftSize node void btreeInsertnode newnoderootptr node    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students