title: How to create a Binary Search Tree for strings instead of integers?
body: <p>There is this piece of code in my C book that explains how the BST works for integers, but my homework is about BST for strings. </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;stdbool.h&gt;
  
  struct node
  {
      char data;
      struct node *left;
      struct node *right;
  };
  
  struct node *root;
  struct node *newnode(char name);
  struct node *find(char key);
  struct node *insert(char name);
  void display(struct node *ptr);
  bool rm(char key);
  struct node *find_left_most(struct node *rt);
  struct node *find_right_most(struct node *rt);
  
  int main(void)
  {
      char ch;
      char *a;
      a=malloc(100*sizeof(char));
      struct node *new;
      root=NULL;
      while(1)
      {
          printf("0-&gt;EXIT 1-&gt;Add name : 2-&gt;Search name : ");
          printf("3-&gt;Delete name : 4-&gt;BST display :\n");
  
          ch=getch();
          switch(ch)
          {
              case '0':
                      exit(0);
              case '1':
                      printf("name:");
                      scanf("%s",&amp;a);
                      new=insert(a);
                      if (root==NULL) root=new;
                      if(new==NULL)
                          puts("There is no memory available");
                      break;
              case '2':
                      printf("name:");
                      scanf("%s",&amp;a);
                      new=find(a);
                      if(new!=NULL)
                          printf("It has been found\n");
                      else
                          printf("There is no such name\n");
                      break;
              case '3':
                      printf("name:");
                      scanf("%s",&amp;a);
                      rm(a);
                      break;
              case '4':
                      display(root);
                      puts("");
                      break;
              default:
                      puts("Wrong key");
                      break;
          }
      }
      free(a);
      return 0;
  }
  
  struct node *newnode(char name)
  {
      struct node *neos;
      neos=malloc(sizeof(struct node));
      neos-&gt;data = name;
      neos-&gt;left = NULL;
      neos-&gt;right = NULL;
      return(neos);
  }
  
  void display(struct node *ptr)
  {
      if (ptr == NULL) return;
      display(ptr-&gt;left);
      printf("%s ", ptr-&gt;data);
      display(ptr-&gt;right);
  }
  
  struct node *find(char key)
  {
      struct node *current;
      current=root;
      while(current-&gt;data != key)
      {
          if(key &lt; current-&gt;data)
              current = current-&gt;left;
          else
              current = current-&gt;right;
          if(current == NULL)
              return NULL;
      }
      return current;
  }
  
  struct node *insert(char name)
  {
      struct node *next,*current,*ptr;
      bool isleft;
      next=current=root;
      ptr=newnode(name);
      if (root == NULL)
      {
          return ptr;
      }
      while(1)
      {
          if(name &lt; current-&gt;data)
          {
              next = current-&gt;left;
              isleft=true;
          }
          else
          {
              next = current-&gt;right;
              isleft=false;
          }
          if(next == NULL)
          {
              if(isleft)
                  current-&gt;left=ptr;
              else
                  current-&gt;right=ptr;
              return ptr;
          }
          current=next;
      }
  }
  
  bool rm(char key)
  {
      struct node *current;
      struct node *parent;
      bool isLeftChild = true;
      current=parent=root;
      while(current-&gt;data != key)
      {
          parent = current;
          if(key &lt; current-&gt;data)
          {
              isLeftChild = true;
              current = current-&gt;left;
          }
          else
          {
              isLeftChild = false;
              current = current-&gt;right;
          }
          if(current == NULL)
              return false;
      }
  
      if(current-&gt;left==NULL &amp;&amp; current-&gt;right==NULL)
      {
          if(current == root)        
              root = NULL;           
          else if(isLeftChild)
              parent-&gt;left = NULL;   
          else                       
              parent-&gt;right = NULL;
      }
  
      else if(current-&gt;right==NULL)
          if(current == root)
              root = current-&gt;left;
          else if(isLeftChild)
              parent-&gt;left = current-&gt;left;
          else
              parent-&gt;right = current-&gt;left;
  
      else if(current-&gt;left==NULL)
          if(current == root)
              root = current-&gt;right;
          else if(isLeftChild)
              parent-&gt;left = current-&gt;right;
          else
              parent-&gt;right = current-&gt;right;
      else
      {
          struct node *successor,*temp,*old_root;
          if(current == root)
          {
              temp=root-&gt;left;
              successor=find_left_most(root-&gt;right);
              root=root-&gt;right;
              successor-&gt;left=temp;
          }
  
          else if(isLeftChild)
          {
              successor=find_left_most(current-&gt;right);
              successor-&gt;left=current-&gt;left;
              parent-&gt;left = current-&gt;right;
          }
          else
          {
              successor=find_right_most(current-&gt;left);
              successor-&gt;right=current-&gt;right;
              parent-&gt;right = current-&gt;left;
          }
      }
      free(current);
      return true;
  }
  
  //εντόπισε τον τελευταίο αριστερά κόμβο του υποδένρδου
  struct node *find_left_most(struct node *rt)
  {
      if(rt==NULL) return NULL;
      while(rt-&gt;left!=NULL)
      {
          rt=rt-&gt;left;
      }
      return rt;
  }
  
  //εντόπισε τον τελευταίο δεξιά κόμβο του υποδένρδου
  struct node *find_right_most(struct node *rt)
  {
      if(rt==NULL) return NULL;
      while(rt-&gt;right!=NULL)
      {
          rt=rt-&gt;right;
      }
      return rt;
  }
  </code></pre>
  
  <p>I am pretty sure i messed up with memory management and that is the main reason my program crashes when i try to display the Tree . More specifically i think that the way i treat "a" variable is wrong , but i don't know what to do ...</p>