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.

27 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
  18. Hey! Would you mind if I share your blog with my facebook group? There's a lot of folks that I think would really appreciate your content. Please let me know. Thanks top advertising

    ReplyDelete
  19. Hello, i read your blog from time to time and i own a similar one and i was just wondering if you get a lot of spam remarks? If so how do you stop it, any plugin or anything you can advise? I get so much lately it's driving me crazy so any support is very much appreciated. website design agency

    ReplyDelete
  20. I am glad for commenting to make you understand of the helpful experience our daughter found using your site. She figured out a lot of issues, with the inclusion of what it's like to possess an amazing teaching style to let many more quite simply learn about a variety of complicated topics. You truly surpassed readers' expected results. Many thanks for displaying such important, trusted, educational and unique thoughts on that topic to Emily.
    seo agencies singapore

    ReplyDelete
  21. Excellent blog here! Also your site loads up very fast! What host are you using? Can I get your affiliate link to your host? I wish my web site loaded up as quickly as yours lol
    online advertisement

    ReplyDelete
  22. I loved as much as you will receive carried out right here. The sketch is of interest, your authored material stylish. nonetheless, you command get bought an nervousness over that you wish be delivering the following. unwell unquestionably come further formerly again as the exact same nearly a great deal often inside case you shield this increase. Helicopter Service

    ReplyDelete
  23. You could definitely see your enthusiasm in the work you write. The world hopes for more passionate writers like you who aren't afraid to say how they believe. Always follow your heart. chocolate gift Singapore

    ReplyDelete
  24. Hello! I know this is kind of off topic but I was wondering which blog platform are you using for this website? I'm getting fed up of Wordpress because I've had issues with hackers and I'm looking at options for another platform. I would be great if you could point me in the direction of a good platform.
    manage paid ads campaign on Google

    ReplyDelete
  25. Heya i am for the first time here. I came across this board and I find It really useful & it helped me out much. I hope to give something back and aid others like you helped me. 24 hour dental clinic singapore

    ReplyDelete
  26. Hmm is anyone else having problems with the pictures on this blog loading? I'm trying to figure out if its a problem on my end or if it's the blog. Any feed-back would be greatly appreciated.
    brand design singapore

    ReplyDelete
  27. I enjoy you because of every one of your work on this blog. Debby take interest in going through investigation and it's easy to see why. My spouse and i learn all regarding the compelling mode you render vital guidelines by means of this website and in addition increase participation from some others on that concern while our princess is studying a lot. Take advantage of the remaining portion of the new year. Your performing a really great job.
    seo singapore

    ReplyDelete

Back to Top