This blog is under construction

Sunday, 26 May 2013

C Program To Represent Binary Search Tree Using Arrays

See Also:
C Program To Represent Binary Search Tree Using Arrays
C Program To Perform Insertion, Deletion and Traversal In Binary Search Tree
C Program To Implement Binary Tree Traversals: In-order, Pre-order and Post-order
C Program To Implement Dictionary Using Binary Search Tree
C Program To Perform Searching in Binary Search Tree
C Program To Perform Insertion, Deletion And Traversal In Red Black Tree
C Program To Perform Insertion, Deletion and Traversal in AVL Tree
C Program To Perform Insertion, Deletion and Traversal In B-Tree
C Program To Implement Priority Queue Using Binary Heaps
Construct Binary Search Tree From In-order and Pre-order Traversal Outputs


Example Program To Represent Binary Search Tree Using Arrays(in C):



  #include <stdio.h>
  #include <stdlib.h>

  #define NODECOUNT 7

  struct bstNode {
        int data;
        struct bstNode *lchild, *rchild;
  };

  struct bstNode *root = NULL;
  int bstData[] = {100, 80, 120, 70, 90, 110, 130};
  int count = 0;

  /* construct binary search tree from Arrays */
  struct bstNode * implementBSTtree(int n) {
        struct bstNode *newnode;

        if (n >= NODECOUNT)
                return NULL;

        newnode = (struct bstNode *)malloc(sizeof (struct bstNode));
        /* node at position n - have its left child at the position (2 * n) + 1 */
        newnode->lchild = implementBSTtree((2 * n) + 1);
        newnode->data   = bstData[n];
        /* node at position n - have right child at the position (2 * n) + 2 */
        newnode->rchild = implementBSTtree((2 * n) + 2);
        return newnode;
  }

  /* Pre-Order traversal in Binary Search Tree */
  void preOrder(struct bstNode *myNode) {
        if (myNode) {
                printf("%d ", myNode->data);
                preOrder(myNode->lchild);
                preOrder(myNode->rchild);
        }
        return;
  }

    /* In-Order traversal in Binary Search Tree */
   void inOrder(struct bstNode * myNode) {
        if (myNode) {
                inOrder(myNode->lchild);
                printf("%d ", myNode->data);
                inOrder(myNode->rchild);
        }
        return;
  }

  /* Post-Order traversal in Binary Search Tree */
  void postOrder(struct bstNode *myNode) {
        if (myNode) {
                postOrder(myNode->lchild);
                postOrder(myNode->rchild);
                printf("%d ", myNode->data);
        }
        return;
  }

  int main() {
        int i = 0;
        printf("Data in Array:\n");
        while (i < NODECOUNT) {
                printf("%d ", bstData[i]);
                i++;
        }
        i = 0;
        root = implementBSTtree(i);
        printf("\nPre-Order:\n");
        preOrder(root);
        printf("\nIn-Order:\n");
        inOrder(root);
        printf("\nPost-Order:\n");
        postOrder(root);
        printf("\n");
        return 0;
  }



  Output: (Insertion, In-order, Pre-Order& Post-order traversal in BST - C Pgm)
  jp@jp-VirtualBox:~/$ ./a.out
  Data in Array:
  100 80 120 70 90 110 130 
  Pre-Order:
  100 80 70 90 120 110 130 
  In-Order:
  70 80 90 100 110 120 130 
  Post-Order:
  70 90 80 110 130 120 100 



No comments:

Post a Comment