-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHospitalTree.java
More file actions
executable file
·138 lines (105 loc) · 4.54 KB
/
Copy pathHospitalTree.java
File metadata and controls
executable file
·138 lines (105 loc) · 4.54 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
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package hospitalmanagementsystem;
/**
*
* @author egehanhatipoglu
*/
import java.util.ArrayList;
// Represents the organizational structure of the hospital using a General Tree (N-ary Tree).
// Unlike a Binary Tree, each node here can have an unlimited number of children (Departments).
public class HospitalTree {
// INNER CLASS: DepartmentTreeNode
// A node in the general tree. It contains the data (Department)
// and a list of children nodes (Sub-departments).
private class DepartmentTreeNode {
Department department;
ArrayList<DepartmentTreeNode> children;
public DepartmentTreeNode(Department department) {
this.department = department;
this.children = new ArrayList<>();
}
public void addChild(DepartmentTreeNode child) {
children.add(child);
}
}
private DepartmentTreeNode root;
// Constructor: Initializes the tree with the main "Hospital" entity as the Root Node.
// All departments will be added as children to this root.
public HospitalTree(String hospitalName) {
Department hospital = new Department(0, hospitalName, 999);
this.root = new DepartmentTreeNode(hospital);
}
// Adds a department to the tree.
// Currently, it adds departments directly under the root (Level 1 hierarchy).
public void addDepartmentToRoot(Department department) {
if (root != null && department != null) {
DepartmentTreeNode newNode = new DepartmentTreeNode(department);
root.addChild(newNode);
}
}
// Initiates the display of the organizational chart.
public void displayHierarchy() {
System.out.println("");
System.out.println(" HOSPITAL ORGANIZATION STRUCTURE ");
System.out.println("");
displayHierarchyRec(root, 0);
}
// Recursive Traversal (DFS): Prints the tree structure.
// Uses 'level' to calculate indentation, visually representing the parent-child relationships.
// This is a Depth-First Search approach.
private void displayHierarchyRec(DepartmentTreeNode node, int level) {
String indent = " ".repeat(level);
String prefix = level == 0 ? " - " : " - ";
System.out.println(indent + prefix + node.department.getName() +
" (" + node.department.getDoctorCount() + " doctors)");
for (DepartmentTreeNode child : node.children) {
displayHierarchyRec(child, level + 1);
}
}
public void showDepartmentDoctors(Department department) {
System.out.println("\n=== " + department.getName() + " Department Doctors ===");
if (department.getDoctors().isEmpty()) {
System.out.println(" No doctors in this department.");
return;
}
for (Doctor doctor : department.getDoctors()) {
System.out.println(" Dr. " + doctor.getFirstName() + " " +
doctor.getLastName() + " (Phone: " + doctor.getPhone() + ")");
}
}
public int getDepartmentCount() {
return countDepartments(root) - 1; // Root'u (hastaneyi) sayma
}
// Recursively counts the total number of nodes in the tree.
private int countDepartments(DepartmentTreeNode node) {
if (node == null) {
return 0; }
int count = 1;
for (DepartmentTreeNode child : node.children) {
count += countDepartments(child);
}
return count;
}
public int getTotalDoctorCount() {
return countDoctors(root);
}
// Recursive Aggregation: Traverses the entire tree to sum up data (doctor counts)
// from all nodes. Demonstrates how trees are useful for hierarchical data aggregation.
private int countDoctors(DepartmentTreeNode node) {
if (node == null) {
return 0;
}
int count = node.department.getDoctorCount();
for (DepartmentTreeNode child : node.children) {
count += countDoctors(child);
}
return count;
}
}