Tuesday, 15 January 2013

java - BinaryTree - with switch case -



java - BinaryTree - with switch case -

played bit binary trees , built menu user chooses whether build binary tree, insert value binary tree built or delete it, when clicked on creation of tree. tree created, menu appeared 1 time again , want set number in tree, variable not set in case, each case should set variable ? or can utilize global variable? here code of 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 tree"); system.out.println("4. exit "); system.out.println("please select choice"); scanner selection = new scanner(system.in); int = choice.nextint(); if(i>0 && i<=4){ switch (i) { case 1:{ system.out.println("creating 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 no number in menu "+i); system.exit(0); } } } }

how can utilize same tree created , insert him values? thanks

declare tree1 private global variable as

public class treemenu { private static bst tree1 = null; .....

now instantiate tree1 within switch case 1, can utilize same tree variable within case 2

a thing note need error checking in case 2 , 3, if tree1 == null, mean no tree has been created yet.

java switch-statement binary-tree

No comments:

Post a Comment