Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

A Easy Java Question Implement the isBST() and isBalanced() methods in the BinaryTree class. import java.util.List; public class BinaryTree { private BinaryTree left; private BinaryTree

A Easy Java Question

Implement the isBST() and isBalanced() methods in the BinaryTree class.

import java.util.List;

public class BinaryTree> { private BinaryTree left; private BinaryTree right; private T data; private int size;

public BinaryTree(T data) { this.data = data; }

public T getData() { return data; }

public void setLeft(BinaryTree left) { this.left = left; }

public void setRight(BinaryTree right) { this.right = right; }

public BinaryTree getLeft() { return this.left; }

public BinaryTree getRight() { return this.right; }

/* @description This method recursively checks if the binary tree is a BST. * The data implements the Comparable interface, so you can compare the * data in the nodes. For example, to compare the data in the left child * of the current node to this node, use the expression * left.data.compareTo(this.data) * Which returns < 0 if the left.data precedes this.data * returns 0 if left.data == this.data * returns > 0 if left.data lexically follows this.data * @return true if and only if the tree is a BST */ public boolean isBST() { // WRITE CODE HERE }

/* @description This method recursively checks if the binary tree is balanced. * A binary tree is balanced if the left and right subtrees are balanced and * if the difference in sizes of the left and right subtrees is less than or * equal to 1. * Note: You may assume that getSize() was called prior to this method and that * each node in the tree stores the size of the subtree of which it is the root. * @return true if and only if the tree is balanced */ public boolean isBalanced() { // WRITE CODE HERE }

/* @description This method recursively computes the size of the binary tree * @return an integer >= 1 equal to the number of nodes in the binary tree * rooted at this node. */ public int getSize() { size = 1; if (left != null) { size += left.getSize(); } if (right != null) { size += right.getSize(); } return size; }

/* @description This method recursively computes the height of the binary tree * @return an integer >= 0 equal to the height of the binary tree * rooted at this node. */ public int getHeight() { int height = 0; if (left != null) { height = left.getHeight(); } if (right != null) { height = Math.max(height, right.getHeight()); } return height; }

/* @description This method recursively appends all data stored in this * binary tree using an inorder traversal * @param a List where all items are to be appeded during an inorder * traversal. */ public void toList(List list) { // WRITE CODE HERE if (left != null) { left.toList(list); } list.add(data); if (right != null) { right.toList(list); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

JDBC Database Programming With J2ee

Authors: Art Taylor

1st Edition

0130453234, 978-0130453235

More Books

Students also viewed these Databases questions

Question

1. How can evolutionary theory explain prosocial behaviour?

Answered: 1 week ago

Question

What are the functions of top management?

Answered: 1 week ago

Question

Bring out the limitations of planning.

Answered: 1 week ago

Question

Why should a business be socially responsible?

Answered: 1 week ago

Question

Discuss the general principles of management given by Henri Fayol

Answered: 1 week ago