Search Tutorials

Saturday, 20 April 2013

C code to implement Binary Search Tree using Father Field

/* C program to implement binary search tree using father field */

#include <stdio.h>

/* Parent field=father field*/
struct st
{
    int data;
    struct st* lc;
    struct st* rc;
    struct st* parent;
};

struct st *root=NULL;
struct st * find_leftmost(struct st *);
struct st * find_succ(struct st *, struct st *);
struct st * insert(struct st *,int);

int main()
{
  struct st* temp, *succ;
  int ch,ele,n,i;
  clrscr();
  while(1)
    {
    printf("\n\t\tMENU\n\t1.To insert\n\t2.For inorder Traversal\n\t0.To exit\n\t");
    scanf("%d",&ch);

        switch(ch)
        {
         case 1:
            printf("\n\tEnter number of elements\n\t");
            scanf("%d",&n);
            printf("\n\tEnter %d numbers to be inserted \n\t: ",n);
            for(i=0;i<n;i++)
            {
            printf("\n\t");
            scanf("%d",&ele);
            root=insert(root,ele);
            }
           break;
         case 2:
         clrscr();
         printf("\n\tInorder traversal using Father field\n\t");
         temp=find_leftmost(root);
         printf("%d ",temp->data);
         a:
         succ=find_succ(root,temp);
         if(succ !=  NULL)
         {
            printf("%d ",succ->data);
            temp=succ;goto a;
         }
        break;
        case 0:
        getch();
        exit(0);
        default:printf("Wrong choice pleas enter valid option  ");
        }
    }
}

/* Creating binary search tree using parent field */

struct st* insert(struct st* root, int ele)
{
    struct st *temp;
    if (root== NULL)
    {
    struct st* root = (struct st*)
    malloc(sizeof(struct st));
    root->data   = ele;
    root->lc   = NULL;
    root->rc  = NULL;
    root->parent = NULL;
    return(root);
    }
  else
  {
    if (ele <= root->data)
    {
     temp = insert(root->lc, ele);
     root->lc  = temp;
     temp->parent= root;
    }
    else
    {
    temp = insert(root->rc, ele);
    root->rc = temp;
    temp->parent = root;
    }
       return root;
  }
}

/* Finding left most node */

struct st * find_leftmost(struct st* node) {
    struct st* current = node;
    while (current->lc != NULL) {
    current = current->lc;
  }
  return current;
}

/* Finding succeeder of the node  */

struct st * find_succ(struct st *root, struct st *n)
{
   struct st *p;
    if( n->rc != NULL )
    return find_leftmost(n->rc);
    p= n->parent;
   while(p != NULL && n == p->rc)
   {
     n = p;
     p = p->parent;
   }
  return p;
}

/* Output of Binary Search Tree using Father Field Program */

C code to implement Binary Search Tree using father field
Output of BST using Father Field

C code to implement Binary Search Tree using father field
Output of BST using Father Field

For more related to Data Structure check List of Data Structure Programs. If you like this program, Please share and comment to improve this blog.

17 comments:

  1. Hey There. I found your post using msn. This is a really well written article. I will make sure to bookmark it and come back to read more of your useful information. Thanks for the post. I will certainly comeback. best seo company singapore

    ReplyDelete
  2. I really like your blog.. very nice colors & theme. Did you make this website yourself or did you hire someone to do it for you? Plz answer back as I'm looking to design my own blog and would like to know where u got this from. appreciate itsocial advertisement

    ReplyDelete
  3. I’m not that much of a internet reader to be honest but your sites really nice, keep it up! I'll go ahead and bookmark your website to come back down the road. All the best Parc clematis psf

    ReplyDelete
  4. excellent points altogether, you just gained a new reader. What would you suggest in regards to your post that you made a few days ago? Any positive? concrete cylinder

    ReplyDelete
  5. Hey! Quick question that's totally off topic. Do you know how to make your site mobile friendly? My blog looks weird when browsing from my iphone 4. I'm trying to find a template or plugin that might be able to correct this issue. If you have any recommendations, please share. Many thanks! dental crown cost singapore

    ReplyDelete
  6. Wonderful goods from you, man. I've understand your stuff previous to and you are just too wonderful. I really like what you have acquired here, really like what you are saying and the way in which you say it. You make it enjoyable and you still take care of to keep it wise. I can not wait to read much more from you. This is actually a wonderful site. SEM Singapore

    ReplyDelete
  7. Hello, i feel that i saw you visited my weblog thus i got here to “return the favor”.I am trying to find issues to improve my web site!I guess its adequate to use a few of your ideas!! infographics to explain

    ReplyDelete
  8. I'm truly enjoying the design and layout of your website. It's a very easy on the eyes which makes it much more enjoyable for me to come here and visit more often. Did you hire out a developer to create your theme? Exceptional work!facebook marketing singapore

    ReplyDelete
  9. I’m not that much of a online reader to be honest but your blogs really nice, keep it up! I'll go ahead and bookmark your site to come back down the road. Many thanks SEO  web design

    ReplyDelete
  10. I'm really enjoying the design and layout of your website. It's a very easy on the eyes which makes it much more pleasant for me to come here and visit more often. Did you hire out a designer to create your theme? Fantastic work! jade jewellery singapore

    ReplyDelete
  11. Hey there! Would you mind if I share your blog with my zynga group? There's a lot of folks that I think would really appreciate your content. Please let me know. Cheers corporate gifts suppliers singapore

    ReplyDelete
  12. Hello there! I could have sworn I've been to this website before but after checking through some of the post I realized it's new to me. Anyways, I'm definitely delighted I found it and I'll be book-marking and checking back frequently! fundraising in Singapore

    ReplyDelete
  13. Wow, fantastic weblog layout! How long have you been blogging for? you made blogging look easy. The total glance of your website is great, let alone the content! nose filler singapore

    ReplyDelete
  14. Do you have a spam issue on this site; I also am a blogger, and I was wanting to know your situation; we have created some nice methods and we are looking to swap methods with others, why not shoot me an e-mail if interested.top led grow lights

    ReplyDelete
  15. Hey there! I could have sworn I've been to this blog before but after checking through some of the post I realized it's new to me. Nonetheless, I'm definitely happy I found it and I'll be bookmarking and checking back often! social media engagement

    ReplyDelete
  16. Good day! Would you mind if I share your blog with my myspace group? There's a lot of folks that I think would really enjoy your content. Please let me know. Thankstoefl speaking sample answer

    ReplyDelete
  17. Hello! Quick question that's entirely off topic. Do you know how to make your site mobile friendly? My blog looks weird when browsing from my apple iphone. I'm trying to find a theme or plugin that might be able to resolve this issue. If you have any recommendations, please share. Thank you! best bed bug exterminator nyc

    ReplyDelete

Back to Top