-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathProblem_18.java
More file actions
73 lines (60 loc) · 2.01 KB
/
Copy pathProblem_18.java
File metadata and controls
73 lines (60 loc) · 2.01 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
// Given n appointments, find the conflicting appointments
class Problem_18 {
// A binary tree node
static class Node {
int data;
Node left, right;
Node(int item) {
data = item;
left = right = null;
}
}
Node root;
// Function to insert a new node with given key in BST
static Node insert(Node node, int key) {
// If the tree is empty, return a new node
if (node == null) {
return new Node(key);
}
// Otherwise, recur down the tree
if (key < node.data) {
node.left = insert(node.left, key);
} else {
node.right = insert(node.right, key);
}
return node;
}
// Function to check for conflicts in appointments
static boolean hasConflict(Node node, int start, int end) {
// Base case
if (node == null) {
return false;
}
// If current node's data lies within the range, there is a conflict
if (node.data >= start && node.data <= end) {
return true;
}
// If current node's data is less than start, check right subtree
if (node.data < start) {
return hasConflict(node.right, start, end);
}
// If current node's data is greater than end, check left subtree
return hasConflict(node.left, start, end);
}
// Driver method to test above methods
public static void main(String args[]) {
Node root = null;
int[] appointments = {10, 20, 30, 40, 50};
// Insert appointments into BST
for (int appt : appointments) {
root = insert(root, appt);
}
int newStart = 25;
int newEnd = 35;
if (hasConflict(root, newStart, newEnd)) {
System.out.println("Conflict detected for appointment [" + newStart + ", " + newEnd + "]");
} else {
System.out.println("No conflict for appointment [" + newStart + ", " + newEnd + "]");
}
}
}