#1
  1. :(){ :|:& };:
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Location
    Aberystwyth, Wales, UK
    Posts
    10
    Rep Power
    0

    Unhappy 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);
    }
  2. #2
  3. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    1. u would want to include<cstring>
    2. you could then use strcmp() to compare strings to find which is greater.
    3.then to step thru the tree u would write a recursive function that would traverse down the left side, then recurse printing out values up to the root. then traverse teh right subtree and recurse.

    -i did my final project on this in CS last year, but my hard drive got wiped out so i cant give u any sample code, sorry!

    edit: i can tell u the recursive function woud look something like this if my memory serves me right:D

    void inOrderTraversal(NODETYPE *ptr) <--pass the root
    {
    if (ptr != 0)
    {
    inOrderTraversal(ptr->leftPtr);
    cout << ptr ->data << " ";
    inOrderTraversal(ptr->rightPtr);
    }
    }
    Last edited by infamous41md; March 18th, 2003 at 08:57 PM.

IMN logo majestic logo threadwatch logo seochat tools logo