Question
Need help with bolded functions #pragma once #include // std::less #include #include // std::queue #include // std::pair template class BinarySearchTree { public: using key_type =
Need help with bolded functions
#pragma once
#include
#include
#include
#include
template
class BinarySearchTree
{
public:
using key_type = K;
using value_type = V;
using key_compare = Comparator;
using pair = std::pair
using pointer = pair*;
using const_pointer = const pair*;
using reference = pair&;
using const_reference = const pair&;
using difference_type = ptrdiff_t;
using size_type = size_t;
private:
struct BinaryNode
{
pair element;
BinaryNode *left;
BinaryNode *right;
BinaryNode( const_reference theElement, BinaryNode *lt, BinaryNode *rt )
: element{ theElement }, left{ lt }, right{ rt } { }
BinaryNode( pair && theElement, BinaryNode *lt, BinaryNode *rt )
: element{ std::move( theElement ) }, left{ lt }, right{ rt } { }
};
using node = BinaryNode;
using node_ptr = node*;
using const_node_ptr = const node*;
node_ptr _root;
size_type _size;
key_compare comp;
public:
BinarySearchTree() {
_root = nullptr;
_size = 0;
}
BinarySearchTree( const BinarySearchTree & rhs ) {
_root = clone(rhs._root);
_size = rhs._size;
}
BinarySearchTree( BinarySearchTree && rhs ): _root{ rhs._root }, _size{ rhs._size }, comp{ rhs.comp } {
rhs._root = nullptr;
rhs._size = 0;
}
~BinarySearchTree() {
clear(_root);
}
const_reference min() const { return min( _root )->element; }
const_reference max() const { return max( _root )->element; }
const_reference root() const {
return _root->element;
}
bool contains( const key_type & x ) const { return contains( x, _root ); }
value_type & find( const key_type & key ) { return find( key, _root )->element.second; }
const value_type & find( const key_type & key ) const { return find( key, _root )->element.second; }
bool empty() const {
return _size == 0;
}
size_type size() const {
return _size;
}
void clear() {
clear( _root );
_size = 0;
}
void insert( const_reference x ) { insert( x, _root ); }
void insert( pair && x ) { insert( std::move( x ), _root ); }
void erase( const key_type & x ) { erase(x, _root); }
BinarySearchTree & operator=( const BinarySearchTree & rhs ) {
// TODO
}
BinarySearchTree & operator=( BinarySearchTree && rhs ) {
// TODO
}
private:
void insert( const_reference x, node_ptr & t ) {
if (t == nullptr) {
t = new node{x, nullptr, nullptr};
++_size;
}
else if (comp(t->element.first, x.first)) {
insert(x, t->right);
}
else if (comp(x.first, t->element.first)) {
insert(x, t->left);
}
else {
t->element.second = x.second;
}
}
void insert( pair && x, node_ptr & t ) {
if (t == nullptr) {
t = new node{ std::move(x), nullptr, nullptr };
}
else if (x.first < t->data.first) {
insert(std::move(x), t->left);
}
else if (t->data.first < x.first) {
insert(std::move(x), t->right);
}
else {
t->data.second = x.second;
}
}
void erase( const key_type & x, node_ptr & t ) {
// TODO
}
const_node_ptr min( const_node_ptr t ) const {
if (t == nullptr) {
return nullptr;
}
while (t->left != nullptr) {
t = t->left;
}
return t;
}
const_node_ptr max( const_node_ptr t ) const {
if (t == nullptr) {
return nullptr;
}
while (t->right != nullptr) {
t = t->right;
}
return t;
}
bool contains( const key_type & x, const_node_ptr t ) const {
if (t == nullptr) {
return false;
}
}
node_ptr find( const key_type & key, node_ptr t ) {
// TODO
}
const_node_ptr find( const key_type & key, const_node_ptr t ) const {
if (t == nullptr) {
return nullptr;
}
}
void clear( node_ptr & t ) {
// TODO
}
node_ptr clone ( const_node_ptr t ) const {
// TODO
}
public:
template
friend void printLevelByLevel( const BinarySearchTree
template
friend std::ostream& printNode(std::ostream& o, const typename BinarySearchTree
template
friend void printTree( const BinarySearchTree
template
friend void printTree(typename BinarySearchTree
template
friend void vizTree(
typename BinarySearchTree
std::ostream & out,
typename BinarySearchTree
);
template
friend void vizTree(
const BinarySearchTree
std::ostream & out
);
};
template
std::ostream& printNode(std::ostream & o, const typename BinarySearchTree
return o << '(' << bn.element.first << ", " << bn.element.second << ')';
}
template
void printLevelByLevel( const BinarySearchTree
using node = typename BinarySearchTree
using node_ptr = typename BinarySearchTree
using const_node_ptr = typename BinarySearchTree
// TODO -- Guide in Instructions
}
template
void printTree( const BinarySearchTree
template
void printTree(typename BinarySearchTree
if (t != nullptr) {
printTree
for (unsigned i = 0; i < depth; ++i)
out << '\t';
printNode
printTree
}
}
template
void vizTree(
typename BinarySearchTree
std::ostream & out,
typename BinarySearchTree
) {
if(node) {
std::hash
out << "\t" "node_" << (uint32_t) khash(node->element.first)
<< "[label=\"" << node->element.first
<< " [" << node->element.second << "]\"];" << std::endl;
if(prev)
out << "\tnode_" << (uint32_t) khash(prev->element.first) << " -> ";
else
out << "\t";
out << "node_" << (uint32_t) khash(node->element.first) << ";" << std::endl;
vizTree
vizTree
}
}
template
void vizTree(
const BinarySearchTree
std::ostream & out = std::cout
) {
out << "digraph Tree {" << std::endl;
vizTree
out << "}" << std::endl;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started