// 中序遍历二叉排序树,打印用户信息
void inorderTraversal(Node* root) {
if (root != NULL) {
inorderTraversal(root->left);
printf("ID: %d, Username: %s, Password: %s, Phone: %s, Email: %s\n",
root->user.id, root->user.username, root->user.password, root->user.phone, root->user.email);
inorderTraversal(root->right);
}
}
// 删除二叉排序树中的结点
Node* deleteNode(Node* root, char* username) {
if (root == NULL) {
return root;
}
if (strcmp(username, root->user.username) < 0) {
root->left = deleteNode(root->left, username);
} else if (strcmp(username, root->user.username) > 0) {
root->right = deleteNode(root->right, username);
} else {
// 找到要删除的结点
// 结点没有子节点或只有一个子节点
if (root->left == NULL) {
Node* temp = root->right;
free(root);
return temp;
} else if (root->right == NULL) {
Node* temp = root->left;
free(root);
return temp;
}
// 结点有两个子节点
Node* minNode = root->right;
while (minNode->left != NULL) {
minNode = minNode->left;
}
root->user = minNode->user;
root->right = deleteNode(root->right, minNode->user.username);
}
return root;
}