#ifndef BINARY_TREE_H
#define BINARY_TREE_H
#include <boost/lexical_cast.hpp>
#include "Exception.h"
#include "DoubleLinkedListNode.h"
#include <stdio.h>
#include <iostream>
using namespace std;
template<typename T>
class BinaryTree
{
public:
// assignment operator. over loaded.
const BinaryTree<T>& operator = (const BinaryTree<T>& otherList)
{
if(rootNode != NULL)
{
destroy(rootNode);
if(otherList.rootNode == NULL)
{
rootNode = NULL;
}
else
{
copyTree(rootNode,otherList.rootNode);
}
}
return *this;
}
// returns whether the tree is currently empty.
bool isTreeEmpty() const
{
return (rootNode == NULL);
}
// Inorder traversal.
// nodes are visited in order
// post-condition: Nodes are printed in inorder sequence.
void inorderTraverlsal()
{
inorderTraverlsal(rootNode);
}
// preorder traversal.
// traverse the binary tree in pre order.
void preorderTraversal()
{
preorderTraversal(rootNode);
}
// post order traversal
// traverse the binary tree in post order.
void postOrderTraversal()
{
postOrderTraversal(rootNode);
}
// function to return the tree hieght.
int getTreeHeight() const
{
return height(rootNode);
}
// get Leaf count.
int getLeafCount() const
{
return leafCount(rootNode);
}
// get the tree node count
int getTreeNodeCount() const
{
return nodeCount(rootNode);
}
// function to destroy the tree.
void destroyTree()
{
destroy(rootNode);
}
DListNode<T>* getRoot() const
{
return rootNode;
}
// virtual function to search for an item in the tree.
// returns a boolean
virtual bool search(const T& elem) const = 0;
// add item to tree
// post condition: inserted item does not already exist.
virtual void InsertNode(T& data) = 0;
// add node to tree.
// same as the insertNode but takes a pointer to a node rather then
// just the data
virtual void InsertNode(DListNode<T>* node) = 0;
// delete node.
// function to delete a node based on data infro.
virtual void deleteNode(const T& item) = 0;
// function to delete node based on a node input.
virtual void deleteNode(DListNode<T>* &node) = 0;
BinaryTree()
{
rootNode = NULL;
}
BinaryTree(const BinaryTree<T>& otherTree)
{
operator=(otherTree);
}
~BinaryTree()
{
destroyTree();
}
protected:
DListNode<T>* rootNode; // root node
private:
// function to copy a tree to another tree.
void copyTree(DListNode<T>* ©Root,
DListNode<T>* otherTreeRoot)
{
if(otherTreeRoot == NULL)
{
copyRoot = NULL;
}
else
{
copyRoot = new DListNode<T>();
copyRoot->setData(otherTreeRoot->getData());
copyTree(copyRoot->getFirstLink(), otherTreeRoot->getFirstLink());
copyTree(copyRoot->getLastLink(), otherTreeRoot->getLastLink());
}
}
// function to delete a node.
void destroy(DListNode<T>* Node)
{
if(Node != NULL)
{
destroy(Node->getFirstLink());
destroy(Node->getLastLink());
delete Node;
Node = NULL;
}
}
// inorder traversal function
void inorderTraverlsal(DListNode<T>* node)
{
if(node != NULL)
{
inorderTraverlsal(node->getFirstLink());
cout << node->getData() << " ";
inorderTraverlsal(node->getLastLink());
}
}
// pre-order traversal function
void preorderTraversal(DListNode<T>* node)
{
if(node != NULL)
{
cout << node->getData() << " ";
preorderTraversal(node->getFirstLink());
preorderTraversal(node->getLastLink());
}
}
// post-order traversal function
void postOrderTraversal(DListNode<T>* node)
{
if(node != NULL)
{
postOrderTraversal(node->getFirstLink());
postOrderTraversal(node->getLastLink());
cout << node->getData() << " ";
}
}
int height(DListNode<T>* &p)
{
if (p == NULL)
{
return 0;
}
else
{
int X = height(p->getFirstLink());
int Y = height(p->getLastLink());
return 1 + Max(X,Y);
}
}
// returns the bigger value of x or y depending which is bigger.
int Max(int x, int y)
{
if(x >= y)
return x;
else
return y;
}
// gets the node count
int nodeCount(DListNode<T>& node)
{
/*
if(node == NULL)
{
return 0;
}
else
{
if(node->getFirstLink() == NULL && node->getLastLink() == NULL)
{
return 1;
}
else
return (1 + nodeCount(node.getFirstLink()) + nodeCount(node.getLastLink()));
}*/
}
// get leaf count
int leafCount(const DListNode<T>* &p)
{
}
};
#endif
Now, the functions height and getTreeheight return this weird error which makes no sense what so ever to me.:
1>c:\users\tim sweeny\documents\visual studio 2008\projects\x\datastructures\binarytree.h(66) : error C2662: 'BinaryTree<T>::height' : cannot convert 'this' pointer from 'const BinaryTree<T>' to 'BinaryTree<T> &'
So yea, this is really making me angry like the hulk *Enerjak SMASH*






