Thread: Dictionay - implementation

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    3

    Question Dictionay - implementation

    I'm supposed to do a dictionary impplementation using a binary tree that's shown by pointers, this is what I did:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef char elementtype[30];
    
    typedef struct cell_tag{
            elementtype element;
            struct cell_tag *leftchild;
            struct cell_tag *rightchild;
            } celltype;
            
    typedef celltype *DICTIONARY;
    
    void MAKE_NULL(DICTIONARY *Ap){
         *Ap=NULL;
         return;
         }
    
    int MEMBER (elementtype x, DICTIONARY A){
        if (A == NULL) return 0;
        else if (strcmp(x, A->element)==0) return 1;
        else if (strcmp(x, A->element)<0) return (MEMBER(x,A->leftchild));
        else return (MEMBER(x,A->rightchild));
    }
    
    void INSERT (elementtype x, DICTIONARY *Ap){
         if (*Ap == NULL){
                 *Ap = (celltype*)malloc(sizeof(celltype));
                 strcpy((*Ap)->element, x);
                 (*Ap)->leftchild = (*Ap)->rightchild = NULL;
                 }
         else if (strcmp(x, (*Ap)->element)<0) INSERT(x, &((*Ap)->leftchild));
         else if (strcmp(x, (*Ap)->element)>0) INSERT(x, &((*Ap)->rightchild));
         }
         
    elementtype DELETE_MIN(DICTIONARY *Ap){
                celltype *temp;
                elementtype minel;
                if (((*Ap)->leftchild) == NULL){
                                       strcpy(minel, (*Ap)->element);
                                       temp = (*Ap);
                                       (*Ap) = (*Ap)->rightchild;
                                       free(temp);
                                       }
                else strcpy(minel, DELETE_MIN(&((*Ap)->leftchild)));
                return minel;
                }
                
    void DELETE (elementtype x, DICTIONARY *Ap){
         celltype *temp;
         if (*Ap != NULL)
            if (strcmp(x, (*Ap)->element)<0)
               DELETE(x, &((*Ap)->leftchild));
            else if (strcmp(x, (*Ap)->element)>0)
                 DELETE(x, &((*Ap)->rightchild));
            else if( ((*Ap)->leftchild==NULL) && ((*Ap)->rightchild == NULL)){
                 free(*Ap);
                 *Ap=NULL;
                 }
            else if( (*Ap)->leftchild == NULL){
                 temp=*Ap;
                 *Ap=(*Ap)->rightchild;
                 free(temp);
                 }
            else strcpy((*Ap)->element, DELETE_MIN( &((*Ap)->rightchild)));
            }
    this doesn't work, the problem is in the DELETE_MIN function, but I don't know how to fix it...Any help at all would be great...

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Moved to the C forum (previously under C++)

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by snezic View Post
    I'm supposed to do a dictionary impplementation using a binary tree that's shown by pointers, this is what I did:

    . . .

    this doesn't work, the problem is in the DELETE_MIN function, but I don't know how to fix it...Any help at all would be great...
    Ah, the 'ol "doesn't work" problem. I think we fixed that last week.
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    So add a main() to it, which creates a very simple tree (say 1 node), then try to delete that node.
    Does it work?

    What about 2 nodes - or even 3 nodes?

    What you need to do is create a really simple test framework around the code and then step through it all using the debugger.


    > elementtype minel;
    ..
    > return minel;
    Ah, but you have
    typedef char elementtype[30];

    You're returning a POINTER to a local array!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    3
    I tested the code and it works just fine, all except the DELETE_MIN function, I also know
    Quote Originally Posted by Salem View Post


    > elementtype minel;
    ..
    > return minel;
    Ah, but you have
    typedef char elementtype[30];

    You're returning a POINTER to a local array!
    but I don't know what to do to fix that, any hints...

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    One way would be to make a struct
    Code:
    typedef struct {
        char element[30];
    } elementtype;
    and then change all the references in the code, say
    strcpy((*Ap)->element.element, DELETE_MIN( &((*Ap)->rightchild)));

    Structs are returned by value, so this should work.
    But it is not the most efficient of things, so if you're doing this a lot, or your struct is much bigger, then another approach would be a good idea.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Jan 2010
    Posts
    3
    salem - Thank you sooo much,you saved my life...
    I did what you said and now everything is working just fine, finally

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rand() implementation
    By habert79 in forum C Programming
    Replies: 4
    Last Post: 02-07-2009, 01:18 PM
  2. implementation file
    By bejiz in forum C++ Programming
    Replies: 5
    Last Post: 11-28-2005, 01:59 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Pure virtual implementation, or not.
    By Eibro in forum C++ Programming
    Replies: 2
    Last Post: 03-19-2003, 08:05 PM
  5. hash table data structure implementation
    By sanju in forum C Programming
    Replies: 1
    Last Post: 09-27-2002, 05:06 AM