Guest User

BSTDict.cpp

a guest
Mar 11th, 2013
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.90 KB | None | 0 0
  1. #ifndef _BSTDICT_CPP
  2. #define _BSTDICT_CPP
  3.  
  4. //BSTDict.cpp
  5. #include "BSTDict.hpp"
  6. #include <cassert>
  7. #include <cstdlib> //for NULL
  8. #include <iostream>
  9.  
  10. // An implementation of a dictionary ADT as a binary search tree (without balancing
  11. // operations).
  12. //
  13. // The template type means that the dictionary will store elements with key type T, and
  14. // data type T; and two elements of type T will be compared using the operator() of a
  15. // Compare class.
  16. //
  17. // Compare() (elem1, elem2), which will return a number > 0 if elem1 is greater than
  18. // elem2, 0 if elem1 is equal to elem2, and < 0 if elem1 is less than elem2. Note that
  19. // this will NOT give the same result as directly comparing elem1 and elem2 as
  20. // "elem1 > elem2" as it does not correspond to any overloaded operator>, operator==, or
  21. // operator< defined by the type.
  22.  
  23. template <typename T, class Compare>
  24. BSTDict<T, Compare>::BSTDict() {
  25.     root = NULL;
  26. }
  27.  
  28. template <typename T, class Compare>
  29. BSTDict<T, Compare>::~BSTDict() {
  30.     // Ideally, we would recursively traverse the tree to delete all the Nodes.  We'd also
  31.     // need to keep track of whether or not to delete the data and key fields, which can
  32.     // be very tricky (are there any other pointers to these fields?)  The newest C++ has
  33.     // so-called smart pointers that will do this for you, but fortunately for us, in our
  34.     // program we never need to delete a dictionary.
  35.    
  36.     // So, we'll just do nothing in the destructor.
  37. }
  38.  
  39. template <typename T, class Compare>
  40. bool BSTDict<T, Compare>::find(T key, T& pred) {
  41.     return find_helper(root, key, pred);
  42. }
  43.  
  44. template <typename T, class Compare>
  45. void BSTDict<T, Compare>::add(T key, T pred) {
  46.     /*// Determine where the Node will be added.
  47.     Node** temp = &root; // A pointer to a pointer. :)
  48.     while (*temp != NULL) {
  49.         int compare = Compare()(key, (*temp)->key);
  50.         if (compare < 0) {
  51.             temp = &((*temp)->left);
  52.         } else if (compare > 0) {
  53.             temp = &((*temp)->right);
  54.         } else {
  55.             return; // The Node already exists.
  56.         }
  57.     }
  58.    
  59.     // Create the new Node.
  60.     *temp = new Node();
  61.     (*temp)->key = key;
  62.     (*temp)->data = pred;*/
  63.    
  64.     Node*& temp = add_helper(root, key);
  65.     temp = new Node();
  66.     temp->key = key;
  67.     temp->data = pred;
  68. }
  69.  
  70. template <typename T, class Compare>
  71. Node*& BSTDict<T, Compare>::add_helper(Node*& r, T key) {
  72.     if (r == NULL) {
  73.         return r;
  74.     }
  75.    
  76.     int compare = Compare()(key, r->key);
  77.     if (compare < 0) {
  78.         return add_helper(r->left, key);
  79.     } else if (compare > 0) {
  80.         return add_helper(r->right, key);
  81.     } else {
  82.         return r;
  83.     }
  84. }
  85.  
  86. template <typename T, class Compare>
  87. bool BSTDict<T, Compare>::find_helper(Node* r, T key, T& pred) {
  88.     if (r == NULL) {
  89.         return false; // No Node found
  90.     }
  91.    
  92.     int compare = Compare()(key, r->key);
  93.     if (compare < 0) {
  94.         return find_helper(r->left, key, pred);
  95.     } else if (compare > 0) {
  96.         return find_helper(r->right, key, pred);
  97.     } else {
  98.         pred = r->data;
  99.         return true;
  100.     }
  101. }
  102.  
  103. #endif
Advertisement
Add Comment
Please, Sign In to add comment