title: Binary Search Tree C implementation
body: <p>I recently wrote a fairly simple piece of code attempting to implement a Binary Search Tree in C with insertion, search, deletion and display operations. Unfortunately, the code does not seem to work.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  struct TreeNode {
      int data;
      struct TreeNode *leftChildNode;
      struct TreeNode *rightChildNode;
  };
  
  typedef struct TreeNode node;
  node *rootNode = NULL;
  
  void insertNode(int i, node *n) {
      if(n == NULL) {
          n = (node*)malloc(sizeof(node));
          n-&gt;leftChildNode = NULL;
          n-&gt;rightChildNode = NULL;
          n-&gt;data = i;
      }
      else 
      if(n-&gt;data == i)
          printf("\nThis value already exists in the tree!");
      else
      if(i &gt; n-&gt;data)
          insertNode(i, n-&gt;rightChildNode);
      else
          insertNode(i, n-&gt;leftChildNode);
      }
  
  void searchNode(int i, node *n) {
      if(n == NULL)
          printf("\nValue does not exist in tree!");
      else
      if(n-&gt;data == i)
          printf("\nValue found!");
      else
      if(i &gt; n-&gt;data)
          searchNode(i, n-&gt;rightChildNode);
      else
          searchNode(i, n-&gt;leftChildNode);
      }
  
  void deleteNode(int i, node *n) {
      if(n == NULL)
          printf("\nValue does not exist in tree!");
      else
      if(n-&gt;data == i) {
          if(n-&gt;leftChildNode == NULL)
              n = n-&gt;rightChildNode;
          else
          if(n-&gt;rightChildNode == NULL)
              n = n-&gt;leftChildNode;
          else {
              node *temp = n-&gt;rightChildNode;
              while(temp-&gt;leftChildNode != NULL)
                  temp = temp-&gt;leftChildNode;
              n = temp;
          }
      }
      else
      if(i &gt; n-&gt;data)
          deleteNode(i, n-&gt;rightChildNode);
      else
          deleteNode(i, n-&gt;leftChildNode);
      }
  
  void displayPreOrder(node *n) {
      if(n != NULL) {
          printf("%d ", n-&gt;data);
          displayPreOrder(n-&gt;leftChildNode);
          displayPreOrder(n-&gt;rightChildNode);
      }
  }
  
  void displayPostOrder(node *n) {
      if(n != NULL) {
          displayPostOrder(n-&gt;leftChildNode);
          displayPostOrder(n-&gt;rightChildNode);
          printf("%d ", n-&gt;data);
      }
  }
  
  void displayInOrder(node *n) {
      if(n != NULL) {
          displayInOrder(n-&gt;leftChildNode);
          printf("%d ", n-&gt;data);
          displayInOrder(n-&gt;rightChildNode);
      }
  }
  
  int main(void) {
      int ch, num, num1;
      do {
          printf("\nSelect a choice from the menu below.");
          printf("\n1. Insert a node.");
          printf("\n2. Search for a node.");
          printf("\n3. Delete a node.");
          printf("\n4. Display the Binary Search Tree.");
          printf("\nChoice: ");
          scanf("%d", &amp;ch);
          switch(ch) {
              case 1: printf("\nEnter an element: ");
                      scanf("%d", &amp;num);
                      //printf("YESYES");
                      insertNode(num, rootNode);
                      break;
  
              case 2: printf("\nEnter the element to be searched for: ");
                      scanf("%d", &amp;num);
                      searchNode(num, rootNode);
                      break;
  
              case 3: printf("\nEnter the element to be deleted: ");
                      scanf("%d", &amp;num);
                      deleteNode(num, rootNode);
                      break;
  
              case 4: printf("\nSelect an order for the elements to be display in.");
                      printf("\n1. Pre-order.");
                      printf("\n2. Post-order.");
                      printf("\n3. In-order.");
                      printf("\nChoice: ");
                      scanf("%d", &amp;num1);
                      switch(num1) {
                          case 1: printf("\nPre-order Display: ");
                                  displayPreOrder(rootNode);
                                  break;
  
                          case 2: printf("\nPost-order Display: ");
                                  displayPostOrder(rootNode);
                                  break;
  
                          case 3: printf("\nIn-order Display: ");
                                  displayInOrder(rootNode);
                                  break;
  
                          default: exit(0);
                      }
                      break;
  
              default: exit(0);
              }
          //printf("%d", rootNode-&gt;data);
          printf("\nIf you want to return to the menu, press 1.");
          printf("\nChoice: ");
          scanf("%d", &amp;num);
      } while(num == 1);
  
      return 0;
  }
  </code></pre>
  
  <p>In fact, notice the commented line <code>printf("%d", rootNode-&gt;data);</code> just before the <code>do-while</code> block in <code>main()</code> ends. If I uncomment this line, compile the program and run it, the program throws a segmentation fault. Could anyone tell me why this error is occurring and why the code as a whole isn't running? Thanks in advance.</p>