1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
saul85 [17]
3 years ago
13

A perfect binary tree is a complete binary tree with all levels fully filled. Add a method in the BST class to return true if th

e tree is a perfect binary tree.
Computers and Technology
1 answer:
algol [13]3 years ago
6 0

Answer:

class BST {

static class Node

{

int val;

Node left, right;

}

static boolean checkPerfectBT(Node node, int h, int d)

{

if (node == null)

return true;

 

if (node.left == null && node.right == null)

{

if(h==d+1)

return true;

else

return false;

}

 

if (node.left == null || node.right== null)

return false;

 

return checkPerfectBT(node.left, h, d+1) && checkPerfectBT(node.right, h, d+1);

}

static int height(Node node)

{

int dep = 0;

while (node != null)

{

node = node.right;

dep=dep+1;

}

return dep;

}

static boolean isPerfectBT(Node node)

{

int h = height(node);

return checkPerfectBT(node, h, 0);

}

 

static Node addNode(int x)

{

Node node = new Node();

node.val= x;

node.right = null;

node.left = null;

return node;

}

public static void main(String args[])

{

int i,j,k;

Node node= null;

node = addNode(34);

node.left = addNode(2);

node.right = addNode(322);

 

node.left.left = addNode(21);

node.left.right = addNode(23);

node.right.left = addNode(37);

node.right.right = addNode(54);

 

if (isPerfectBT(node) == true)

System.out.println("This is a Perfect Binary tree");

else

System.out.println("This is Not a perfect Binary Tree");

}

}

Explanation:

  • Calculate the depth of BST by using a while loop until it reaches a null value.
  • In the addNode method, make an object of Node class.
  • In the main method, pass the values to addNode method.
  • Finally display the relevant message to show if it's Perfect or not.
You might be interested in
I need help under standing an assignment, I've contacted my teacher but I want to finish this class already. Here's the directio
Verizon [17]

Answer: So have you done assignment 5? and have you read back the previous one?

8 0
4 years ago
What is the function of a breadcrumb trail website
ziro4ka [17]

Breadcrumb or breadcrumb trail is a graphical control element frequently used as a navigational aid in user interfaces and on web pages. It allows users to keep track and maintain awareness of their locations within programs, documents, or websites.

7 0
3 years ago
Look act the picture
VLD [36.1K]

Answer:

the answer is b

Explanation:

3 0
4 years ago
I'm getting an iphone xr today. what should i do 1st? Any cool fetures? i have a iphone 6 now so its a pretty big upgrade
Sonja [21]

Answer:

I wouldn't get an iphone xr my stepmom had one and the screen started having problems and the apps were glitching.

8 0
3 years ago
Read 2 more answers
A peripheral can be used to tell a computer to complete a specific task. True False
never [62]
I looked up the answer; I believe the answer is true.
7 0
3 years ago
Other questions:
  • The main benefit of encryption of a hard drive is to make part of an ROI report.
    15·1 answer
  • Which of the following is something that scientists often seek by using computer models and simulations?
    8·2 answers
  • Create a new conditional format that applies yellow fill (fourth color under Standard Colors) and bold font to values that conta
    14·1 answer
  • The four key stages with regards to data visualization workflow. Select one key stage and explain it briefly about that stage.St
    10·1 answer
  • Which note-taking method quickly captures and organizes information?
    9·2 answers
  • I have all the points to level up to virtuoso, but it hasn't yet does anyone know when it will?
    9·1 answer
  • Convert 198 / 61 to ratio​
    9·1 answer
  • Which of the following techniques is a direct benefit of using Design Patterns? Please choose all that apply Design patterns hel
    12·1 answer
  • The =COUNT function calculates what value?
    15·2 answers
  • Future Cell Tech, a company that produces cell phones, is always on top of the newest technology and is the leader in cell phone
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!