
March 18th, 2003, 06:18 PM
|
 |
:(){ :|:& };:
|
|
Join Date: Mar 2003
Location: Aberystwyth, Wales, UK
Posts: 10
  
Time spent in forums: 6 h 14 m 39 sec
Reputation Power: 0
|
|
Binary Tree: change a single character to a string
i have this code and currently you can enter and uppercase letter and it will enter it into the binary tree and sort it alphabetically, but i need to change it so that you can add a string of say 20 characters and it will sort it by the first letter of the string only, and at then end where you type > then enter, it will display all the strings in alphabetical order ??
thanks for anyone who can help
Code:
#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);
}
|