Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8917417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:28:42+00:00 2026-06-15T05:28:42+00:00

Hello I have a small binary tree program in c that I’m working on

  • 0

Hello I have a small binary tree program in c that I’m working on and after an operation is performed the main menu is printed twice before allowing the next input to be scanned. Any ideas to prevent this?

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

struct btree{
    int id, val;
    struct btree *left, *right;
};

typedef struct btree node;

void myparent(node *tree, int myid, node **parent){
    if(tree->id==(myid/2))
            *parent = tree;
    if(tree->left)
            myparent(tree->left, myid, parent);
    if(tree->right)
            myparent(tree->right, myid, parent);
}

void insert(node **tree, node *item){
    node *parent;
    if(item->id==1)
            *tree=item;
    else{
            myparent(*tree, item->id,&parent);
            if((item->id)%2)
                    parent->right=item;
            else
                    parent->left=item;
    }
}
void preorder_print(node *tree){
if (tree!=NULL){
    printf("    %d\n",tree->val);
    printf("\t");
    preorder_print(tree->left);
    printf("\n");
    preorder_print(tree->right);
}
}
void inorder_print(node *tree){
if (tree!= NULL){
    inorder_print(tree->left); 
    printf("%d ", tree->val); 
    inorder_print(tree->right); 
}
}


void printout(node *tree){
    if(tree->left)
            printout(tree->left);
    printf("%d\n", tree->val);
    if(tree->right)
            printout(tree->right);
}

main(){
    char opn;
    node *root, *curr;
    int idcount=1, inp=0;
    root=NULL;
    //input 9999 is the exit point
    do{
    printf("\n ### Binary Tree Operations ### \n\n");
    printf("\n Enter one of the following Operations:\n a- Add\n e- Empty\n i- List IN  ORDER\n r- List PRE-ORDER\n p- List POST ORDER\n q- Quit\n");

    scanf("%c", &opn);

    switch (opn) {
        case 1:
        case 'a':
        case 'A':

        printf("\nEnter a Node>");
            scanf("%d",&inp);
            curr=(node*)malloc(sizeof(node));
            curr->val=inp;
            curr->id=idcount++;
            curr->left = curr->right = NULL;
            insert(&root, curr);
            printf("The number was inserted\n");
            break;

        case 2:
        case 'e':
        case 'E':
            printf("The value was deleted\n");
            break;

        case 3: 
        case 'i':
        case 'I':
            printf("IN ORDER Tree: \n");
            inorder_print(root);
            break;

        case 4:
        case 'r':
        case 'R':
             printf("PRE ORDER Tree: \n");
             preorder_print (root); 
             break;     

        case 5:
        case 'p':
        case 'P':
            printf("POST ORDER Tree: \n");
            printout(root);  
            break;

        case 6:
        case 'q':
        case 'Q':
            printf("\n\n Terminating \n\n");
            exit(1);

        default:
            printf("Invalid Opition \n \n");
            break;

    }
    printf("\n Press any key to continue . . .");
} while (opn != 'q');



 /*   printf("\n Entered Binary Tree is \n");
    printout(root);
    return 0; */
}

A menu is printed out with an invalid option entered even though nothing was inputed
so output looks like:

 ### Binary Tree Operations ### 


 Enter one of the following Operations:
 a- Add
 e- Empty
 i- List IN ORDER
 r- List PRE-ORDER
 p- List POST ORDER
 q- Quit
a

Enter a Node>1
The number was inserted

 Press any key to continue . . .
 ### Binary Tree Operations ### 


 Enter one of the following Operations:
 a- Add
 e- Empty
 i- List IN ORDER
 r- List PRE-ORDER
 p- List POST ORDER
 q- Quit
Invalid Opition 


 Press any key to continue . . .
 ### Binary Tree Operations ### 


 Enter one of the following Operations:
 a- Add
 e- Empty
 i- List IN ORDER
 r- List PRE-ORDER
 p- List POST ORDER
 q- Quit
i
IN ORDER Tree: 
2 4 1 3 5 
Press any key to continue . . .
 ### Binary Tree Operations ### 


 Enter one of the following Operations:
 a- Add
 e- Empty
 i- List IN ORDER
 r- List PRE-ORDER
 p- List POST ORDER
 q- Quit
Invalid Opition 


 Press any key to continue . . .
 ### Binary Tree Operations ### 


 Enter one of the following Operations:
 a- Add
 e- Empty
 i- List IN ORDER
 r- List PRE-ORDER
 p- List POST ORDER
 q- Quit
q


 Terminating 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T05:28:43+00:00Added an answer on June 15, 2026 at 5:28 am

    scanf will populate opn with a line feed (\n) with the above code if “nothing was inputed”. Since you don’t have a case for that, it will be handled by the default.

    Even if a character is inputed, the line feed will stay in the buffer for the next scanf call.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hello I have a small question in objective-C memory management. I know that if
I have written a small program just like hello the world , everything was
Hello I have an array that looks like this after passing it through uksort()
Hello i am working about a articles script i have a small problem in
I have a small Python program that should react to pushing the up button
Hello to all that read I have a small problem(or it could be large!),
Hello I have a small List<string> that I want to maintain across postbacks on
Hello everybody, I have a small code in PHP that I would like to
I have problems generating a small hello-world program in C with ld as linker.
hello all I have a small dialog which I created dynamically, which has a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.