0

prog6.h

#include "340.h"
#include "binTree.h"

#ifndef H_PROG6
#define H_PROG6

const vector < int > A{ 1, -2, 3, -4, 5, -6, 7, -8, 9, -10, 11, -12, 13, -14, 15 };
const vector < float > B{ 0.5, 1.75, -3, 4.25, 5.50, -6.75, 8, 9.25, -10.5 };
const vector < string > C{ "This", "is", "a", "list", "of", "string", "objects." };


template < class T > void print(T& x);
template < class T > void increment(T& x);
template < class T > void decrement(T& x);
template < class T > void isEmpty(const binTree < T >&);
template < class T > void header(const binTree < T >&);
template < class T > void inOrder(binTree < T >&, const string&);
template < class T > void preOrder(binTree < T >&, const string&);
template < class T > void postOrder(binTree < T >&, const string&);
template < class T > void print_attributes(binTree < T >& tree, const string& name);


#endif

Node.h

#include "340.h"

#ifndef H_NODE
#define H_NODE

// definition for class of nodes in bin tree

template < class T > class binTree; // forward declaration

template < class T >
class Node {
    friend class binTree < T >;         // binTree is friend
public:
    // default constructor
    Node(const T& x = T(), Node < T >* l = 0, Node < T >* r = 0) :
        data(x), left(l), right(r) { }
private:
    T data;                         // data component
    Node < T > *left, *right;       // left and right links
};
#endif

binTree.h

#ifndef BINTREE_H
#define BINTREE_H

#include "340.h"
#include "Node.h"

template < class T > class binTree {

public:
    binTree(); // default constructor   

    bool empty() const; // checks if tree empty 

    unsigned size() const; // returns no of nodes 

    unsigned height() const; // returns height of tree 

    virtual void insert(const T&); // inserts a node in shortest subtree 

    void inorder(void(*) (T&)); // inorder traversal of tree 

    void preorder(void(*) (T&)); // preorder traversal of tree 

    void postorder(void(*) (T&)); // postorder traversal of tree 

protected:
    Node < T >* root; // root of tree 

private:
    unsigned size(Node < T >*) const; // private version of size ( ) 

    unsigned height(Node < T >*) const; // private version of height ( ) 

    void insert(Node < T >*&, const T&); // private version of insert ( ) 

    void inorder(Node < T >*, void(*) (T&)); // private version of inorder ( ) 

    void preorder(Node < T >*, void(*) (T&)); // private version of preorder ( ) 

    void postorder(Node < T >*, void(*) (T&));// private version of postorder ( ) 
};

#endif

340.h

#ifndef H_340
#define H_340

#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>

#include <climits>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <stdexcept>

#include <algorithm>
#include <array>
#include <deque>
#include <functional>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#include <assert.h>
#include <string.h>

using namespace std;
using namespace std::placeholders;

prog6.cc

#include "340.h"
#include "prog6.h"
#include "Node.h"


int main()
{
    binTree<int> tree_A;
    /*****************************************************

    VECTOR A

    *****************************************************/
    //Inserting the Vector Elements into the Tree
    typedef vector<int>::const_iterator TT;
    for (TT i = A.begin(); i != A.end(); i++)
    {
        tree_A.insert(*i);
    }

    //increases it's data value by 1
    tree_A.preorder(increment);

    //calls the Header Function
    cout << "first: ";
    header(tree_A);

    inOrder(tree_A, "first");

    preOrder(tree_A, "first");

    postOrder(tree_A, "first");

    cout << endl;

    binTree<float> tree_B;
    /*****************************************************

    VECTOR B

    *****************************************************/
    //Inserting the Vector Elements into the Tree
    typedef vector<float>::const_iterator IT;
    for (IT j = B.begin(); j != B.end(); j++)
    {
        tree_B.insert(*j);
    }

    //decreases it's data value by 1
    tree_B.postorder(decrement);

    //calls the Header Function
    cout << "second: ";
    header(tree_B);

    inOrder(tree_B, "second");

    preOrder(tree_B, "second");


    postOrder(tree_B, "second");

    cout << endl;

    binTree<string> tree_C;

    //Inserting the Vector Elements into the Tree
    typedef vector<string>::const_iterator ITS;
    for (ITS k = C.begin(); k != C.end(); k++)
    {
        tree_C.insert(*k);
    }

    //calls the Header Function
    cout << "third: ";
    header(tree_C);

    inOrder(tree_C, "third");

    preOrder(tree_C, "third");

    postOrder(tree_C, "third");


    return 0;
}


template < class T > void  print(T& x)
{
    cout << setw(2) << x << " ";
}


template < class T > void increment(T& x)
{
    x = x + 1;
}


template < class T > void decrement(T& x)
{
    x = x - 1;
}


template < class T > void isEmpty(const binTree< T >& p)
{
    if (p.empty())
    {
        cout << "tree is empty" << endl;
    }
    else
    {
        cout << "tree is not empty" << endl;
    }

}

template < class T > void header(const binTree< T >& p)
{
    isEmpty(p);
    cout << "      no of nodes    = " << setw(2) << p.size() << endl;
    cout << "      height of tree = " << setw(2) << p.height() << endl << endl;
}


template < class T > void inOrder(binTree<T>& p, const string& x)
{
    cout << "The elements of '" << x << "' in inorder:" << endl;
    cout << " " << '\t';
    p.inorder(print);
    cout << endl;
}

template < class T > void preOrder(binTree<T>& p, const string& x)
{
    cout << "The elements of '" << x << "' in preorder:" << endl;
    cout << " " << '\t';
    p.preorder(print);
    cout << endl;
}


template < class T > void postOrder(binTree<T>& p, const string& x)
{
    cout << "The elements of '" << x << "' in postorder:" << endl;
    cout << " " << '\t';
    p.postorder(print);
    cout << endl;
}

/*template <class T> void print_attributes(binTree <T>& tree, const string& name)
{
    cout << "The name of of the tree is" << name << endl;

}
*/

I can't get it to compile and need help fixing the void print_attributes.

Thanks

#endif
2
Contributors
1
Reply
13
Views
2 Years
Discussion Span
Last Post by Banfa
0

Actually your code compiles fine, but it fails to link because in binTree.h you declare all the functions in the template class binTree but you don't define any of them. You need to write the imlementation of all those functions.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.