5. (14 points) C onsider the method insertât that allows you to insert an entry
ID: 3752748 • Letter: 5
Question
5. (14 points) C onsider the method insertât that allows you to insert an entry x into a Eytree by choosing its location as an index in the string created by the in-order trave s, inserting an entry at pos would result in the entry being the pos+1 entry were to insert x at position 0 then the element returned by the iterator. For example, if you would be the first element in the in-order traversal (i.e., the first one returned by the iterator). Write a recursive body for the static method below using only BinaryTree kerne summary for the BinaryTreeKerne1 interface (including the definition of IN ORDER, in case you do not remember it) is in the handout. : Inserts x in t so that it is at position pos in IN ORDERt) · ealiasos reference x eupdates t 8requires 0 t, int pos, T x) ( assert 0Explanation / Answer
here is your answer : --------->>>>>>>
private static <T> void insertAt(BinaryTree<T> t,int pos,T x){
assert 0 <= pos: "Violation of: 0 <= pos";
assert pos <= t.size(): "Violation of: pos <= |t|";
BinaryTree<T> left = t.newInstance();
BinaryTree<T> right = t.newInstance();
T root = t.disassemble(left,right);
if(pos == t.size()){
t.assemble(x,left,right);
return;
}else if(pos <= left.size()){
insertAt(left,pos,x);
}else if(pos > t.size()){
insertAt(right,pos-t.size(),x);
}
t.assemble(root,left,right);
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.