Question: Complete the code for the four problems that follow. Leave everything as a public boolean. Possibly relevant code is included here: public int size ()

Complete the code for the four problems that follow. Leave everything as a public boolean. Possibly relevant code is included here:

public int size () {

return size (root, 0);

}

private static int size (Node x, int sz) {

if (x != null) {

sz = sz + 1;

sz = size (x.left, sz);

sz = size (x.right, sz);

}

return sz;

}

// the height of the tree

public int height() {

return height(this.root);

}

private int height(Node current) {

if (current == null) {

return -1;

}

else {

return 1+ Math.max(height(current.left), height(current.right));

}

}

THINGS NEEDING SOLVING STARTS HERE:

#1

// tree is perfect if for every node, size of left == size of right

// hint: in the helper, return -1 if the tree is not perfect, otherwise return the size

public boolean isPerfectlyBalancedS() {

// TODO: complete code

return false;

}

#2

// tree is perfect if for every node, height of left == height of right

// hint: in the helper, return -2 if the tree is not perfect, otherwise return the height

public boolean isPerfectlyBalancedH() {

// TODO: complete code

return false;

}

#3

// tree is odd-perfect if for every node, #odd descendant on left == # odd descendants on right

// A node is odd if it has an odd key

// hint: in the helper, return -1 if the tree is not odd-perfect, otherwise return the odd size

public boolean isOddBalanced() {

// TODO: complete code

return false;

}

#4

// tree is semi-perfect if every node is semi-perfect

// A node with 0 children is semi-perfect.

// A node with 1 child is NOT semi-perfect.

// A node with 2 children is semi-perfect if (size-of-larger-child <= size-of-smaller-child * 3)

// hint: in the helper, return -1 if the tree is not semi-perfect, otherwise return the size

public boolean isSemiBalanced() {

// TODO: complete code

return false;

}

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!