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
What is a Network It’s a system that is used to link two or more computers and name the different types of networks.?
mariarad [96]
Yes, that's correct. a network is where you connect two+ computers together.
5 0
3 years ago
We cannot imagine a life without the Internet. Imagine that you had to live without being connected to the Internet. Discuss the
sleet_krkn [62]

Answer:

1, I would not be able to contact anyone, or call for help.

2, Stores like Apple would lose their purpose.

3, Life would be boring and dull.

  • i hope that helped at all.

4 0
2 years ago
A computer ____ is a potentially damaging computer program that affects a computer negatively by altering the way the computer w
Leviafan [203]
Virus










-----------------------------------
5 0
3 years ago
What is software piracy
Tju [1.3M]
Software piracy is the illegal copying, distribution, or use of software.
8 0
3 years ago
Read 2 more answers
Write a program whose input is two integers. Output the first integer and subsequent increments of 5 as long as the value is les
adell [148]

Answer:

Following are the code to the given question:

For code 1:

start = int(input())#defining a start variable that takes input from the user end

end = int(input())#defining a end variable that takes input from the user end

if start > end:#use if that checks start value greater than end value

   print("Second integer can't be less than the first.")#print message

else:#defining else block

   while start <= end:#defining a while loop that checks start value less than equal to end value

       print(start, end=' ')#print input value

       start += 5#incrementing the start value by 5

   print()#use print for space

For code 2:

while True:#defining a while loop that runs when its true

   data = input()#defining a data variable that inputs values

   if data == 'Done' or data == 'done' or data == 'd':#defining if block that checks data value

       break#use break keyword

   rev = ''#defining a string variable rev

   for ch in data:#defining a for loop that adds value in string variable  

       rev = ch + rev#adding value in rev variable

   print(rev)#print rev value

Explanation:

In the first code two-variable "first and end" is declared that takes input from the user end. After inputting the value if a block is used that checks start value greater than end value and use the print method that prints message.

In the else block a while loop is declared that checks start value less than equal to end value and inside the loop it prints input value and increments the start value by 5.

In the second code, a while loop runs when it's true and defines a data variable that inputs values. In the if block is used that checks data value and use break keyword.

In the next step, "rev" as a string variable is declared that uses the for loop that adds value in its variable and prints its values.

3 0
3 years ago
Other questions:
  • What could prevent earmuffs from providing your ears good protection from noise?
    10·1 answer
  • In your own words, describe the structure and function of both the stack and queue data structure and discuss how they are diffe
    6·1 answer
  • What is a benifit of having a client/server network?
    6·1 answer
  • How can you achieve an effect like that shown in the image?
    7·2 answers
  • How many arguments are required for the sum function?
    15·1 answer
  • Develop a program that will maintain an ordered linked list of positive whole numbers. Your program will provide for the followi
    13·1 answer
  • Which Python expression results in 49?<br><br> 7 * 2<br><br> 7^2<br><br> 7 // 2<br><br> 7 ** 2
    12·2 answers
  • Camera work is at the center of video production. True or False?
    14·1 answer
  • Hmm what should i do with this information
    14·2 answers
  • Which option is an example of an algorithm that is used in daily life?
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!