-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDSA-lab7-BinarySearchTree
More file actions
156 lines (138 loc) · 4.11 KB
/
DSA-lab7-BinarySearchTree
File metadata and controls
156 lines (138 loc) · 4.11 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import java.util.Scanner;
public class BSTOperations {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
BinarySearchTree tree = new BinarySearchTree();
while (true) {
System.out.println("1.Insert 2.Search 3.Delete 4.Display 5.Exit");
int ch = sc.nextInt();
switch (ch) {
case 1:
System.out.print("Enter element to insert: ");
tree.insert(sc.nextInt());
break;
case 2:
System.out.print("Enter element to search: ");
System.out.println(tree.search(sc.nextInt()) ? "Found" : "Not Found");
break;
case 3:
System.out.print("Enter element to delete: ");
tree.delete(sc.nextInt());
break;
case 4:
System.out.println("Inorder: ");
tree.inorder();
System.out.println("Preorder: ");
tree.preorder();
System.out.println("Postorder: ");
tree.postorder();
break;
case 5:
System.exit(0);
}
}
}
}
class Node {
int key;
Node left, right;
Node(int key) {
this.key = key;
left = right = null;
}
}
class BinarySearchTree {
Node root;
void insert(int key) {
root = insertRec(root, key);
}
Node insertRec(Node root, int key) {
if (root == null) {
root = new Node(key);
return root;
}
if (key < root.key)
root.left = insertRec(root.left, key);
else if (key > root.key)
root.right = insertRec(root.right, key);
return root;
}
boolean search(int key) {
return searchRec(root, key);
}
boolean searchRec(Node root, int key) {
if (root == null)
return false;
if (root.key == key)
return true;
if (key < root.key)
return searchRec(root.left, key);
else
return searchRec(root.right, key);
}
void delete(int key) {
root = deleteRec(root, key);
}
Node deleteRec(Node root, int key) {
if (root == null)
return root;
if (key < root.key)
root.left = deleteRec(root.left, key);
else if (key > root.key)
root.right = deleteRec(root.right, key);
else {
// Node with only one child or no child
if (root.left == null)
return root.right;
else if (root.right == null)
return root.left;
// Node with two children: Get the inorder successor (smallest in the right subtree)
root.key = minValue(root.right);
// Delete the inorder successor
root.right = deleteRec(root.right, root.key);
}
return root;
}
// Utility function to find the minimum value in a tree (used for deletion)
int minValue(Node root) {
int minv = root.key;
while (root.left != null) {
minv = root.left.key;
root = root.left;
}
return minv;
}
void inorder() {
inorderRec(root);
System.out.println();
}
void inorderRec(Node root) {
if (root != null) {
inorderRec(root.left);
System.out.print(root.key + " ");
inorderRec(root.right);
}
}
void preorder() {
preorderRec(root);
System.out.println();
}
void preorderRec(Node root) {
if (root != null) {
System.out.print(root.key + " ");
preorderRec(root.left);
preorderRec(root.right);
}
}
void postorder() {
postorderRec(root);
System.out.println();
}
void postorderRec(Node root) {
if (root != null) {
postorderRec(root.left);
postorderRec(root.right);
System.out.print(root.key + " ");
}
}
}