Convert into JAVA #include \"BinarySearchTree.h\" int maxDepth(TreeNode *x) { if
ID: 3581334 • Letter: C
Question
Convert into JAVA
#include "BinarySearchTree.h"
int maxDepth(TreeNode *x) {
if (x == NULL) {
return 0;
} else {
int lDepth = maxDepth(x->left);
int rDepth = maxDepth(x->right);
if (lDepth > rDepth) {
return (lDepth + 1);
} else {
return (rDepth + 1);
}
}
}
void inorderTreeWalk(TreeNode *x, void ( *action )( void *) ) {
if (x == NULL)
return;
inorderTreeWalk(x->left, action);
action(x->key);
inorderTreeWalk(x->right, action);
}
void treeInsert(Tree* t, TreeNode *z, int ( *compare )( void *, void * )) {
struct TreeNode *y = NULL;
struct TreeNode *x = t->root;
while (x != NULL) {
y = x;
if (compare(z->key,x->key) < 0) {
x = x->left;
} else {
x = x->right;
}
}
z->parent = y;
if (y == NULL) {
t->root = z;
} else if (compare(z->key, y->key) < 0) {
y->left = z;
} else {
y->right = z;
}
}
TreeNode* treeMinimum(TreeNode *x) {
while (x->left != NULL) {
x = x->left;
}
return x;
}
TreeNode* treeMaximum(TreeNode *x) {
while (x->right != NULL) {
x = x->right;
}
return x;
}
TreeNode* treeSearch(TreeNode *x, void *k, int ( *compare )( void *, void *)) {
if (x == NULL || compare(k,x->key) == 0) {
return x;
}
if (compare(k,x->key) < 0) {
return treeSearch(x->left, k, compare);
} else {
return treeSearch(x->right, k, compare);
}
}
void transplant(Tree *t, TreeNode *u, TreeNode *v) {
if (u->parent == NULL) {
t->root = v;
} else if (u == u->parent->left) {
u->parent->left = v;
} else {
u->parent->right = v;
}
if (v != NULL) {
v->parent = u->parent;
}
}
void treeDelete(Tree* t, TreeNode* z) {
if (z->left == NULL) {
transplant(t, z, z->right);
} else if (z->right == NULL) {
transplant(t, z, z->left);
} else {
TreeNode* y = treeMinimum(z->right);
if (y->parent != z) {
transplant(t, y, y->right);
y->right = z->right;
y->right->parent = y;
}
transplant(t, z, y);
y->left = z->left;
y->left->parent = y;
}
}