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
Margaret [11]
3 years ago
10

Define a class called TreeNode containing three data fields: element, left and right. The element is a generic type. Create cons

tructors, setters and getters as appropriate. (20) Define a class called BinaryTree containing two data fields: root and numberElement. Create constructors, setters and getters as appropriate. (10) Define a method balanceCheck to check if a tree is balanced. A tree being balanced means that the balance factor is -1, 0, or 1. (20)
Computers and Technology
1 answer:
m_a_m_a [10]3 years ago
5 0

Answer:

See explaination

Explanation:

// Class for BinarySearchTreeNode

class TreeNode

{

// To store key value

public int key;

// To point to left child

public TreeNode left;

// To point to right child

public TreeNode right;

// Default constructor to initialize instance variables

public TreeNode(int key)

{

this.key = key;

left = null;

right = null;

key = 0;

}// End of default constructor

}// End of class

// Defines a class to crate binary tree

class BinaryTree

{

// Creates root node

TreeNode root;

int numberElement;

// Default constructor to initialize root

public BinaryTree()

{

this.root = null;

numberElement = 0;

}// End of default constructor

// Method to insert key

public void insert(int key)

{

// Creates a node using parameterized constructor

TreeNode newNode = new TreeNode(key);

numberElement++;

// Checks if root is null then this node is the first node

if(root == null)

{

// root is pointing to new node

root = newNode;

return;

}// End of if condition

// Otherwise at least one node available

// Declares current node points to root

TreeNode currentNode = root;

// Declares parent node assigns null

TreeNode parentNode = null;

// Loops till node inserted

while(true)

{

// Parent node points to current node

parentNode = currentNode;

// Checks if parameter key is less than the current node key

if(key < currentNode.key)

{

// Current node points to current node left

currentNode = currentNode.left;

// Checks if current node is null

if(currentNode == null)

{

// Parent node left points to new node

parentNode.left = newNode;

return;

}// End of inner if condition

}// End of outer if condition

// Otherwise parameter key is greater than the current node key

else

{

// Current node points to current node right

currentNode = currentNode.right;

// Checks if current node is null

if(currentNode == null)

{

// Parent node right points to new node

parentNode.right = newNode;

return;

}// End of inner if condition

}// End of outer if condition

}// End of while

}// End of method

// Method to check tree is balanced or not

private int checkBalance(TreeNode currentNode)

{

// Checks if current node is null then return 0 for balanced

if (currentNode == null)

return 0;

// Recursively calls the method with left child and

// stores the return value as height of left sub tree

int leftSubtreeHeight = checkBalance(currentNode.left);

// Checks if left sub tree height is -1 then return -1

// for not balanced

if (leftSubtreeHeight == -1)

return -1;

// Recursively calls the method with right child and

// stores the return value as height of right sub tree

int rightSubtreeHeight = checkBalance(currentNode.right);

// Checks if right sub tree height is -1 then return -1

// for not balanced

if (rightSubtreeHeight == -1) return -1;

// Checks if left and right sub tree difference is greater than 1

// then return -1 for not balanced

if (Math.abs(leftSubtreeHeight - rightSubtreeHeight) > 1)

return -1;

// Returns the maximum value of left and right subtree plus one

return (Math.max(leftSubtreeHeight, rightSubtreeHeight) + 1);

}// End of method

// Method to calls the check balance method

// returns false for not balanced if check balance method returns -1

// otherwise return true for balanced

public boolean balanceCheck()

{

// Calls the method to check balance

// Returns false for not balanced if method returns -1

if (checkBalance(root) == -1)

return false;

// Otherwise returns true

return true;

}//End of method

// Method for In Order traversal

public void inorder()

{

inorder(root);

}//End of method

// Method for In Order traversal recursively

private void inorder(TreeNode root)

{

// Checks if root is not null

if (root != null)

{

// Recursively calls the method with left child

inorder(root.left);

// Displays current node value

System.out.print(root.key + " ");

// Recursively calls the method with right child

inorder(root.right);

}// End of if condition

}// End of method

}// End of class BinaryTree

// Driver class definition

class BalancedBinaryTreeCheck

{

// main method definition

public static void main(String args[])

{

// Creates an object of class BinaryTree

BinaryTree treeOne = new BinaryTree();

// Calls the method to insert node

treeOne.insert(1);

treeOne.insert(2);

treeOne.insert(3);

treeOne.insert(4);

treeOne.insert(5);

treeOne.insert(8);

// Calls the method to display in order traversal

System.out.print("\n In order traversal of Tree One: ");

treeOne.inorder();

if (treeOne.balanceCheck())

System.out.println("\n Tree One is balanced");

else

System.out.println("\n Tree One is not balanced");

BinaryTree

BinaryTree treeTwo = new BinaryTree();

treeTwo.insert(10);

treeTwo.insert(18);

treeTwo.insert(8);

treeTwo.insert(14);

treeTwo.insert(25);

treeTwo.insert(9);

treeTwo.insert(5);

System.out.print("\n\n In order traversal of Tree Two: ");

treeTwo.inorder();

if (treeTwo.balanceCheck())

System.out.println("\n Tree Two is balanced");

else

System.out.println("\n Tree Two is not balanced");

}// End of main method

}// End of driver class

You might be interested in
2. Write a simple program in C++ to investigate the safety of its enumeration types. Include at least 10 different operations on
erastovalidia [21]

Answer:

Check the explanation

Explanation:

To solve the question above, we will be doing some arithmetic and bit wise operations on enum values. in addition to that, we are asserting the array with INTCOUNT  and also printing the size of array i.e 12 and we are adding BIG_COUNT enum value to 5 and the result is 25.

#include<stdio.h>

enum EnumBits

{

ONE = 1,

TWO = 2,

FOUR = 4,

EIGHT = 8

};

enum Randoms

{

BIG_COUNT = 20,

INTCOUNT = 3

};

int main()

{

// Basic Mathimatical operations

printf("%d\n",(ONE + TWO)); //addition

printf("%d\n",(FOUR - TWO)); //subtraction

printf("%d\n",(TWO * EIGHT)); //multiplication

printf("%d\n",(EIGHT / TWO)); //division

// Some bitwise operations

printf("%d\n",(ONE | TWO)); //bitwise OR

printf("%d\n",(TWO & FOUR)); //bitwise AND

printf("%d\n",(TWO ^ EIGHT)); //bitwise XOR

printf("%d\n",(EIGHT << 1)); //left shift operator

printf("%d\n",(EIGHT >> 1)); //right shift operator

// Initialize an array based upon an enum value

int intArray[INTCOUNT]; // declaring array

// Have a value initialized be initialized to a static value plus

int someVal = 5 + BIG_COUNT; //adding constant and BIG_COUNT variable

printf("%d\n",someVal); //value will be 25

printf("%d",sizeof(intArray)); //value will be 12

return 0;

}

Kindly check the attached image below for the Code and Output Screenshots:

6 0
2 years ago
Importance of software in computer
Juli2301 [7.4K]

Answer:

Computer hardware is virtually useless without computer software. Software is the programs that are needed to accomplish the input, processing, output, storage, and control activities of information systems.

Explanation:

4 0
2 years ago
Read 2 more answers
Because its radio waves can pass through walls or desktops,
jeka57 [31]
Bluetooth i believe, not too sure however but its ur best bet
5 0
2 years ago
What is the output when you run the following program? print(3 + 7) print("2 + 3")
QveST [7]

print(3+7) will output 10, which is an integer.

print("2+3") will output 2+3, which is a string.

6 0
2 years ago
To add a solid green color to the back of an entire slide, navigate to the Design tab and apply a _____.
Dmitrij [34]

Answer:

snap girls decoydoan

Explanation:

3 0
3 years ago
Read 2 more answers
Other questions:
  • Which Command Prompt commands in Windows is used for listing a computer connections to shared resources
    10·1 answer
  • An idea concerning what will happen in the future. (Crossword)
    9·1 answer
  • Enables you to navigate through pieces of information by using links which connect them.
    10·1 answer
  • What is one effective way for employees to keep their skill-sets current
    14·1 answer
  • When discussing the data-modeling building blocks, anything (a person, a place, a thing, or an event) about which data are to be
    15·1 answer
  • Which design principle is the subject of a photo or image? ​
    10·1 answer
  • Gina is driving her car down the street. She has a teddy bear sitting on the back seat. A dog runs in front of Gina's car, so sh
    15·2 answers
  • Several people work with data at Erica’s office. She enters data. One of her coworkers enters new product numbers. Another cowor
    5·1 answer
  • A program that allows employees to choose their starting and ending times, as long as they are at work during a specified core p
    8·1 answer
  • Advantages of python programming language
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!