Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

Explanation / Answer

public class BinarySearchTree { public static Node root; public BinarySearchTree(){ this.root = null; } public boolean insert(int id){ Node current = root; while(current!=null){ if(current.data==id){ return true; }else if(current.data>id){ current = current.left; }else{ current = current.right; } } return false; } public boolean delete(int id){ Node parent = root; Node current = root; boolean isLeftChild = false; while(current.data!=id){ parent = current; if(current.data>id){ isLeftChild = true; current = current.left; }else{ isLeftChild = false; current = current.right; } if(current ==null){ return false; } } if(current.left==null && current.right==null){ if(current==root){ root = null; } if(isLeftChild ==true){ parent.left = null; }else{ parent.right = null; } } else if(current.right==null){ if(current==root){ root = current.left; }else if(isLeftChild){ parent.left = current.left; }else{ parent.right = current.left; } } else if(current.left==null){ if(current==root){ root = current.right; }else if(isLeftChild){ parent.left = current.right; }else{ parent.right = current.right; } }else if(current.left!=null && current.right!=null){ Node successor = getSuccessor(current); if(current==root){ root = successor; }else if(isLeftChild){ parent.left = successor; }else{ parent.right = successor; } successor.left = current.left; } return true; } public Node getSuccessor(Node deleleNode){ Node successsor =null; Node successsorParent =null; Node current = deleleNode.right; while(current!=null){ successsorParent = successsor; successsor = current; current = current.left; } if (successsor!=deleleNode.right){ successsorParent.left = successsor.right; successsor.right = deleleNode.right; } return successsor; } public void insert(int id){ Node newNode = new Node(id); if(root==null){ root = newNode; return; } Node current = root; Node parent = null; while(true){ parent = current; if(id