In: Computer Science
AVL Group assignment
POST ANSWER IN JAVA PLS
Populate a tree via a text file (input.txt) 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 (output.txt)
Hint:
//I will not accept any other algorithm
//This is not a recursive algorithm
node * rebalance(node *node){
node->height = max(height(node->left), height(node->right)) + 1;
int balance = getBalance(node); //node->left - node->right
/*do rotations as necessary
If Left heavy outside : return rightRotate(node);
If right heavy outside: return leftRotate(node);
If left heavy inside: left rotation first, right rotation 2nd, return top node
node->left = leftRotate(node->left);
return rightRotate(node);
if right heavy inside: right rotation first, left rotation 2nd, return top node
node->right = rightRotate(node->right);
return leftRotate(node);
if no rotation, return node
*/
}
//non-tail recursive algorithm because of rebalance
node* insert(node* node, int key)
{
//recursive Code for inserting a node
//When insert happens set height to 0 for the node
if (node == NULL)
return(newNode(key));
if (key < node->key)
node->left = insert(node->left, key);
else
node->right = insert(node->right, key);
node=rebalance(node); //update heights and rebalance
return node;
}
node *leftRotate(node *x){
struct node *y=x->right;
//add more code to rotate to the left, update heights for x and y
//return y
}
node *rightRotate(node *x){
struct node *y=x->left;
//add more code to rotate to the right, update heights for x and y
//return y
}
POST ANSWER IN JAVA PLS
Assuming that the data is present in a text file "data.txt".
class SBBSTNodes
{
SBBSTNodes left, right;
int data;
int height;
public SBBSTNodes()
{
left = null;
right = null;
data = 0;
height = 0;
}
public SBBSTNodes(int n)
{
left = null;
right = null;
data = n;
height = 0;
}
}
class SelfBalancingBinarySearchTrees
{
private SBBSTNodes root;
public SelfBalancingBinarySearchTrees()
{
root = null;
}
public boolean isEmpty()
{
return root == null;
}
public void clear()
{
root = null;
}
static void printLevelOrder(SBBSTNodes root)
{
int h = height(root);
int i;
for (i=1; i<=h; i++)
{
printGivenLevel(root, i,i);
System.out.println();
}
}
/* Print nodes at a given level */
void printGivenLevel(SBBSTNodes root, int level, int height)
{
if (root == null)
return;
if (level == 1){
System.out.println(root.data);
System.out.println("height is"+ height);}
else if (level > 1)
{
printGivenLevel(root.left, level-1,height);
printGivenLevel(root.right, level-1,height);
}
}
public level_order_traversal(){
printlevelorder(root);
}
public void insert(int data)
{
root = insert(data, root);
}
private int height(SBBSTNodes t)
{
return t == null ? -1 : t.height;
}
private int max(int lhs, int rhs)
{
return lhs > rhs ? lhs : rhs;
}
private SBBSTNodes insert(int x, SBBSTNodes t)
{
if (t == null)
t = new SBBSTNodes(x);
else if (x < t.data)
{
t.left = insert(x, t.left);
if (height(t.left) - height(t.right) == 2)
if (x < t.left.data)
t = rotateWithLeftChild(t);
else
t = doubleWithLeftChild(t);
} else if (x > t.data)
{
t.right = insert(x, t.right);
if (height(t.right) - height(t.left) == 2)
if (x > t.right.data)
t = rotateWithRightChild(t);
else
t = doubleWithRightChild(t);
} else
;
t.height = max(height(t.left), height(t.right)) + 1;
return t;
}
private SBBSTNodes rotateWithLeftChild(SBBSTNodes k2)
{
SBBSTNodes k1 = k2.left;
k2.left = k1.right;
k1.right = k2;
k2.height = max(height(k2.left), height(k2.right)) + 1;
k1.height = max(height(k1.left), k2.height) + 1;
return k1;
}
private SBBSTNodes rotateWithRightChild(SBBSTNodes k1)
{
SBBSTNodes k2 = k1.right;
k1.right = k2.left;
k2.left = k1;
k1.height = max(height(k1.left), height(k1.right)) + 1;
k2.height = max(height(k2.right), k1.height) + 1;
return k2;
}
private SBBSTNodes doubleWithLeftChild(SBBSTNodes k3)
{
k3.left = rotateWithRightChild(k3.left);
return rotateWithLeftChild(k3);
}
private SBBSTNodes doubleWithRightChild(SBBSTNodes k1)
{
k1.right = rotateWithLeftChild(k1.right);
return rotateWithRightChild(k1);
}
public int countNodes()
{
return countNodes(root);
}
private int countNodes(SBBSTNodes r)
{
if (r == null)
return 0;
else
{
int l = 1;
l += countNodes(r.left);
l += countNodes(r.right);
return l;
}
}
public boolean search(int val)
{
return search(root, val);
}
private boolean search(SBBSTNodes r, int val)
{
boolean found = false;
while ((r != null) && !found)
{
int rval = r.data;
if (val < rval)
r = r.left;
else if (val > rval)
r = r.right;
else
{
found = true;
break;
}
found = search(r, val);
}
return found;
}
public class Balanced_B_Tree
{
public static void main(String[] args)
{Scanner scanner = new Scanner(new File("data.txt"))
SelfBalancingBinarySearchTrees sbbst = new SelfBalancingBinarySearchTrees();
System.out.println("Self Balancing Tree\n"); while(scanner.hasNextInt()){
sbbst.insert(scan.nextInt());
}
scan.close(); sbbst.level_order_traversal();
}
}