Jump to content

Conversion

- - - - -

  • Please log in to reply
4 replies to this topic

#1
mr mike

mr mike

    Learning Programmer

  • Members
  • PipPipPip
  • 96 posts
Hello, this code implements a binary tree to display output. Can someone show an example how to implement an avl tree from this code?


#include <stdio.h>

#include <ctype.h>

#include <stdlib.h>

#include <string.h>


typedef struct LinkedLineList

{

   LinkedLineList *next;

   int line;

}LinkedLineList;


typedef struct AVLWordNode

{

   char *word;

   int height;

   LinkedLineList *firstline;

   struct AVLWordNode *left;

   struct AVLWordNode *right;

}AVLWordNode;

 

LinkedLineList *addLine(int line)

{

   LinkedLineList *newLine = malloc(sizeof *newLine);

   if(newLine != NULL)

   {

      newLine->line = line;

      newLine->next = NULL;

   }

   return new;

}


AVLWordTree *addNode(AVLWordTree **node, char *word, int line)

{

  AVLWordTree *wordloc = NULL;

  LinkedLineList *newline = NULL;

  AVLWordTree *temp = NULL;

  int diff = 0;


  if(node != NULL && word != NULL)

  {

    if(NULL == *node)

    {

      *node = malloc(sizeof **node);

      if(NULL != *node)

      {

        (*node)->left = NULL;

        (*node)->right = NULL;

        (*node)->word = dupstr(word);

        if((*node)->word != NULL)

        {

          (*node)->firstline = addLine(line);

          if((*node)->firstline != NULL)

          {

            wordloc = *node;

          }

        }

      }

    }

    else

    {

      diff = caseInsensitiveCmp((*node)->word, word);

      if(0 == diff)

      {

        newline = addLine(line);

        if(newline != NULL)

        {

          wordloc = *node;

          newline->next = (*node)->firstline;

          (*node)->firstline = newline;

        }

      }

      else if(0 < diff)

      {

        temp = *node;

        wordloc = addNode(&temp->left, word, line);

      }

      else

      {

        temp = *node;

        wordloc = addNode(&temp->right, word, line);

      }

    }

  }


  if(wordloc == NULL)

  {

    delWord(node);

  }


  return wordloc;

}


/* This is the issue */

/* need rightrotate, leftrotate, doubleleft and right, balance */

int getHeight(AVLWordTree *node)

{

   int lChild = (node->left) ? node->left->height + 1 : 0;

   int rChild = (node->right) ? node->right->height + 1 : 0;


   return(lChild >= rChild) ? lChild : rChild);

}


void delWord(AVLWordTree **node)

{

   AVLWordTree *temp = NULL;

   if(node != NULL)

   {

      if(*node != '\0')

      {

         if{(*node)->right != NULL)

         {

            temp = *node;

            delWord(&temp->right);

         }

         if((*node)->left != NULL)

         {

            temp = *node;

            delWord(&temp->left);

         }

         if((*node)->word != NULL)

         {

            free((*node)->word);

         }

         if((*node)->firstline != NULL)

         {

            delList((*node)->firstline;

         }

         free(*node);

         *node = NULL;

      }

   }

}


void delList(LinkedLineList *listnode)

{

   if(listnode != NULL)

   {

      delList(listnode->next);

      free(listnode);

   }

}


void printList(LinkedLineList *list)

{

   if(list != NULL)

   {

      printList(list->next);

      printf("%d ", list->line);

   }

}


void printTree(AVLWordNode *node)

{

   if(node != NULL)

   {

      printtree(node->left);

      printf("%s:  ", node->word);

      printlist(node->firstline);

      printf("\n");

      printtree(node->right);

   }

}


char *char_in_string(char *s, int c)

{

  char *p = NULL;


  if(s != NULL)

  {

    if(c != '\0')

    {

      while(*s != '\0' && *s != c)

      {

        ++s;

      }

      if(*s == c)

      {

        p = s;

      }

    }

  }


  return p;

}


char *tokenize(char **s, char *eliminate)

{

  char *p = NULL;

  char *q = NULL;


  if(s != NULL && *s != '\0' && eliminate != NULL)

  {

    

    while(NULL != char_in_string(eliminate, **s))

    {

      ++*s;

    }

    if(**s != '\0')

    {

      q = *s + 1;

      p = *s;

      while(*q != '\0' && NULL == char_in_string(eliminate, *q))

      {

        ++q;

      }


      *s = q + (*q != '\0');

      *q = '\0';

    }

  }


  return p;

}


int caseInsensitiveCmp(const char *s, const char *t)

{

  int diff = 0;

  char cs = 0;

  char ct = 0;


  while(diff == 0 && *s != '\0' && *t != '\0')

  {

    cs = tolower((unsigned char)*s);

    ct = tolower((unsigned char)*t);

    if(cs < ct)

    {

      diff = -1;

    }

    else if(cs > ct)

    {

      diff = 1;

    }

    ++s;

    ++t;

  }


  if(diff == 0 && *s != *t)

  {

    if(*s == '\0')

    {

      diff = -1;

    }

    else

    {

      diff = 1;

    }

  }


  return diff;

}


char *duplicateStr(char *s)

{

  char *p = NULL;


  if(s != NULL)

  {

    p = malloc(strlen(s) + 1);

    if(p)

    {

      strcpy(p, s);

    }

  }


  return p;

}


char *getLine(char *s, int n, FILE *fp)

{

   int c = 0;

   int done = 0;

   char *p = s;


   while(!done && --n > 0 && (c = getc(fp)) != EOF)

   {

     if((*p++ = c) == '\n')

     {

       done = 1;

     }

   }


   *p = '\0';


   if(EOF == c && p == s)

   {

     p = NULL;

   }

   else

   {

     p = s;

   }


   return p;

}


#define MAXLINE 8192


int main(void)

{

   static char buffer[MAXLINE] = {0};

   char *s = NULL;

   char *word = NULL;

   int line = 0;

   int giveup = 0;

   AVLWordTree *tree = NULL;


   char *eliminate = " \t\n\r\a\f\v!\"%^&*()_=+{}[]\\|/,.<>:;#~?1234567890";


   while(!giveup && getLine(buffer, sizeof buffer, stdin) != NULL)

   {

     ++line;

     s = buffer;

     while(!giveup && (word = tokenize(&s, eliminate)) != NULL)

     {

       if(NULL == addWord(&tree, word, line))

       {

          printf("Error adding data into memory. Giving up.\n");

          giveup = 1;

       }

      

     } 

   }


   if(!giveup)

   {

     printTree(tree);

   }


   delWord(&tree);


   return 0;

}

  


And maybe even condense it a little.
Thanks

Edited by mr mike, 26 March 2011 - 11:35 AM.
cleanup


#2
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,718 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
https://www.cse.ohio...lrotations.html

This should help you a bit.
sudo rm -rf /

#3
mr mike

mr mike

    Learning Programmer

  • Members
  • PipPipPip
  • 96 posts
@ dargueta
Thanks, the link helped alot. I am however still getting the following errors >>

progTwo.c:210: error: expected ‘)’ before ‘*’ token

progTwo.c: In function ‘delList’:

progTwo.c:257: error: expected ‘;’ before ‘}’ token

progTwo.c: In function ‘main’:

progTwo.c:433: warning: comparison between pointer and integer


from this

#include <stdio.h>

#include <ctype.h>

#include <stdlib.h>

#include <string.h>


typedef struct LinkedLineList

{

   struct LinkedLineList *next;

   int line;

}LinkedLineList;


typedef struct AVLWordNode

{

   char *word;

   int height;

   LinkedLineList *firstline;

   struct AVLWordNode *left;

   struct AVLWordNode *right;

}AVLWordNode;


void rotateLeft(AVLWordNode **node);

void doubleRotateLeft(AVLWordNode **node);

void rotateRight(AVLWordNode **node);

void doubleRotateRight(AVLWordNode **node);

void delList(LinkedLineList *listnode);

char *duplicateStr(char *s);

 

LinkedLineList *addLine(int line)

{

   LinkedLineList *newLine = malloc(sizeof *newLine);

   if(newLine != NULL)

   {

      newLine->line = line;

      newLine->next = NULL;

   }

   return newLine;

}


AVLWordNode *addNode(AVLWordNode **node, char *word, int line)

{

  AVLWordNode *wordPos = NULL;

  LinkedLineList *newline = NULL;

  AVLWordNode *temp = NULL;

  int diff = 0;


  if(node != NULL && word != NULL)

  {

    if(NULL == *node)

    {

      *node = malloc(sizeof **node);

      if(NULL != *node)

      {

        (*node)->left = NULL;

        (*node)->right = NULL;

        (*node)->word = duplicateStr(word);

        if((*node)->word != NULL)

        {

          (*node)->firstline = addLine(line);

          if((*node)->firstline != NULL)

          {

            wordPos = *node;

          }

        }

      }

    }

    else

    {

      diff = caseInsensitiveCmp((*node)->word, word);

      if(0 == diff)

      {

        newline = addLine(line);

        if(newline != NULL)

        {

          wordPos = *node;

          newline->next = (*node)->firstline;

          (*node)->firstline = newline;

        }

      }

      else if(0 < diff)

      {

        temp = *node;

        wordPos = addNode(&temp->left, word, line);

        balance(wordPos);

        return wordPos;

      }

      else

      {

        temp = *node;

        wordPos = addNode(&temp->right, word, line);

        balance(wordPos);

        return wordPos);

      }

    }

  }


  if(wordPos == NULL)

  {

    deleteword(node);

  }


  return NULL;

}


void balance(AVLWordNode **node)

{

   int bal;

   int hleft, hright;

   

   bal = getBalance(*node);

   if(bal < -1)

   {

      bal = getBalance((*node)->left);

      

      if(bal > 0)

      {

         doubleRotateRight(node);

      } else {

          rotateRight(node);

      }

   } else if(bal > 1) {

         bal = getBalance((*node)->right);

         if(bal < 0)

         {

            doubleRotateLeft(node);

         } else {

            rotateLeft(node);

         }

   }

   hleft = getHeight((*node)->left);

   hright = getHeight((*node)->right);

   (*node)->height = (hleft > hright ? hleft : hright) + 1;

}


int getBalance(AVLWordNode *node)

{

   int bal = 0;

   if(node->left)

   {

      bal -= node->left->height;

   }

   if(node->right)

   {

      bal += node->right->height;

   }

   return bal;

}


void rotateRight(AVLWordNode **node) 

{


   AVLWordNode *a, *b, *c, *d, *e;

   int left, right;


   a = (*node)->left->left;

   b = (*node)->left->right;

   c = (*node)->right;

   d = (*node)->left;

   e = (*node);


   (*node) = d;

   (*node)->right = e;

   (*node)->left = a;

   (*node)->right->left = b;

   (*node)->right->right = c;


   left = getHeight(b);

   right = getHeight(b);

   e->height = (left > right ? left : right) + 1;

   d->height = e->height + 1;


}


void rotateLeft(AVLWordNode **node) 

{


   AVLWordNode *a, *b, *c, *d, *e;

   int left, right;


   a = (*node)->left;

   b = (*node)->right->left;

   c = (*node)->right->right;

   d = (*node);

   e = (*node)->right;


   *node = e;

   (*node)->left = d;

   (*node)->left->left = a;

   (*node)->left->right = b;

   (*node)->right = c;


   left = getHeight(a);

   right = getHeight(b);

   d->height = (left > right ? left : right) + 1;

   e->height = d->height + 1;


}



void doubleRotateRight(AVLWordNode **node) 

{


   rotateLeft(&(*node)->left);

   rotateRight(node);


}


void doubleRotateLeft(AVLWordNode **node) {


   rotateRight(&(*node)->right);

   rotateLeft(node);


}      


int getHeight(AVLTreeNode *node) {

   if(node) {

      return node->height;

   } else {

      return 0;

   }

}


void delWord(AVLWordNode **node)

{

   AVLWordNode *temp = NULL;

   if(node != NULL)

   {

      if(*node != '\0')

      {

         if((*node)->right != NULL)

         {

            temp = *node;

            delWord(&temp->right);

         }

         if((*node)->left != NULL)

         {

            temp = *node;

            delWord(&temp->left);

         }

         if((*node)->word != NULL)

         {

            free((*node)->word);

         }

         if((*node)->firstline != NULL)

         {

            delList((*node)->firstline);

         }

         free(*node);

         *node = NULL;

      }

   }

}


void delList(LinkedLineList *listnode)

{

   if(listnode != NULL)

   {

      delList(listnode->next);

      free(listnode);

   } else {

      free(listnode)

   }

}


void printList(LinkedLineList *list)

{

   if(list != NULL)

   {

      printList(list->next);

      printf("%d ", list->line);

   }

}


void printTree(AVLWordNode *node)

{

   if(node != NULL)

   {

      printtree(node->left);

      printf("%s:  ", node->word);

      printlist(node->firstline);

      printf("\n");

      printtree(node->right);

   }

}


char *charToString(char *s, int c)

{

  char *p = NULL;


  if(s != NULL)

  {

    if(c != '\0')

    {

      while(*s != '\0' && *s != c)

      {

        ++s;

      }

      if(*s == c)

      {

        p = s;

      }

    }

  }


  return p;

}


char *tokenize(char **s, char *eliminate)

{

  char *p = NULL;

  char *q = NULL;


  if(s != NULL && *s != '\0' && eliminate != NULL)

  {

    

    while(NULL != charToString(eliminate, **s))

    {

      ++*s;

    }

    if(**s != '\0')

    {

      q = *s + 1;

      p = *s;

      while(*q != '\0' && NULL == charToString(eliminate, *q))

      {

        ++q;

      }


      *s = q + (*q != '\0');

      *q = '\0';

    }

  }


  return p;

}


int caseInsensitiveCmp(const char *s, const char *t)

{

  int diff = 0;

  char cs = 0;

  char ct = 0;


  while(diff == 0 && *s != '\0' && *t != '\0')

  {

    cs = tolower((unsigned char)*s);

    ct = tolower((unsigned char)*t);

    if(cs < ct)

    {

      diff = -1;

    }

    else if(cs > ct)

    {

      diff = 1;

    }

    ++s;

    ++t;

  }


  if(diff == 0 && *s != *t)

  {

    

    if(*s == '\0')

    {

      diff = -1;

    }

    else

    {

      diff = 1;

    }

  }


  return diff;

}


char *duplicateStr(char *s)

{

  char *p = NULL;


  if(s != NULL)

  {

    p = malloc(strlen(s) + 1);

    if(p)

    {

      strcpy(p, s);

    }

  }


  return p;

}


char *getLine(char *s, int n, FILE *fp)

{

   int c = 0;

   int done = 0;

   char *p = s;


   while(!done && --n > 0 && (c = getc(fp)) != EOF)

   {

     if((*p++ = c) == '\n')

     {

       done = 1;

     }

   }


   *p = '\0';


   if(EOF == c && p == s)

   {

     p = NULL;

   }

   else

   {

     p = s;

   }


   return p;

}


#define MAXLINE 9321


int main(void)

{

   static char buf[MAXLINE] = {0};

   char *s = NULL;

   char *word = NULL;

   int line = 0;

   int quit = 0;

   AVLWordNode *tree = NULL;


   char *eliminate = " \t\n\r\a\f\v!\"%^&*()_=+{}[]\\|/,.<>:;#~?1234567890";


   while(!quit && getLine(buf, sizeof buf, stdin) != NULL)

   {

     ++line;

     s = buf;

     while(!quit && (word = tokenize(&s, eliminate)) != NULL)

     {

       if(NULL == addword(&tree, word, line))

       {

          printf("Error adding data into memory. Giving up.\n");

          quit = 1;

       }

      

     } 

   }


   if(!quit)

   {

     printTree(tree);

   }


   delWord(&tree);

   return 0;

}



#4
mr mike

mr mike

    Learning Programmer

  • Members
  • PipPipPip
  • 96 posts
Fixed...
No Help needed....
For now.....:loading:

#5
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,718 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Okay, great! Keep us posted (ha-ha) on any other problems you might have.
sudo rm -rf /




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users