Repository navigation
Expand file tree
/
Copy pathBMain.java
More file actions
executable file
·125 lines (105 loc) · 4.32 KB
/
Copy pathBMain.java
File metadata and controls
executable file
·125 lines (105 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* This is the main Class for B-Tree implementation in Java.
* It creates a B-Tree Tree with order user value N.
* Then Lets the user do the following operations on the BTree Tree
* 1.Add more Keys to the Tree.
* 2.Print the entire Tree in preorder.
* 3.Delete a key from the leaf
* 4.Search for a Key and print the Node it belongs to
* The above menu is put in a while loop so that the User has a easy interface to Test or familiarize with the B-Tree.
*
* Authors: Jeremy Phelps and Krish Unnikannan
*/
import java.util.Scanner;
public class BMain
{
public static void main(String[] args)
{
Scanner input = new Scanner( System.in );
int n,n2,temp;
System.out.print("Enter the t of the Tree? ");
n=input.nextInt();
while ( n<2)
{
System.out.print("Please enter a integer greater than 1 : ");
n=input.nextInt();// User inputs the order of Tree and is assinged to N.
}
BTree tree = new BTree(n);// B-Tree Tree with order N is created.
// Initial Values are added to the Tree.. The user and Input any number of Values.
System.out.print("\n How many values do you want to enter?: ");
n2 = input.nextInt();
for ( int i=0;i< n2;i++)
{
System.out.print("\nEnter Value:");
System.out.println(i+1);
temp=input.nextInt();
tree.insert(tree,temp);
}
int choice,k;// Variables used to control the Repeated loop of the MENU.
boolean flag;
flag=true;
System.out.println("\tM\tE\tN\tU\n");
System.out.println("1. Enter more values in a Tree");
System.out.println("2. Print the whole Tree in preorder");
System.out.println("3. Search for a Key and print the Node it belongs to");
System.out.println("4. Delete a key from the leaf");
System.out.println("5. Exit");
while (flag)// This While loop runs as long as the user enters anything other than a 5.
{
System.out.print("\nPlease enter your choice::");
choice=input.nextInt();
if ( choice == 5)
{
System.out.printf("The program is exiting...,\n"
+"Thank you for using B-Tree implementation in Java\n"
+" Authors: Jeremy Phelps and Krish Unnikannan");
System.exit(0);
flag=false;
break;
}
else
{
switch(choice)
{
case 1: //If the User Enters 1 this case is executed and
//its function is to Enter more values in a Tree.
System.out.print("How many values do you want to enter?:");
n2=input.nextInt();
for ( int i=0;i< n2;i++)
{
System.out.print("\nEnter Value: ");
System.out.println(i+1);
temp=input.nextInt();
tree.insert(tree,temp);
}
break;
case 2: //If the User Enters 2 this case is executed and
//its function is to Print the whole Tree in preorder format.
tree.print(tree.root);
System.out.println();
break;
case 3: //If the User Enters 3 this case is executed and
//its function is to Delete a key from the leaf
System.out.println("What is the key you wish to search for:");
int key2=input.nextInt();
tree.SearchPrintNode(tree,key2);
break;
case 4: //If User Enters 4, this case is executed
//Its Function is to search for a Key and print the Node it belongs to
System.out.println("Enter a key to be deleted:");
int key=input.nextInt();
tree.deleteKey(tree,key);
System.out.println("Here is the tree printed in preorder after delete");
tree.print(tree.root);
break;
case 5: //If the User Enters 5, this case is executed and
//its function is to Exit
System.exit(0);
break;
default: // If the User enters a wrong choice, then this case is executed.
System.out.println("\nPlease enter a valid choice of 1,2,3 or 4\n");
break;
}//end of switch block
}//end of else block
}//end while(flag)
}//main
}//class