C++. Test if a Tree is Balanced Use the given Tree class and implement a function to test if the binary tree is balanced or not. An empty tree is height-balanced. A non-empty binary tree T is balanced if:
1) Left subtree of T is balanced
2) Right subtree of T is balanced
3) The difference between heights of left subtree and right subtree is not more than 1.
Please MAKE SURE TO CLEARLY SHOW THE CODING. AND YOUR OUTPUT.
#include <iostream>
using namespace std;
template<class ItemType>
class Tree {
private:
Tree<ItemType> * left;
Tree<ItemType> * right;
ItemType data;
public:
Tree(ItemType _data){left = NULL; right = NULL; data =
_data;}
void insert(ItemType _data){
if( _data > data){
if(right == NULL){
right = new Tree(_data);
}else{
right->insert(_data);
}
}else{
if(left == NULL){
left = new Tree(_data);
}else{
left->insert(_data);
}
}
}
bool isBalanced(){
// Implement this Function
}
};
int main() {
return 0;
}
#include <iostream>
using namespace std;
template<class ItemType>
class Tree {
private:
Tree<ItemType> * left;
Tree<ItemType> * right;
ItemType data;
public:
Tree(ItemType _data){
left = NULL; right = NULL; data = _data;
}
void insert(ItemType _data){
if( _data > data){
if(right == NULL){
right = new Tree(_data);
}else{
right->insert(_data);
}
}else{
if(left == NULL){
left = new Tree(_data);
}else{
left->insert(_data);
}
}
}
int getHeight() {
int leftHeight = 0, rightHeight =0;
if(left != NULL) {
leftHeight = left->getHeight();
}
if(right != NULL) {
rightHeight = right->getHeight();
}
if(leftHeight > rightHeight) {
return leftHeight + 1;
} else {
return rightHeight + 1;
}
}
bool isBalanced(){
if(left !=NULL && !left->isBalanced()) {
return false;
}
if(right !=NULL && !right->isBalanced()) {
return false;
}
int leftHeight = 0, rightHeight =0;
if(left != NULL) {
leftHeight = left->getHeight();
}
if(right != NULL) {
rightHeight = right->getHeight();
}
return (abs(leftHeight - rightHeight) <= 1);
}
};
int main() {
return 0;
}
C++. Test if a Tree is Balanced Use the given Tree class and implement a function...
C++. Test if a Tree is Balanced Use the given Tree class and implement a function to test if the binary tree is balanced or not. An empty tree is height-balanced. A non-empty binary tree T is balanced if: 1) Left subtree of T is balanced 2) Right subtree of T is balanced 3) The difference between heights of left subtree and right subtree is not more than 1. #include <iostream> using namespace std; template<class ItemType> class Tree { private:...
Java binary search tree using strings The code below is the function to insert integers into a binary search tree. How can we change this function so that strings can be added into the tree? public void insert(T insertValue) { if ( data.compareTo(insertValue) > 0 ) { // insert new TreeNode if ( leftNode == null ) leftNode = new TreeNode<T>( insertValue ); else // continue traversing left subtree leftNode.insert( insertValue ); } // end if else if ( data.compareTo(insertValue)...
In C++ I need the printRange function, and the main.cpp program. Thanks. (Binary search tree) Write a function printRange that takes as input a binary search tree t and two keys, k1 and k2, which are ordered so that k1 < k2, and print all elements x in the tree such that k1 <= x <= k2. You can add this function in BinarySearchTree.h (click the link) that we used in the lecture and lab 7. public: void printRange(int k1,...
3. (Gaddis Exercises 20.4) Tree Height Write a recursive member function for the BinaryTree class that returns the height of the tree. The height of the tree is the number of levels it contains. Demonstrate the function in a driver program. CPP FILE CODE: #include "BinaryTree.h" #include <iostream> using namespace std; BinaryTree::BinaryTree() { root = NULL; } BinaryTree::~BinaryTree() { destroy(root); } bool BinaryTree::search(int data) { return search(data, root); } void BinaryTree::insert(int data) { insert(data, root); } void BinaryTree::traverseInOrder() { traverseInOrder(root);...
Consider the class specifications for the Binary Tree class and Binary Search Tree class in the attached files // BinaryTree.h #include <iostream> using namespace std; //Definition of the Node template <class elemType> struct TreeNode { elemType data; TreeNode<elemType> *left; TreeNode<elemType> *right; }; //Definition of class Binary Tree template <class elemType> class BinaryTree { protected: TreeNode<elemType> *root; public: BinaryTree(); BinaryTreel const BinaryTree<elemType>& otherTree); BinaryTree(); bool is Empty() const; virtual boot search(const elemType& searchItem) const = 0; virtual void insert(const elemType& insertItem)...
C++ ONLY This question has 2 parts (a, and b). The partial definition of tree with up to 3-nodes for each node is given below. This is not a binary search tree! class Tree3 { public : Tree3() {} private : class Node { public : int Val ; Node * Left ; Node * Middle ; Node * Right ; }; }; Node * Root { nullptr }; a) A Node is considered balanced if the sum of the...
In C++ and use functions that are asked for, thanks! Implement the BinarySearchTree ADT in a file BinarySearchTree.h exactly as shown below. // BinarySearchTree.h // after Mark A. Weiss, Chapter 4, Dr. Kerstin Voigt #ifndef BINARY_SEARCH_TREE_H #define BINARY_SEARCH_TREE_H #include <cassert> #include <iostream> using namespace std; template <typename C> class BinarySearchTree { public: BinarySearchTree( ) : root{ nullptr } { } ~BinarySearchTree( ) { makeEmpty(); } const C & findMin( ) const { assert(!isEmpty()); return findMin( root )->element; } const C...
Consider the class specifications for the Binary Tree class and BinarySearch Tree class below: // Binary Tree.h #include <iostream> using namespace std; //Definition of the Node template <class elemType struct TreeNode { elemType data; TreeNode<elemType> *left; TreeNode<elemType *right; }; //Definition of class Binary Tree template <class elemType> class Binary Tree { protected: TreeNode<elemType> *root; public: BinaryTree(); BinaryTreel const BinaryTree<elemType>& otherTree); -Binary Tree(): bool is Empty() const; virtual bool search(const elemTypes searchItem) const = 0; virtual void insert(const elemTypek insertItem) =...
Use Haskell for the above problems. Unless stated
otherwise do not use library functions that are not in the Haskell
standard prelude.
5. Consider the following definition for a binary tree. data Tree a Empty l Leaf a l Node a (Tree a) (Tree a) A binary tree is balanced if, at every node, the difference between the height the left and right subtree is at most one Height is defined as follows: height of an Empty node is 0...
I need to do a tree sort method but the treesortMethod is not working /****Binarytree class****\ package Tree; public class BinaryTree { private TreeNode root; // head of the list //constructor - create an empty binary tree public BinaryTree() { root = null; } //isEmpty() - return true if tree is empty, false otherwise public boolean isEmpty() { return (root == null); } //deleteTree() - remove all items from tree public void deleteList() { root =...