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

I need help with 2, 3, and 4. The questions with the red box around them, not qu

ID: 3834182 • Letter: I

Question

I need help with 2, 3, and 4. The questions with the red box around them, not question 1. Programming language is Java. This is a worksheet i'm working on. These questions are entirely possible to complete without outside information. I just need the blanks filled in. Thanks in advance. :)

1- What nodes would P and C be referencing after the method findNode was invoked to locate: a The node with key 4 in the tree on the right? b) The node with key 5 in the tree on the right? To c) The root node in the tree on the right? After returning from findNode(key, p, c), give the code to (note: don't consider the TreeNodeWrapper class) a Fetch a deep copy of the node with key 4. b) Insert the node with key field 5. c) Delete the node with key field 6. d) Delete the node with key field 8.

Explanation / Answer

2.a for deep copy of 4 is new Node(4,2,6)

value is 4 , p is 2, c is 6

2.b. 5 is inserted as left child of 6. 5 is less than root and more than root.left and less than root.left.right.code is below :

}

2.c and d

4. {2,4,6,7,8,9} inorder representation of tree in array

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<current.data){ current = current.left; if(current==null){ parent.left = newNode; return; } }else{ current = current.right; if(current==null){ parent.right = newNode; return; } } }

}

2.c and d

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 i am here that means we have found the node //Case 1: if node to be deleted has no children if(current.left==null && current.right==null){ if(current==root){ root = null; } if(isLeftChild ==true){ parent.left = null; }else{ parent.right = null; } } //Case 2 : if node to be deleted has only one child 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){ //now we have found the minimum element in the right sub tree 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; }

4. {2,4,6,7,8,9} inorder representation of tree in array

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote