Implement the function for the prototype below. Document your code. You do not have access to authors helper functions, only the binary_tree_node class.
template size_t count(binary_tree_node * root_ptr, Item value)
// Precondition: root_ptr is the root pointer of a binary tree (which may be empty).
// Postcondition: The number of nodes that contain value in the tree is returned.
size_t count(binary_tree_node * root_ptr, Item value)
{
// if the current subtree is empty
if( !root_ptr )
return 0;
// if the current node has the value 'value'
if( root_ptr->data == value )
// recursively find the number of nodes in left and right subtree
return 1 + count( root_ptr->left, value ) + count( root_ptr->right, value );
else
// recursively find the number of nodes in left and right subtree
return count( root_ptr->left, value ) + count( root_ptr->right, value );
}
Implement the function for the prototype below. Document your code. You do not have access to...
Short Answers Section 10.4 Tree Traversals 9. Using the binary_tree_node from Section 10.3, Write a recursive function to meet the following specification. You do not need to check the precondition. template <class Item> void increase(binary_tree_node<Item>* root_ptr) // Precondition: root_ptr is the root pointer of a binary tree. // Postcondition: Every node of the tree has had its data increased by one. 10. Using the binary_tree_node from Section 10.3, write a recursive function to meet the following specification. You do not...
9. Please implement the following function (recursively) 1. template < class Item> 2. void flip(binary_ tree_node Item root ptr) 3. I/ Precondition: root_ptr is the root pointer of a non-empty binary tree .I/ Postcondition: The tree is now the mirror image of its original value. Example: 2 3 3 2 Answer: 1. template class Item> 2. void lip (binary_tree_node <Item root ptr) 3. binary_tree_node <Ittemp; assert (root ptr!-NULL) 4.
Use a B-Tree to implement the set.h class. #ifndef MAIN_SAVITCH_SET_H #define MAIN_SAVITCH_SET_H #include <cstdlib> // Provides size_t namespace main_savitch_11 { template <class Item> class set { public: // TYPEDEFS typedef Item value_type; // CONSTRUCTORS and DESTRUCTOR set( ); set(const set& source); ~set( ) { clear( ); } // MODIFICATION MEMBER FUNCTIONS void operator =(const set& source); void clear( ); bool insert(const Item& entry); std::size_t erase(const Item& target); // CONSTANT MEMBER FUNCTIONS std::size_t count(const Item& target) const; bool empty( ) const...
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...
Can someone please help me fix my code on this assignment that's due in the morning? Please read the question carefully. Using C++, construct a graph class, graph.template, in which the edges are stored in adjacency sets. You need to use graph.h and test_graph.cpp to implement graph.template. The author provides an implementation of a graph using an adjacency matrix. I have started the template file but I cannot get it to compile. I have posted my template file of what...
Requirements Print a range Write a bag member function with two parameters. The two parameters are Items x and y. The function should write to the console all Items in the bag that are between the first occurrence of x and the first occurrence of y. You may assume that items can be compared for equality using ==. Use the following header for the function: void print_value_range(const Item& x, const Item& y); print_value_range can be interpreted in a number of...
Binary Search Tree Part A: The code attached in this document is a sample code to demonstrate insert operation in binary search tree. Please fill in the missing part for the insert method to make the program work. The expected output should be as follows. 20 30 40 50 60 70 80 Part B: Find Lowest Common Ancestor (LCA) of a Binary Search Tree. According to WikiPedia definition , The lowest common ancestor is defined between two nodes v and...
Please use the linked approach implement the BST ADT,
implement all the functions in the BSTree.cpp. Add the ouput of the
implementation.
use recursive functions to traverse the tree - read the
implementation notes on using helper functions.
Please use C++ programming
//////////////////////////////////////////////////////////////
#include "BSTree.h"
template <typename DataType, class KeyType>
BSTree<DataType, KeyType>::BSTreeNode::BSTreeNode ( const
DataType &nodeDataItem, BSTreeNode *leftPtr, BSTreeNode
*rightPtr )
{
}
template < typename DataType, class KeyType >
BSTree<DataType, KeyType>::BSTree ()
{
root = 0;
}
template...
Add a public member function to the BST class below that returns the size of the tree—the number of the nodes in the tree. For simplicity, the class contains only the key and not a value. You may add any helper functions you find necessary. (Hint: think recursion) template <typename T> struct Node { T key; Node<T>* left; Node<T>* parent; Node<T>* right; }; template <typename T> class BST { private: Node<T>* root; public: BST():...
I need help implemeting the remove_repetitions() Here is a brief outline of an algorithm: A node pointer p steps through the bag For each Item, define a new pointer q equal to p While the q is not the last Item in the bag If the next Item has data equal to the data in p, remove the next Item Otherwise move q to the next Item in the bag. I also need help creating a test program _____________________________________________________________________________________________________________________________________________________ #ifndef...