#include <stdio.h>
#include <stdlib.h>

typedef struct Nod
{
  struct Nod * Left;
  struct Nod * Right;
  char Data;
  char Count;
  /*want to see if i can add the other parts of the memberlist here*/
}
Node_Type, * P_Type;


/*
 *DECLARING THE PROTOTYPES HERE
 */

P_Type    Create_Node(char, int);
P_Type    Add_Item(P_Type, char);
void      Strip(P_Type);
void      Print(int, char);



int main(void)
{

  P_Type    Root;
  char      Choice, Letter, w[80];

  printf("\nEnter +L (or + any letter) to add letter to tree"
	 "\nEnter > to list alphabetically\n");

  Root = NULL;
  while(1) /*infinite loop */
    {
      Choice = Letter = 0;
      sscanf (gets (w), " %c %c ", & Choice, & Letter);
            if (Letter == 0 || ( Letter >= 'A' && Letter <= 'Z'))
      	{
	  switch (Choice)
	    {
	    case '+':
	      Root = Add_Item (Root, Letter);
	      break;

	    case '>':
	      Strip (Root);
	      printf("\n");
	    }
	 }
    }
  return 0;

}

/*Here are the functions that i declared earlier */

P_Type Create_Node (char D, int C)
{
  P_Type p;
  p = (P_Type) malloc (sizeof(Node_Type));
  p -> Left = NULL;
  p -> Right = NULL;
  p -> Data = D;
  p -> Count = C;
  return p;
}

P_Type Add_Item (P_Type p, char Letter)
{
  if (p == NULL)
      p = Create_Node (Letter, 1);
  else
    if (Letter < p -> Data)
      p -> Left = Add_Item ( p -> Left, Letter);
    else
      if (Letter > p -> Data)
	p -> Right = Add_Item (p -> Right, Letter);
      else
	p -> Count ++;
  return p;
}

void Strip (P_Type p)
{
  if (p != NULL)
    {
      Strip ( p -> Left);
      Print ( p -> Count, p -> Data);
      Strip (p -> Right);
    }
}

void Print (int i, char c)
{
  while (i--)
    printf("%c", c);
}
	  

 
