/* A simple program to maintain a binary search tree of integers 
   file:  /home/walker/c/prog-mgmt/bst.c
*/

/* library for standard I/O */
#include <stdio.h>

/* the following are included since trees utilize both nodes and
   standard tree operations */
#include "node.h"
#include "bst-proc.h"

int main (void) {
  struct node * root = NULL;
  char name[strMax];
  char option;

  printf ("This program performs operations on a binary search tree\n\n");

  while (1) {
    printf ;
    printf ("Menu Options\n");
    printf ("    C - Count the number of names in the tree\n");
    printf ("    D - Delete a name from the tree\n");
    printf ("    F - Ddetermine if a specified name is in the tree\n");
    printf ("    I - Insert name into tree\n");
    printf ("    L - Print the leaves in the tree\n");
    printf ("    P - Print alphabetical listing of names\n");
    printf ("    Q - Quit\n");
    printf ("Enter desired option: ");
    scanf (" %c", &option);
    switch (option) 
      { case 'c':
        case 'C': printf ("The number of nodes in the tree is %d\n\n", 
                          countNodes(root));
          break;
        case 'd':
        case 'D':  
          printf ("Enter name to be deleted from the tree:  ");
          scanf (" %s", name);
          deleteName (&root, name);
          printf ("Deletion completed\n\n");
          break;
        case 'f':
        case 'F': 
          printf ("Search:  Enter name to be sought on the tree:  ");
          scanf (" %s", name);
          if (find(root, name))
             printf ("%s found on list\n\n", name);
          else printf ("%s NOT found on list\n\n", name);
          break;
        case 'i':
        case 'I': 
          printf ("Enter name to be added to the tree:  ");
          scanf (" %s", name);
          addName (&root, name);
          printf ("Insertion completed\n\n");
          break;
        case 'l':
        case 'L': 
          printf ("The following leaves are in the tree:\n");
          printLeaves (root);
          printf ("End of listing\n\n");
          break;
        case 'p':
        case 'P': 
          printf ("The following nodes are in the tree:\n");
          print (root, 0);
          printf ("End of listing\n\n");
          break;
        case 'q':
        case 'Q': ;
          printf ("Program terminated\n");
          return ;
          break;
        default: printf ("Invalid Option\n\n");
      }
  }
}
