C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 18th, 2003, 06:18 PM
zmerlinz's Avatar
zmerlinz zmerlinz is offline
:(){ :|:& };:
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Location: Aberystwyth, Wales, UK
Posts: 10 zmerlinz User rank is Corporal (100 - 500 Reputation Level)zmerlinz User rank is Corporal (100 - 500 Reputation Level)zmerlinz User rank is Corporal (100 - 500 Reputation Level)zmerlinz User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 6 h 14 m 39 sec
Reputation Power: 0
Send a message via MSN to zmerlinz
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);
}

Reply With Quote
  #2  
Old March 18th, 2003, 07:41 PM
infamous41md's Avatar
infamous41md infamous41md is offline
not a fan of fascism (n00b)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Feb 2003
Location: ct
Posts: 2,756 infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Days 11 h 4 m 29 sec
Reputation Power: 94
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

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 07:57 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Binary Tree: change a single character to a string

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap