Question: [JAVA] Write the following brute force technique to balance a tree: Write an inorder traversal of the tree to an array and then use a

[JAVA] Write the following brute force technique to balance a tree: Write an inorder traversal of the tree to an array and then use a recursive method (like binary search) to insert the middle element of the array as the root, and then build balanced left and right substrings.

the inOrder section

public Iterator iteratorInOrder() { ArrayUnorderedList tempList = new ArrayUnorderedList(); inOrder(root, tempList);

return new TreeIterator(tempList.iterator()); }

protected void inOrder(BinaryTreeNode node, ArrayUnorderedList tempList) { if (node != null) { inOrder(node.getLeft(), tempList); tempList.addToRear(node.getElement()); inOrder(node.getRight(), tempList); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!