Advertisement
Guest User

binaryTree.h and binarySearchTree.h

a guest
Apr 23rd, 2014
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 16.39 KB | None | 0 0
  1. [code]
  2.   //Header File Binary Search Tree
  3. #ifndef H_binaryTree
  4. #define H_binaryTree
  5.  
  6. #include <iostream>
  7.  
  8. using namespace std;
  9.  
  10.     //Definition of the Node
  11. template <class elemType>
  12. struct nodeType
  13. {
  14.     elemType info;
  15.     nodeType<elemType> *lLink;
  16.     nodeType<elemType> *rLink;
  17. };
  18.    
  19.     //Definition of the class
  20. template <class elemType>
  21. class binaryTreeType
  22. {
  23. public:
  24.     const binaryTreeType<elemType>& operator=
  25.                  (const binaryTreeType<elemType>&);
  26.       //Overload the assignment operator.
  27.  
  28.     bool isEmpty() const;
  29.       //Function to determine whether the binary tree is empty.
  30.       //Postcondition: Returns true if the binary tree is empty;
  31.       //               otherwise, returns false.
  32.  
  33.     void inorderTraversal() const;
  34.       //Function to do an inorder traversal of the binary tree.
  35.       //Postcondition: Nodes are printed in inorder sequence.
  36.  
  37.     void preorderTraversal() const;
  38.       //Function to do a preorder traversal of the binary tree.
  39.       //Postcondition: Nodes are printed in preorder sequence.
  40.  
  41.     void postorderTraversal() const;
  42.       //Function to do a postorder traversal of the binary tree.
  43.       //Postcondition: Nodes are printed in postorder sequence.
  44.  
  45.     int treeHeight() const;
  46.       //Function to determine the height of a binary tree.
  47.       //Postcondition: Returns the height of the binary tree.
  48.  
  49.     int treeNodeCount() const;
  50.       //Function to determine the number of nodes in a
  51.       //binary tree.
  52.       //Postcondition: Returns the number of nodes in the
  53.       //               binary tree.
  54.  
  55.     int treeLeavesCount() const;
  56.       //Function to determine the number of leaves in a
  57.       //binary tree.
  58.       //Postcondition: Returns the number of leaves in the
  59.       //               binary tree.
  60.  
  61.     void destroyTree();
  62.       //Function to destroy the binary tree.
  63.       //Postcondition: Memory space occupied by each node
  64.       //               is deallocated.
  65.       //               root = NULL;
  66.  
  67.     virtual bool search(const elemType& searchItem) const = 0;
  68.       //Function to determine if searchItem is in the binary
  69.       //tree.
  70.       //Postcondition: Returns true if searchItem is found in
  71.       //               the binary tree; otherwise, returns
  72.       //               false.
  73.  
  74.     virtual void insert(const elemType& insertItem) = 0;
  75.       //Function to insert insertItem in the binary tree.
  76.       //Postcondition: If there is no node in the binary tree
  77.       //               that has the same info as insertItem, a
  78.       //               node with the info insertItem is created
  79.       //               and inserted in the binary search tree.
  80.  
  81.     virtual void deleteNode(const elemType& deleteItem) = 0;
  82.       //Function to delete deleteItem from the binary tree
  83.       //Postcondition: If a node with the same info as
  84.       //               deleteItem is found, it is deleted from
  85.       //               the binary tree.
  86.       //               If the binary tree is empty or
  87.       //               deleteItem is not in the binary tree,
  88.       //               an appropriate message is printed.
  89.  
  90.     binaryTreeType(const binaryTreeType<elemType>& otherTree);
  91.       //Copy constructor
  92.  
  93.     binaryTreeType();  
  94.       //Default constructor
  95.  
  96.     ~binaryTreeType();  
  97.       //Destructor
  98.  
  99. protected:
  100.     nodeType<elemType>  *root;
  101.  
  102. private:
  103.     void copyTree(nodeType<elemType>* &copiedTreeRoot,
  104.                   nodeType<elemType>* otherTreeRoot);
  105.       //Makes a copy of the binary tree to which
  106.       //otherTreeRoot points.
  107.       //Postcondition: The pointer copiedTreeRoot points to
  108.       //               the root of the copied binary tree.
  109.  
  110.     void destroy(nodeType<elemType>* &p);
  111.       //Function to destroy the binary tree to which p points.
  112.       //Postcondition: Memory space occupied by each node, in
  113.       //               the binary tree to which p points, is
  114.       //               deallocated.
  115.       //               p = NULL;
  116.  
  117.     void inorder(nodeType<elemType> *p) const;
  118.       //Function to do an inorder traversal of the binary
  119.       //tree to which p points.  
  120.       //Postcondition: Nodes of the binary tree, to which p
  121.       //               points, are printed in inorder sequence.
  122.  
  123.     void preorder(nodeType<elemType> *p) const;
  124.       //Function to do a preorder traversal of the binary
  125.       //tree to which p points.  
  126.       //Postcondition: Nodes of the binary tree, to which p
  127.       //               points, are printed in preorder
  128.       //               sequence.
  129.  
  130.     void postorder(nodeType<elemType> *p) const;
  131.       //Function to do a postorder traversal of the binary
  132.       //tree to which p points.  
  133.       //Postcondition: Nodes of the binary tree, to which p
  134.       //               points, are printed in postorder
  135.       //               sequence.
  136.  
  137.     int height(nodeType<elemType> *p) const;
  138.       //Function to determine the height of the binary tree
  139.       //to which p points.
  140.       //Postcondition: Height of the binary tree to which
  141.       //               p points is returned.
  142.  
  143.     int max(int x, int y) const;
  144.       //Function to determine the larger of x and y.
  145.       //Postcondition: Returns the larger of x and y.
  146.  
  147.     int nodeCount(nodeType<elemType> *p) const;
  148.       //Function to determine the number of nodes in
  149.       //the binary tree to which p points.
  150.       //Postcondition: The number of nodes in the binary
  151.       //               tree to which p points is returned.
  152.  
  153.     int leavesCount(nodeType<elemType> *p) const;
  154.       //Function to determine the number of leaves in  
  155.       //the binary tree to which p points
  156.       //Postcondition: The number of leaves in the binary
  157.       //               tree to which p points is returned.
  158. };
  159.  
  160.     //Definition of member functions
  161.  
  162. template <class elemType>
  163. binaryTreeType<elemType>::binaryTreeType()
  164. {
  165.     root = NULL;
  166. }
  167.  
  168. template <class elemType>
  169. bool binaryTreeType<elemType>::isEmpty() const
  170. {
  171.     return (root == NULL);
  172. }
  173.  
  174. template <class elemType>
  175. void binaryTreeType<elemType>::inorderTraversal() const
  176. {
  177.     inorder(root);
  178. }
  179.  
  180. template <class elemType>
  181. void binaryTreeType<elemType>::preorderTraversal() const
  182. {
  183.     preorder(root);
  184. }
  185.  
  186. template <class elemType>
  187. void binaryTreeType<elemType>::postorderTraversal() const
  188. {
  189.     postorder(root);
  190. }
  191.  
  192. template <class elemType>
  193. int binaryTreeType<elemType>::treeHeight() const
  194. {
  195.     return height(root);
  196. }
  197.  
  198. template <class elemType>
  199. int binaryTreeType<elemType>::treeNodeCount() const
  200. {
  201.     return nodeCount(root);
  202. }
  203.  
  204. template <class elemType>
  205. int binaryTreeType<elemType>::treeLeavesCount() const
  206. {
  207.     return leavesCount(root);
  208. }
  209.  
  210. template <class elemType>
  211. void  binaryTreeType<elemType>::copyTree
  212.                        (nodeType<elemType>* &copiedTreeRoot,
  213.                         nodeType<elemType>* otherTreeRoot)
  214. {
  215.     if (otherTreeRoot == NULL)
  216.         copiedTreeRoot = NULL;
  217.     else
  218.     {
  219.         copiedTreeRoot = new nodeType<elemType>;
  220.         copiedTreeRoot->info = otherTreeRoot->info;
  221.         copyTree(copiedTreeRoot->lLink, otherTreeRoot->lLink);
  222.         copyTree(copiedTreeRoot->rLink, otherTreeRoot->rLink);
  223.     }
  224. } //end copyTree
  225.  
  226. template <class elemType>
  227. void binaryTreeType<elemType>::inorder
  228.                               (nodeType<elemType> *p) const
  229. {
  230.     if (p != NULL)
  231.     {
  232.         inorder(p->lLink);
  233.         cout << p->info << " ";
  234.         inorder(p->rLink);
  235.     }
  236. }
  237.  
  238. template <class elemType>
  239. void binaryTreeType<elemType>::preorder
  240.                               (nodeType<elemType> *p) const
  241. {
  242.     if (p != NULL)
  243.     {
  244.         cout << p->info << " ";
  245.         preorder(p->lLink);
  246.         preorder(p->rLink);
  247.     }
  248. }
  249.  
  250. template <class elemType>
  251. void binaryTreeType<elemType>::postorder
  252.                               (nodeType<elemType> *p) const
  253. {
  254.     if (p != NULL)
  255.     {
  256.         postorder(p->lLink);
  257.         postorder(p->rLink);
  258.         cout << p->info << " ";
  259.     }      
  260. }
  261.  
  262.    //Overload the assignment operator
  263. template <class elemType>
  264. const binaryTreeType<elemType>& binaryTreeType<elemType>::
  265.         operator=(const binaryTreeType<elemType>& otherTree)
  266. {
  267.     if (this != &otherTree) //avoid self-copy
  268.     {
  269.         if (root != NULL)   //if the binary tree is not empty,
  270.                             //destroy the binary tree
  271.             destroy(root);
  272.  
  273.         if (otherTree.root == NULL) //otherTree is empty
  274.             root = NULL;
  275.         else
  276.             copyTree(root, otherTree.root);
  277.     }//end else
  278.  
  279.     return *this;
  280. }
  281.  
  282. template <class elemType>
  283. void  binaryTreeType<elemType>::destroy(nodeType<elemType>* &p)
  284. {
  285.     if (p != NULL)
  286.     {
  287.         destroy(p->lLink);
  288.         destroy(p->rLink);
  289.         delete p;
  290.         p = NULL;
  291.     }
  292. }
  293.  
  294. template <class elemType>
  295. void  binaryTreeType<elemType>::destroyTree()
  296. {
  297.     destroy(root);
  298. }
  299.  
  300.     //copy constructor
  301. template <class elemType>
  302. binaryTreeType<elemType>::binaryTreeType
  303.                 (const binaryTreeType<elemType>& otherTree)
  304. {
  305.     if (otherTree.root == NULL) //otherTree is empty
  306.         root = NULL;
  307.     else
  308.         copyTree(root, otherTree.root);
  309. }
  310.  
  311.     //Destructor
  312. template <class elemType>
  313. binaryTreeType<elemType>::~binaryTreeType()
  314. {
  315.     destroy(root);
  316. }
  317.  
  318. template<class elemType>
  319. int binaryTreeType<elemType>::height
  320.                              (nodeType<elemType> *p) const
  321. {
  322.     if (p == NULL)
  323.         return 0;
  324.     else
  325.         return 1 + max(height(p->lLink), height(p->rLink));
  326. }
  327.  
  328. template <class elemType>
  329. int binaryTreeType<elemType>::max(int x, int y) const
  330. {
  331.     if (x >= y)
  332.         return x;
  333.     else
  334.         return y;
  335. }
  336.  
  337. template <class elemType>
  338. int binaryTreeType<elemType>::nodeCount(nodeType<elemType> *p) const
  339. {
  340.     cout << "Write the definition of the function nodeCount."
  341.          << endl;
  342.  
  343.     return 0;
  344. }
  345.  
  346. template <class elemType>
  347. int binaryTreeType<elemType>::leavesCount(nodeType<elemType> *p) const
  348. {
  349.     cout << "Write the definition of the function leavesCount."
  350.          << endl;
  351.  
  352.     return 0;
  353. }
  354.  
  355. #endif
  356. [/code]
  357.  
  358. And here's the binarySearchTree.h that is giving me the errors.
  359. [code]
  360. //Header File Binary Search Tree
  361.  
  362. #ifndef H_binarySearchTree
  363. #define H_binarySearchTree
  364. #include <iostream>
  365. #include "binaryTree.h"
  366.  
  367. using namespace std;
  368.  
  369. template <class elemType>
  370. class bSearchTreeType: public binaryTreeType<elemType>
  371. {
  372. public:
  373.    bool search(const elemType& searchItem) const;
  374.      //Function to determine if searchItem is in the binary
  375.      //search tree.
  376.      //Postcondition: Returns true if searchItem is found in
  377.      //               the binary search tree; otherwise,
  378.      //               returns false.
  379.  
  380.    void insert(const elemType& insertItem);
  381.      //Function to insert insertItem in the binary search tree.
  382.      //Postcondition: If there is no node in the binary search
  383.      //               tree that has the same info as
  384.      //               insertItem, a node with the info
  385.      //               insertItem is created and inserted in the
  386.      //               binary search tree.
  387.  
  388.    void deleteNode(const elemType& deleteItem);
  389.      //Function to delete deleteItem from the binary search tree
  390.      //Postcondition: If a node with the same info as deleteItem
  391.      //               is found, it is deleted from the binary
  392.      //               search tree.
  393.      //               If the binary tree is empty or deleteItem
  394.      //               is not in the binary tree, an appropriate
  395.      //               message is ptinted.
  396.  
  397. private:
  398.    void deleteFromTree(nodeType<elemType>* &p);
  399.      //Function to delete the node to which p points is
  400.      //deleted from the binary search tree.
  401.      //Postcondition: The node to which p points is deleted
  402.      //               from the binary search tree.
  403. };
  404.  
  405.  
  406. template <class elemType>
  407. bool bSearchTreeType<elemType>::search
  408.                    (const elemType& searchItem) const
  409. {
  410.    nodeType<elemType> *current;
  411.    bool found = false;
  412.  
  413.    if (root == NULL)
  414.        cout << "Cannot search an empty tree." << endl;
  415.    else
  416.    {
  417.       current = root;
  418.  
  419.       while (current != NULL && !found)
  420.        {
  421.            if (current->info == searchItem)
  422.                found = true;
  423.            else if (current->info > searchItem)
  424.                current = current->lLink;
  425.            else
  426.                current = current->rLink;
  427.        }//end while
  428.    }//end else
  429.  
  430.    return found;
  431. }//end search
  432.  
  433. template <class elemType>
  434. void bSearchTreeType<elemType>::insert
  435.                 (const elemType& insertItem)
  436. {
  437.    nodeType<elemType> *current; //pointer to traverse the tree
  438.    nodeType<elemType> *trailCurrent; //pointer behind current
  439.    nodeType<elemType> *newNode;  //pointer to create the node
  440.  
  441.    newNode = new nodeType<elemType>;
  442.    newNode->info = insertItem;
  443.    newNode->lLink = NULL;
  444.    newNode->rLink = NULL;
  445.  
  446.    if (root == NULL)
  447.        root = newNode;
  448.    else
  449.    {
  450.        current = root;
  451.  
  452.        while (current != NULL)
  453.        {
  454.            trailCurrent = current;
  455.  
  456.            if (current->info == insertItem)
  457.            {
  458.                cout << "The item to be inserted is already ";
  459.                cout << "in the tree -- duplicates are not "
  460.                     << "allowed." << endl;
  461.                return;
  462.            }
  463.            else if (current->info > insertItem)
  464.                current = current->lLink;
  465.            else
  466.                current = current->rLink;
  467.        }//end while
  468.  
  469.        if (trailCurrent->info > insertItem)
  470.            trailCurrent->lLink = newNode;
  471.        else
  472.            trailCurrent->rLink = newNode;
  473.    }
  474. }//end insert
  475.  
  476. template <class elemType>
  477. void bSearchTreeType<elemType>::deleteNode
  478.                                (const elemType& deleteItem)
  479. {
  480.    nodeType<elemType> *current; //pointer to traverse the tree
  481.    nodeType<elemType> *trailCurrent; //pointer behind current
  482.    bool found = false;
  483.  
  484.    if (root == NULL)
  485.        cout << "Cannot delete from an empty tree."
  486.             << endl;
  487.    else
  488.    {
  489.        current = root;
  490.        trailCurrent = root;
  491.  
  492.        while (current != NULL && !found)
  493.        {
  494.            if (current->info == deleteItem)
  495.                found = true;
  496.            else
  497.            {
  498.                trailCurrent = current;
  499.  
  500.                if (current->info > deleteItem)
  501.                    current = current->lLink;
  502.                else
  503.                    current = current->rLink;
  504.            }
  505.        }//end while
  506.  
  507.        if (current == NULL)
  508.            cout << "The item to be deleted is not in the tree."
  509.                 << endl;
  510.        else if (found)
  511.        {
  512.            if (current == root)
  513.                deleteFromTree(root);
  514.            else if (trailCurrent->info > deleteItem)
  515.                deleteFromTree(trailCurrent->lLink);
  516.            else
  517.                deleteFromTree(trailCurrent->rLink);
  518.        }
  519.        else
  520.            cout << "The item to be deleted is not in the tree."
  521.                 << endl;
  522.    }
  523. } //end deleteNode
  524.  
  525. template <class elemType>
  526. void bSearchTreeType<elemType>::deleteFromTree
  527.                                 (nodeType<elemType>* &p)
  528. {
  529.    nodeType<elemType> *current; //pointer to traverse the tree
  530.    nodeType<elemType> *trailCurrent;  //pointer behind current
  531.    nodeType<elemType> *temp;      //pointer to delete the node
  532.  
  533.    if (p == NULL)
  534.        cout << "Error: The node to be deleted is NULL."
  535.             << endl;
  536.    else if (p->lLink == NULL && p->rLink == NULL)
  537.    {
  538.        temp = p;
  539.        p = NULL;
  540.        delete temp;
  541.    }
  542.    else if (p->lLink == NULL)
  543.    {
  544.        temp = p;
  545.        p = temp->rLink;
  546.        delete temp;
  547.    }
  548.    else if (p->rLink == NULL)
  549.    {
  550.        temp = p;
  551.        p = temp->lLink;
  552.        delete temp;
  553.    }
  554.    else
  555.    {
  556.        current = p->lLink;
  557.        trailCurrent = NULL;
  558.  
  559.        while (current->rLink != NULL)
  560.        {
  561.            trailCurrent = current;
  562.            current = current->rLink;
  563.        }//end while
  564.  
  565.        p->info = current->info;
  566.  
  567.        if (trailCurrent == NULL) //current did not move;
  568.                               //current == p->lLink; adjust p
  569.            p->lLink = current->lLink;
  570.        else
  571.            trailCurrent->rLink = current->lLink;
  572.  
  573.        delete current;
  574.    }//end else
  575. } //end deleteFromTree
  576.  
  577. #endif
  578. [/code]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement