C Program to Print Ancestors of a given node in Binary Search Tree

By | May 1, 2017

Print Ancestors of Binary Search Tree 


Write a C Program to Print Ancestors of a given node in Binary Search Tree. Here’s simple Program to Print Ancestors of a given Binary Tree in C Programming Language.


What is Tree ?


In linear data structure, data is organized in sequential order and in non-linear data structure, data is organized in random order. Tree is a very popular data structure used in wide range of applications.

A tree data structure can be defined as follows…

  • Tree is a non-linear data structure which organizes data in hierarchical structure and this is a recursive definition.

A tree data structure can also be defined as follows…

  • Tree data structure is a collection of data (Node) which is organized in hierarchical structure and this is a recursive definition.

Every individual element is called as Node. Node in a tree data structure, stores the actual data of that particular element and link to next element in hierarchical structure.


Below is the source code for C Program to Print Ancestors of a given node in Binary Search Tree which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


 /* C Program to Print Ancestor of a given node in Binary Search Tree  */

#include<stdio.h>
#include<stdlib.h>
#define MAX 100

struct node
{
	struct node *lchild;
	int info;
	struct node *rchild;
};


struct node *insert(struct node *ptr, int ikey);
void display(struct node *ptr,int level);
void DisplayAncestors(struct node *root, int item);

int main()
{
	struct node *root=NULL,*ptr;
	int choice,k,item;

	while(1)
	{
		printf("\n");
		printf("1.Insert\n");
		printf("2.Display\n");
		printf("3.Print Ancestors\n");
		printf("4.Quit\n");
		printf("\nEnter your choice : ");
		scanf("%d",&choice);

		switch(choice)
		{

		case 1:
			printf("\nEnter the key to be inserted : ");
			scanf("%d",&k);
			root = insert(root, k);
			break;

        case 2:
            printf("\n");
            display(root,0);
            printf("\n");
            break;

        case 3:
            printf("\nEnter an item whose Ancestors to be displayed :: ");
            scanf("%d",&item);
            printf("\nAncestors of [ %d ] are :: ",item);
            DisplayAncestors(root, item);
            printf("\n");
            break;

        case 4:
			exit(1);

		 default:
			printf("\nWrong choice\n");

		}/*End of switch */
	}/*End of while */

	return 0;

}/*End of main( )*/


struct node *insert(struct node *ptr, int ikey )
{
	if(ptr==NULL)
	{
		ptr = (struct node *) malloc(sizeof(struct node));
		ptr->info = ikey;
		ptr->lchild = NULL;
		ptr->rchild = NULL;
	}
	else if(ikey < ptr->info)	/*Insertion in left subtree*/
		ptr->lchild = insert(ptr->lchild, ikey);
	else if(ikey > ptr->info)	/*Insertion in right subtree */
		ptr->rchild = insert(ptr->rchild, ikey);
	else
		printf("Duplicate key\n");
	return(ptr);
}/*End of insert( )*/

void display(struct node *ptr,int level)
{
	int i;
	if(ptr == NULL )/*Base Case*/
		return;
	else
    {
		display(ptr->rchild, level+1);
		printf("\n");
		for (i=0; i<level; i++)
			printf("    ");
		printf("%d", ptr->info);
		display(ptr->lchild, level+1);
	}
}/*End of display()*/


void DisplayAncestors(struct node *ptr, int item)
{
	if( ptr == NULL  || ptr->info==item )
		return;
	if( item < ptr->info )
		DisplayAncestors(ptr->lchild, item);
	else
		DisplayAncestors(ptr->rchild, item);
	printf(" %d ",ptr->info);
}/*End of DisplayAncestors()*/

OUTPUT : :


/* C Program to Print Ancestor of a given node in Binary Search Tree  */

1.Insert
2.Display
3.Print Ancestors
4.Quit

Enter your choice : 1

Enter the key to be inserted : 3

1.Insert
2.Display
3.Print Ancestors
4.Quit

Enter your choice : 1

Enter the key to be inserted : 2

1.Insert
2.Display
3.Print Ancestors
4.Quit

Enter your choice : 1

Enter the key to be inserted : 1

1.Insert
2.Display
3.Print Ancestors
4.Quit

Enter your choice : 1

Enter the key to be inserted : 0

1.Insert
2.Display
3.Print Ancestors
4.Quit

Enter your choice : 2


3
    2
        1
            0

1.Insert
2.Display
3.Print Ancestors
4.Quit

Enter your choice : 1

Enter the key to be inserted : 4

1.Insert
2.Display
3.Print Ancestors
4.Quit

Enter your choice : 1

Enter the key to be inserted : 5

1.Insert
2.Display
3.Print Ancestors
4.Quit

Enter your choice : 1

Enter the key to be inserted : 6

1.Insert
2.Display
3.Print Ancestors
4.Quit

Enter your choice : 1

Enter the key to be inserted : 7

1.Insert
2.Display
3.Print Ancestors
4.Quit

Enter your choice : 1

Enter the key to be inserted : 8

1.Insert
2.Display
3.Print Ancestors
4.Quit

Enter your choice : 1

Enter the key to be inserted : 9

1.Insert
2.Display
3.Print Ancestors
4.Quit

Enter your choice : 2


                        9
                    8
                7
            6
        5
    4
3
    2
        1
            0

1.Insert
2.Display
3.Print Ancestors
4.Quit

Enter your choice : 3

Enter an item whose Ancestors to be displayed :: 0

Ancestors of [ 0 ] are ::  1  2  3

1.Insert
2.Display
3.Print Ancestors
4.Quit

Enter your choice : 3

Enter an item whose Ancestors to be displayed :: 9

Ancestors of [ 9 ] are ::  8  7  6  5  4  3

1.Insert
2.Display
3.Print Ancestors
4.Quit

Enter your choice : 2


                        9
                    8
                7
            6
        5
    4
3
    2
        1
            0

1.Insert
2.Display
3.Print Ancestors
4.Quit

Enter your choice : 3

Enter an item whose Ancestors to be displayed :: 5

Ancestors of [ 5 ] are ::  4  3

1.Insert
2.Display
3.Print Ancestors
4.Quit

Enter your choice : 4

Process returned 1

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post…

Leave a Reply