构建一个用二叉链表存储一个公司组织机构的相关数据的程序,假设该公司的下属分支都只有两个分支,且公司的上下级关系为第一层级为总经理,第二层级为副总经理、第三层级为部门经理、第四层级为科室。其中设每个数据
时间: 2023-04-27 22:01:20 浏览: 114
节点包含以下信息:员工姓名、员工职位、员工工资、员工入职时间、员工所在部门、员工所在科室、员工直接上级。程序需要实现以下功能:
1. 添加员工节点
2. 删除员工节点
3. 修改员工信息
4. 查询员工信息
5. 输出公司组织结构图
程序实现思路:
1. 定义二叉链表节点结构体,包含员工信息和左右子节点指针。
2. 定义二叉链表根节点指针,初始化为空。
3. 实现添加员工节点功能,根据员工职位和入职时间插入到合适的位置。
4. 实现删除员工节点功能,根据员工姓名查找节点并删除。
5. 实现修改员工信息功能,根据员工姓名查找节点并修改信息。
6. 实现查询员工信息功能,根据员工姓名查找节点并输出信息。
7. 实现输出公司组织结构图功能,采用递归遍历二叉树,输出每个节点的信息和子节点信息。
程序代码如下:
```c++
#include <iostream>
#include <string>
using namespace std;
// 定义员工信息结构体
struct Employee {
string name; // 员工姓名
string position; // 员工职位
int salary; // 员工工资
string hire_date; // 员工入职时间
string department; // 员工所在部门
string section; // 员工所在科室
string supervisor; // 员工直接上级
};
// 定义二叉链表节点结构体
struct Node {
Employee data; // 员工信息
Node* left; // 左子节点指针
Node* right; // 右子节点指针
};
// 定义二叉链表根节点指针
Node* root = NULL;
// 添加员工节点
void addEmployee(Employee emp) {
Node* newNode = new Node;
newNode->data = emp;
newNode->left = NULL;
newNode->right = NULL;
if (root == NULL) {
root = newNode;
return;
}
Node* cur = root;
while (cur != NULL) {
if (emp.position == "总经理") {
cout << "总经理已存在,无法添加!" << endl;
return;
}
else if (emp.position == "副总经理") {
if (cur->data.position == "总经理") {
if (cur->left == NULL) {
cur->left = newNode;
return;
}
else if (cur->right == NULL) {
cur->right = newNode;
return;
}
else {
cout << "副总经理已满,无法添加!" << endl;
return;
}
}
}
else if (emp.position == "部门经理") {
if (cur->data.position == "副总经理") {
if (cur->left == NULL) {
cur->left = newNode;
return;
}
else if (cur->right == NULL) {
cur->right = newNode;
return;
}
else {
cout << "部门经理已满,无法添加!" << endl;
return;
}
}
}
else if (emp.position == "科室") {
if (cur->data.position == "部门经理") {
if (cur->left == NULL) {
cur->left = newNode;
return;
}
else if (cur->right == NULL) {
cur->right = newNode;
return;
}
else {
cout << "科室已满,无法添加!" << endl;
return;
}
}
}
if (cur->left != NULL && cur->right != NULL) {
cur = cur->left;
}
else if (cur->left != NULL && cur->right == NULL) {
cur = cur->right;
}
else {
cur = cur->left;
}
}
}
// 删除员工节点
void deleteEmployee(string name) {
if (root == NULL) {
cout << "员工不存在,无法删除!" << endl;
return;
}
if (root->data.name == name) {
root = NULL;
return;
}
Node* cur = root;
Node* parent = NULL;
while (cur != NULL) {
if (cur->data.name == name) {
if (cur->left == NULL && cur->right == NULL) {
if (parent->left == cur) {
parent->left = NULL;
}
else {
parent->right = NULL;
}
delete cur;
return;
}
else if (cur->left != NULL && cur->right == NULL) {
if (parent->left == cur) {
parent->left = cur->left;
}
else {
parent->right = cur->left;
}
delete cur;
return;
}
else if (cur->left == NULL && cur->right != NULL) {
if (parent->left == cur) {
parent->left = cur->right;
}
else {
parent->right = cur->right;
}
delete cur;
return;
}
else {
Node* temp = cur->right;
while (temp->left != NULL) {
temp = temp->left;
}
cur->data = temp->data;
deleteEmployee(temp->data.name);
return;
}
}
parent = cur;
if (cur->data.name > name) {
cur = cur->left;
}
else {
cur = cur->right;
}
}
cout << "员工不存在,无法删除!" << endl;
}
// 修改员工信息
void modifyEmployee(string name) {
if (root == NULL) {
cout << "员工不存在,无法修改!" << endl;
return;
}
Node* cur = root;
while (cur != NULL) {
if (cur->data.name == name) {
cout << "请输入修改后的员工信息:" << endl;
cout << "姓名:";
cin >> cur->data.name;
cout << "职位:";
cin >> cur->data.position;
cout << "工资:";
cin >> cur->data.salary;
cout << "入职时间:";
cin >> cur->data.hire_date;
cout << "部门:";
cin >> cur->data.department;
cout << "科室:";
cin >> cur->data.section;
cout << "直接上级:";
cin >> cur->data.supervisor;
return;
}
if (cur->data.name > name) {
cur = cur->left;
}
else {
cur = cur->right;
}
}
cout << "员工不存在,无法修改!" << endl;
}
// 查询员工信息
void queryEmployee(string name) {
if (root == NULL) {
cout << "员工不存在,无法查询!" << endl;
return;
}
Node* cur = root;
while (cur != NULL) {
if (cur->data.name == name) {
cout << "员工信息如下:" << endl;
cout << "姓名:" << cur->data.name << endl;
cout << "职位:" << cur->data.position << endl;
cout << "工资:" << cur->data.salary << endl;
cout << "入职时间:" << cur->data.hire_date << endl;
cout << "部门:" << cur->data.department << endl;
cout << "科室:" << cur->data.section << endl;
cout << "直接上级:" << cur->data.supervisor << endl;
return;
}
if (cur->data.name > name) {
cur = cur->left;
}
else {
cur = cur->right;
}
}
cout << "员工不存在,无法查询!" << endl;
}
// 输出公司组织结构图
void printOrganization(Node* node, int level) {
if (node == NULL) {
return;
}
for (int i = ; i < level; i++) {
cout << " ";
}
cout << "|-- " << node->data.name << " (" << node->data.position << ")" << endl;
printOrganization(node->left, level + 1);
printOrganization(node->right, level + 1);
}
int main() {
while (true) {
cout << "请选择操作:" << endl;
cout << "1. 添加员工节点" << endl;
cout << "2. 删除员工节点" << endl;
cout << "3. 修改员工信息" << endl;
cout << "4. 查询员工信息" << endl;
cout << "5. 输出公司组织结构图" << endl;
cout << "6. 退出程序" << endl;
int choice;
cin >> choice;
if (choice == 1) {
Employee emp;
cout << "请输入员工信息:" << endl;
cout << "姓名:";
cin >> emp.name;
cout << "职位:";
cin >> emp.position;
cout << "工资:";
cin >> emp.salary;
cout << "入职时间:";
cin >> emp.hire_date;
cout << "部门:";
cin >> emp.department;
cout << "科室:";
cin >> emp.section;
cout << "直接上级:";
cin >> emp.supervisor;
addEmployee(emp);
}
else if (choice == 2) {
string name;
cout << "请输入员工姓名:";
cin >> name;
deleteEmployee(name);
}
else if (choice == 3) {
string name;
cout << "请输入员工姓名:";
cin >> name;
modifyEmployee(name);
}
else if (choice == 4) {
string name;
cout << "请输入员工姓名:";
cin >> name;
queryEmployee(name);
}
else if (choice == 5) {
cout << "公司组织结构图如下:" << endl;
printOrganization(root, );
}
else if (choice == 6) {
break;
}
else {
cout << "输入有误,请重新输入!" << endl;
}
}
return ;
}
```