
//
// Dictionary example
//

#include <iostream>
#include <string>

#include "queue.h"
#include "btree.h"
#include "bstree.h"

// Function used by traversals to print information
void Visit  (string& a, int d) { cout << a << " "; }

// Pretty printing function of a tree structure
void Visitd (string& a, int d) 
{ 
  for (d *= 4; d > 0; d--) 
    cout << " "; 
  cout << a << endl; 
}


//
// Container class
//
class dict {
public:
  int data;
  string key;

  dict () { }
  dict (string a, int d) { key = a; data = d; } 

  dict& operator= (dict& b) 
    { this->key = b.key; this->data = b.data; return b; }

  friend bool operator< (dict& a, dict& b) { return a.key < b.key; }
  friend bool operator> (dict& a, dict& b) { return a.key > b.key; }
  friend bool operator<= (dict& a, dict& b) { return a.key <= b.key; }
  friend bool operator>= (dict& a, dict& b) { return a.key >= b.key; }
  friend bool operator!= (dict& a, dict& b) { return a.key != b.key; }
  friend bool operator== (dict& a, dict& b) { return a.key == b.key; }

};

// 
//
//
int
main (int argn, char *argv[])
{
  int N;
  BSTree<dict> *bst = new BSTree<dict>;

  //
  // Insert items in a dictionary 
  //
  dict *i = new dict;
  // Insert first item (key: first, data: 1)
  i->key = "first"; i->data = 1;
  bst->Insert (i);
  // Insert first item (key: second, data: 2)
  i->key = "second"; i->data = 2;
  bst->Insert (i);
  delete i;

  //
  // Delete items from the dictionary
  //
  dict *s = new dict;
  // Search key: first
  s->key = "second";
  if (bst->Delete (s))
    cout << s->key << ":" << s->data << endl;
  if (bst->Delete (s))
    cout << s->key << ":" << s->data << endl;
  else
    cout << s->key << " not found" << endl;
  // Search key: second
  s->key = "first"; 
  if (bst->Search (s))
    cout << s->key << ":" << s->data << endl;
  delete s;

  // Delete the "dictionary"
  delete bst;

  return 0;
}


