Question: In java, populate a tree via a text file ( input . txt ) Make sure that after every insert, the tree is balanced. At
In java, populate a tree via a text file inputtxt Make sure that after every insert, the tree is balanced. At the end, display the tree in level format. Make sure to include the height and the balance factor of every node in your output. Redirect the display to an output file outputtxt
Hint:
I will not accept any other algorithm
This is not a recursive algorithm
node rebalancenode node
nodeheight maxheightnodeleft heightnoderight;
int balance getBalancenode; nodeleft noderight
do rotations as necessary
If Left heavy outside : return rightRotatenode;
If right heavy outside: return leftRotatenode;
If left heavy inside: left rotation first, right rotation nd return top node
nodeleft leftRotatenodeleft;
return rightRotatenode;
if right heavy inside: right rotation first, left rotation nd return top node
noderight rightRotatenoderight;
return leftRotatenode;
if no rotation, return node
nontail recursive algorithm because of rebalance
node insertnode node, int key
recursive Code for inserting a node
When insert happens set height to for the node
if node NULL
returnnewNodekey;
if key nodekey
nodeleft insertnodeleft, key;
else
noderight insertnoderight, key;
noderebalancenode; update heights and rebalance
return node;
node leftRotatenode x
struct node yxright;
add more code to rotate to the left, update heights for x and y
return y
node rightRotatenode x
struct node yxleft;
add more code to rotate to the right, update heights for x and y
return y
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
