This is first program on binaryTrees.
The header file given in the textbook by DS Malik goes like this
I get these errors:
binarySearchTree.h:49:8: error: 'root' was not declared in this scope
if(root == NULL)
everywhere where there is "root" I get this error
//Header File Binary Search Tree
#ifndef H_binaryTree
#define H_binaryTree
#include <iostream>
usingnamespace 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:
const binaryTreeType<elemType>& operator=
(const binaryTreeType<elemType>&);
//Overload the assignment operator.
bool isEmpty();
//Function to determine if the binary tree is empty.
//Postcondition: Returns true if the binary tree is empty;
// otherwise, returns false.
void inorderTraversal();
//Function to do an inorder traversal of the binary tree.
//Postcondition: The nodes of the binary tree are output
// in the inorder sequence.
void preorderTraversal();
//Function to do a preorder traversal of the binary tree.
//Postcondition: The nodes of the binary tree are output
// in the preorder sequence.
void postorderTraversal();
//Function to do a postorder traversal of the binary tree.
//Postcondition: The nodes of the binary tree are output
// in the postorder sequence.
int treeHeight();
//Function to deteremine the height of the binary tree.
//Postcondition: The height of the binary tree is returned.
int treeNodeCount();
//Function to determine the number of nodes in the
//binary tree.
//Postcondition: The number of nodes in the binary tree
// is returned.
int treeLeavesCount();
//Function to determine the number of leaves in the
//binary tree.
//Postcondition: The number of leaves in the binary tree
// is returned.
void destroyTree();
//Deallocates the memory space occupied by the binary tree.
//Postcondition: root = NULL;
binaryTreeType(const binaryTreeType<elemType>& otherTree);
//copy constructor
binaryTreeType();
//default constructor
~binaryTreeType();
//destructor
protected:
nodeType<elemType> *root;
private:
void copyTree(nodeType<elemType>* &copiedTreeRoot,
nodeType<elemType>* otherTreeRoot);
//Function to make a copy of the binary tree to
//which otherTreeRoot points.
//Postcondition: The pointer copiedTreeRoot points to
// the root of the copied binary tree.
void destroy(nodeType<elemType>* &p);
//Function to destroy the binary tree to which p points.
//Postcondition: The nodes of the binary tree to which
// p points are deallocated.
// p = NULL.
void inorder(nodeType<elemType> *p);
//Function to do an inorder traversal of the binary
//tree to which p points.
//Postcondition: The nodes of the binary tree to which p
// points are output in the inorder sequence.
void preorder(nodeType<elemType> *p);
//Function to do a preorder traversal of the binary
//tree to which p points.
//Postcondition: The nodes of the binary tree to which p
// points are output in the preorder sequence.
void postorder(nodeType<elemType> *p);
//Function to do a postorder traversal of the binary
//tree to which p points.
//Postcondition: The nodes of the binary tree to which p
// points are output in the postorder sequence.
int height(nodeType<elemType> *p);
//Function to determine the height of the binary tree
//to which p points.
//Postcondition: The height of the binary tree to which p
// points is returned.
int max(int x, int y);
//Function to determine the larger of x and y.
//Postcondition: The larger of x and y is returned.
int nodeCount(nodeType<elemType> *p);
//Function to determine the number of nodes in the binary
//tree to which p points.
//Postcondition: The number of nodes in the binary tree
// to which p points is returned.
int leavesCount(nodeType<elemType> *p);
//Function to determine the number of leaves in the binary
//tree to which p points.
//Postcondition: The number of nodes in the binary tree
// to which p points is returned.
};
There is also the binarySearchTree that comes before the main program:
//Header File Binary Search Tree
#ifndef H_binarySearchTree
#define H_binarySearchTree
#include <iostream>
#include <cassert>
#include "binaryTree.h"
usingnamespace std;
template<class elemType>
class bSearchTreeType: public binaryTreeType<elemType>
{
public:
bool search(const elemType& searchItem);
//Function to determine if searchItem is in the binary
//search tree.
//Postcondition: Returns true if searchItem is found in the
// binary search tree; otherwise, returns false.
void insert(const elemType& insertItem);
//Function to insert insertItem in the binary search tree.
//Postcondition: If no node in the binary search tree has
// the same info as insertItem, a node with the
// info insertItem is created and inserted in the
//binary search tree.
void deleteNode(const elemType& deleteItem);
//Function to delete deleteItem from the binary search tree
//Postcondition: If a node with the same info as deleteItem
// is found, it is deleted from the binary
// search tree.
private:
void deleteFromTree(nodeType<elemType>* &p);
//Function to delete the node, to which p points, from the
//binary search tree.
//Postcondition: The node to which p points is deleted
// from the binary search tree.
};