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

  • Home
  • SEARCH
  • 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 9260427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:53:21+00:00 2026-06-18T12:53:21+00:00

I played a bit with binary trees and built a menu which the user

  • 0

I played a bit with binary trees and built a menu which the user chooses whether to build a binary tree, insert a value to the binary tree he built or delete it, when I clicked on the creation of a tree. The tree is created, then the menu appeared again and now I want to put a number in this tree, but that variable is not set in the case, each case should set its variable ? Or you can use a global variable?
here its my code of the menu class.

import java.util.Comparator;
 import java.util.Scanner;


 public class TreeMenu {

 public static void main(String[] args) {

     while(true ){
         System.out.println("\n------------Menu-----------");
         System.out.println("1. Create Tree");
         System.out.println("2. Delete Tree");
         System.out.println("3. Insert Value INTO the tree");
         System.out.println("4. Exit ");
         System.out.println("Please Select Your Choice");

         Scanner choice = new Scanner(System.in); 
         int i = choice.nextInt();
         if(i>0 && i<=4){
         switch (i)
          {

            case 1:{

                System.out.println("Creating a Tree Please Wait........");
                Comparator comp = new IntegerComparator();
                BST tree1 = new BST(comp);
                break;
            }
            case 2:{
                System.out.println("You Chose TWO");
                break;
            }
            case 3:{
                Scanner Number = new Scanner(System.in); 
                 int num = Number.nextInt();
                 tree1.insert(num);

            }
            case 4:{
                System.exit(0);

            }

           }

          }
         else{
             System.out.println("There is no number in the menu like that "+i);
             System.exit(0); 

         }
     }

 }

 }

how i can use the same tree that created and insert into him values?
thanks

  • 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-18T12:53:22+00:00Added an answer on June 18, 2026 at 12:53 pm

    Declare tree1 as a private global variable as

      public class TreeMenu {
        private static BST tree1 = null;
        .....
    

    Now instantiate that tree1 inside the switch case 1, then you can use the same tree variable inside case 2

    A thing to note is you would need to do error checking in case 2 and 3, if tree1 == null, that would mean no tree has been created yet.

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

Sidebar

Related Questions

I've played around quite a bit now, and can't seem to get variables to
I just played around a bit with the animate() method. .animate( properties [, duration]
I've played with CVS a little bit and am not the most familiar with
Intrigued by this question , I have played a bit with parallel assignment with
I'm trying to create a dictionary in a functional way. I played a bit
I've played a bit with WPF. What I want to do is drag a
I have played a bit with the Network library, the simple form where the
I downloaded the source code of this tutorial and played a bit with it.
I've checked and played a bit with Microsoft Pex . It's a very handy
has anybody tried out the new 5.3.0 RC1 php release and played a bit

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.