Binary Tree Template
Write your own version of a class template that will create a binary tree that can hold values of any data type. Demonstrate the class with a driver program. Place your binary tree template in it's own header file, Btree.h. Include methods for the following:
inserting new values into the tree
removing nodes from the tree
searching the tree
returning the number of nodes in the tree
displaying the contents of the tree using preorder
traversal
Your public interface must include at least the following methods:
-- void insert( T )
-- void remove( T )
-- bool search( T )
-- void preprint()
-- int count()
Where T is a generic parameter ( you can name it anything, I just chose T )
Sample Usage:
BTree b;
b.preprint();
cout << b.count();
b.insert('A');
cout << b.search('A');
b.remove('A');
Should be able to write statements like this in a program and have
your class compile and run.
The count method must use recursion, you may not store the number
of nodes as an attribute within the class.
You may not use a container from the STL.
I've attached an incomplete BTree.h header file to get you started.
A sample run:
Could be anything that demonstrates the class. A menu-driven
program would be great for this.
Perhaps include options for insert, remove, search, count, print
operations:
(I)nsert
(R)emove
(S)earch
(C)ount
(P)rint
(Q)uit
In C++ pls
// complete these pre-processor directives
#ifndef
#define
#include <iostream>
using std::cout;
using std::endl;
template <class T>
class BTree
{
public:
// constructor
// destructor
// public interface prototypes
void insert( T );
void remove( T );
void preprint() const;
bool search( T value ) const
int count() const
private:
// define and declare the BTree node here
// recursive method prototypes
void preprint( Node* ) const;
};
// method definitions here
template< class T >
void BTree<T>::preprint( Node* r ) const
{
}
#endif
#include<iostream.h>
#include<conio.h>
using namespace std;
template<class T>
class BinaryTreeTemplate
private:
{
struct tree_node
{
treenode*left;
treenode*right;
}
treenode*root;
public:
binarysearchtree()
{
void insert(T);
void remove(T);
bool search(T);
void preprint();
int count();
}
if(t->data < parent->data)
{
parent->left = t;
}
else
parent->right = t;
}
template<class T>
void BinarySearchTree<T>
{
tree_node*t=new tree_node;
t->left=null;
t->right=null;
treenode*currentnode;
current=root;
//insertions
//we are using switch cases
switch(ch)
{
case 1:
cout<<“enter the values”;
b.insert();
case 2:
cout<<“enter the data to be removed”;
b.remove();
case 3:
cout<<“enter the searching elements”;
b.search();
case 4:
cout<<“enter the printed data”;
b.print();
case 5:
cout<<“quit”;
b.quit();
return 0;
}
}
Binary Tree Template Write your own version of a class template that will create a binary...
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) =...
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++ (Using Binary Search Trees) other methods will result in downvote Implement the binary search tree methods (bst.cpp) for the binary search tree provided in the header file. Test your implementation with the included test. bst.h bst_test.cpp Note: Your implementation must correspond to declarations in the header file, and pass the test. Do not modify these two. I will compile your code against these. If the compilation fails, you will get down vote. bst.h #ifndef BINARY_SEARCH_TREE_H #define BINARY_SEARCH_TREE_H #include <string>...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. The definition of the class for a binary tree (as a template) is given as follows: template < class T > class binTree { public: binTree ( ); // default constructor unsigned height ( ) const; // returns height of tree virtual void insert ( const T& ); //...
C++ Binary Search Tree question. I heed help with the level 2 question please, as level 1 is already completed. I will rate the answer a 100% thumbs up. I really appreciate the help!. Thank you! searching.cpp #include <getopt.h> #include <iostream> #include <sstream> #include <stdlib.h> #include <unistd.h> using namespace std; // global variable for tree operations // use to control tree maintenance operations enum Mode { simple, randomised, avl } mode; // tree type // returns size of tree //...
C++ Binary Search Tree question. I heed help with the level 2 question please, as level 1 is already completed. I will rate the answer a 100% thumbs up. I really appreciate the help!. Thank you! searching.cpp #include <getopt.h> #include <iostream> #include <sstream> #include <stdlib.h> #include <unistd.h> using namespace std; // global variable for tree operations // use to control tree maintenance operations enum Mode { simple, randomised, avl } mode; // tree type // returns size of tree //...
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,...
Take the following code for a binary source tree and make it include the following operations. bool replace(const Comparable & item, const Comparable & replacementItem); int getNumberOfNodes() const; (a) The replace method searches for the node that contains item in a binary search tree, if it is found, it replaces item with replacementItem. The binary tree should remain as a binary search tree after the replacement is done. Add the replace operation to the BinarySearchTree class. Test your replace using...
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);...
Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary tree. Add this function to the class binaryTreeType and create a program to test this function. #include <iostream> using namespace std; //Definition of the Node template <class elemType> struct nodeType { elemType info; nodeType<elemType> *lLink; nodeType<elemType> *rLink; }; //Definition of the class template <class elemType> class binaryTreeType { public: //Overload the assignment operator. const binaryTreeType<elemType>& operator=(const binaryTreeType<elemType>&) { if (this != &otherTree) //avoid self-copy...