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
Are there different types of machine language?
7nadin3 [17]
No machine language have no any type
7 0
3 years ago
Read 2 more answers
HELP ME PLEASE ASAP!!!
Ratling [72]

The answer should be: True

5 0
3 years ago
Read 2 more answers
How do we distinguish between remote and local information sources? give an example of each source? i'll give brainliest answer
kirill [66]

A local server means that you have a server setup on your current machine. A remote server is ran on a different machine

7 0
2 years ago
When creating an access list statement, the wildcard mask 0.0.0.0 can be substituted by what word?
weqwewe [10]

Answer:

host (?)

Explanation:

6 0
3 years ago
I need some help with this project. The picture of the assignment is above
Mekhanik [1.2K]

Answer:

sorry

Explanation:

you need to do this on your own Wahhabi

but if you sub to my you-tube channel at chris got ha,x i will help you out

6 0
2 years ago
Other questions:
  • Which of the following was not important in the development of the internet?
    10·2 answers
  • How does a router handle a packet destined for a network that is not listed in the routing table?
    14·1 answer
  • Which actions did Sheila have to take to make the changes observed? Check all that apply.
    9·1 answer
  • What is a stored​ procedure?
    8·1 answer
  • Which os the following is NOT true about the proof of work concept?
    8·1 answer
  • Protocols at which layer of the OSI model have the primary function of moving datagrams through an internetwork connected by rou
    7·1 answer
  • The weight of your car will also affect its_____.
    13·2 answers
  • Cuántos tipos de grua existen en el mundo​
    7·1 answer
  • Greedy Algorithm Design
    8·1 answer
  • your manager asks you to set up a secure network connection at a remote site to move over some backups which protocol would you
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!