Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 // std::less

#include

#include // std::queue

#include // std::pair

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& bst, std::ostream & out );

template

friend std::ostream& printNode(std::ostream& o, const typename BinarySearchTree::node& bn);

template

friend void printTree( const BinarySearchTree& bst, std::ostream & out );

template

friend void printTree(typename BinarySearchTree::const_node_ptr t, std::ostream & out, unsigned depth );

template

friend void vizTree(

typename BinarySearchTree::const_node_ptr node,

std::ostream & out,

typename BinarySearchTree::const_node_ptr prev

);

template

friend void vizTree(

const BinarySearchTree & bst,

std::ostream & out

);

};

template

std::ostream& printNode(std::ostream & o, const typename BinarySearchTree::node & bn) {

return o << '(' << bn.element.first << ", " << bn.element.second << ')';

}

template

void printLevelByLevel( const BinarySearchTree& bst, std::ostream & out = std::cout ) {

using node = typename BinarySearchTree::node;

using node_ptr = typename BinarySearchTree::node_ptr;

using const_node_ptr = typename BinarySearchTree::const_node_ptr;

// TODO -- Guide in Instructions

}

template

void printTree( const BinarySearchTree & bst, std::ostream & out = std::cout ) { printTree(bst._root, out ); }

template

void printTree(typename BinarySearchTree::const_node_ptr t, std::ostream & out, unsigned depth = 0 ) {

if (t != nullptr) {

printTree(t->right, out, depth + 1);

for (unsigned i = 0; i < depth; ++i)

out << '\t';

printNode(out, *t) << ' ';

printTree(t->left, out, depth + 1);

}

}

template

void vizTree(

typename BinarySearchTree::const_node_ptr node,

std::ostream & out,

typename BinarySearchTree::const_node_ptr prev = nullptr

) {

if(node) {

std::hash khash{};

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(node->left, out, node);

vizTree(node->right, out, node);

}

}

template

void vizTree(

const BinarySearchTree & bst,

std::ostream & out = std::cout

) {

out << "digraph Tree {" << std::endl;

vizTree(bst._root, out);

out << "}" << std::endl;

}

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

Mobile Communications

Authors: Jochen Schiller

2nd edition

978-0321123817, 321123816, 978-8131724262

More Books

Students also viewed these Programming questions