C Program to Print Right to Left Nodes at k level of Binary Tree

By | 08.05.2017

Print Right to Left Nodes at k level


Write a C Program to Print Right to Left Nodes at k level of Binary Tree. Here’s simple Program to print rightmost to leftmost node at k level of binary tree in C Programming Language.


What is Binary Search Tree ?


A Binary Search Tree (BST) is a Binary Tree in which all the nodes of Tree satisfies following  properties

  • The left sub-tree of a node has a key less than or equal to its parent node’s key.
  • The right sub-tree of a node has a key greater than to its parent node’s key.

Thus, Binary Search Tree divides all its sub-trees into two segments; the left sub-tree and the right sub-tree and can be defined as −

  • left_subtree (keys)  ≤  node (key)  ≤  right_subtree (keys)

Basic Operations on BST :


Following are the basic operations of a Binary Search Tree −

  • Search − This operation Searches an element in a tree.
  • Insert − This operation Inserts an element in a tree.
  • Pre-order Traversal − This operation Traverses a tree in a pre-order manner.
  • In-order Traversal − This operation Traverses a tree in an in-order manner.
  • Post-order Traversal − This operation Traverses a tree in a post-order manner.

Also Read : : C Program to Print Left to Right Nodes at k level of Binary Tree


Below is the source code for C Program to Print Right to Left Nodes at k level of Binary Tree which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/* C Program to Print Right to Left Nodes at k level of Binary Tree */

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

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 displayLevel_RtoL(struct node* root, int level);

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

        while(1)
        {
                printf("\n");
                printf("1.Insert Tree \n");
                printf("2.Display Tree \n");
                printf("3.Print Right to Left Nodes\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 the level to Print :: ");
            scanf("%d",&level);
            printf("\n");
            displayLevel_RtoL(root,level);
            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("\nDuplicate 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()*/


/*Displays nodes on a level from right to left*/
void displayLevel_RtoL(struct node* ptr, int level)
{
        if(ptr == NULL)
                return;
        if(level == 0)
                printf("%d ", ptr->info);
        displayLevel_RtoL(ptr->rchild, level-1);
        displayLevel_RtoL(ptr->lchild, level-1);
}/*End of displayLevel_RtoL()*/

OUTPUT : :


/* C Program to Print Right to Left Nodes at k level of Binary Tree */

1.Insert Tree
2.Display Tree
3.Print Right to Left Nodes
4.Quit

Enter your choice : 1

Enter the key to be inserted : 4

1.Insert Tree
2.Display Tree
3.Print Right to Left Nodes
4.Quit

Enter your choice : 1

Enter the key to be inserted : 2

1.Insert Tree
2.Display Tree
3.Print Right to Left Nodes
4.Quit

Enter your choice : 1

Enter the key to be inserted : 3

1.Insert Tree
2.Display Tree
3.Print Right to Left Nodes
4.Quit

Enter your choice : 1

Enter the key to be inserted : 1

1.Insert Tree
2.Display Tree
3.Print Right to Left Nodes
4.Quit

Enter your choice : 1

Enter the key to be inserted : 0

1.Insert Tree
2.Display Tree
3.Print Right to Left Nodes
4.Quit

Enter your choice : 1

Enter the key to be inserted : 5

1.Insert Tree
2.Display Tree
3.Print Right to Left Nodes
4.Quit

Enter your choice : 1

Enter the key to be inserted : 6

1.Insert Tree
2.Display Tree
3.Print Right to Left Nodes
4.Quit

Enter your choice : 1

Enter the key to be inserted : 7

1.Insert Tree
2.Display Tree
3.Print Right to Left Nodes
4.Quit

Enter your choice : 1

Enter the key to be inserted : 8

1.Insert Tree
2.Display Tree
3.Print Right to Left Nodes
4.Quit

Enter your choice : 2


                8
            7
        6
    5
4
        3
    2
        1
            0

1.Insert Tree
2.Display Tree
3.Print Right to Left Nodes
4.Quit

Enter your choice : 3

Enter the level to Print :: 0

4

1.Insert Tree
2.Display Tree
3.Print Right to Left Nodes
4.Quit

Enter your choice : 3

Enter the level to Print :: 1

5 2

1.Insert Tree
2.Display Tree
3.Print Right to Left Nodes
4.Quit

Enter your choice : 3

Enter the level to Print :: 2

6 3 1

1.Insert Tree
2.Display Tree
3.Print Right to Left Nodes
4.Quit

Enter your choice : 3

Enter the level to Print :: 3

7 0

1.Insert Tree
2.Display Tree
3.Print Right to Left Nodes
4.Quit

Enter your choice : 3

Enter the level to Print :: 4

8

1.Insert Tree
2.Display Tree
3.Print Right to Left Nodes
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…

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments