集美大学课程实验报告-实验4-树、二叉树与查找
集美大学课程实验报告-4-树、二叉树与查找
项目名称 | 内容 |
---|---|
课程名称 | 数据结构 |
班级 | 网安2411 |
指导教师 | 郑如滨 |
学生姓名 | 李斌财 |
学号 | 202421336021 |
实验项目名称 | 树、二叉树与查找 |
上机实践日期 | |
上机实践时间 | 2学时 |
一、目的
- 掌握创建二叉树与树及二叉树上的基本操作
- 熟练掌握熟练掌握树的递归结构及在其上的递归算法
- 掌握二叉树的层次遍历
- 掌握BST树上的搜索、创建与删除
- 掌握哈希表的应用
二、实验内容与设计思想
- 题目名称及主要内容。
- 函数伪代码及对应代码
- 函数的时间复杂度、空间复杂度。
前序序列创建二叉树
#include <iostream>
#include <string>
using namespace std;
struct TreeNode {
char val;
TreeNode* left;
TreeNode* right;
TreeNode(char x) : val(x), left(nullptr), right(nullptr) {}
};
TreeNode* buildTree(string& preorder, int& index) {
if (index >= preorder.size() || preorder[index] == '#') {
index++;
return nullptr;
}
TreeNode* root = new TreeNode(preorder[index++]);
root->left = buildTree(preorder, index);
root->right = buildTree(preorder, index);
return root;
}
void inorderTraversal(TreeNode* root) {
if (!root) return;
inorderTraversal(root->left);
cout << root->val << " ";
inorderTraversal(root->right);
}
int main() {
string preorder;
while (cin >> preorder) {
int index = 0;
TreeNode* root = buildTree(preorder, index);
inorderTraversal(root);
cout << endl;
}
return 0;
}
- 时间复杂度 :O(n)
- 空间复杂度 :O(n)
先序输出叶结点
void PreorderPrintLeaves( BinTree BT ){
if(BT==NULL)return BT;
if(BT->Left==NULL&&BT->Right==NULL){
printf(" %c",BT->Data);
}
PreorderPrintLeaves(BT->Left);
PreorderPrintLeaves(BT->Right);
}
- 时间复杂度 :O(n)
- 空间复杂度 :O(n)
求二叉树高度
```cpp
int GetHeight(BinTree BT) {
if (!BT) return 0; // 空树高度为0
int leftHeight = GetHeight(BT->Left); // 递归计算左子树高度
int rightHeight = GetHeight(BT->Right); // 递归计算右子树高度
return (leftHeight > rightHeight ? leftHeight : rightHeight) + 1;
}
- 时间复杂度 :O(n)
- 空间复杂度 :O(n)
二叉树的层序遍历(leetcode第102题)
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
queue<TreeNode*> que;
if(root!=nullptr) que.push(root);
vector<vector<int>> result;
while(!que.empty()){
int size=que.size();
vector<int> vec;
for(int i=0;i<size;i++){
TreeNode*node=que.front();
que.pop();
vec.push_back(node->val);
if(node->left)que.push(node->left);
if(node->right)que.push(node->right);
}
result.push_back(vec);
}
return result;
}
- 时间复杂度 :O(n)
- 空间复杂度 :O(n)
};
创建二叉排序树并遍历
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
TreeNode* insertNode(TreeNode* root, int val) {
if (!root) return new TreeNode(val);
if (val < root->val) {
root->left = insertNode(root->left, val);
} else {
root->right = insertNode(root->right, val);
}
return root;
}
void inorderTraversal(TreeNode* root) {
if (!root) return;
inorderTraversal(root->left);
cout << root->val << " ";
inorderTraversal(root->right);
}
int main() {
TreeNode* root = nullptr;
string input;
getline(cin, input);
istringstream iss(input);
string token;
while (iss >> token) {
if (token == "#") break;
root = insertNode(root, stoi(token));
}
inorderTraversal(root);
return 0;
}
- 时间复杂度 :O(n)
- 空间复杂度 :O(n)
删除二叉搜索树中的节点(leetcode 450)
class Solution {
public:
TreeNode* deleteNode(TreeNode* root, int key) {
if(root==nullptr)return root;
if(root->val==key){
if(root->left==nullptr&&root->right==nullptr){
delete root;
return nullptr;
}
else if(root->left==nullptr){
auto retnode=root->right;
delete root;
return retnode;
}
else if(root->right==nullptr){
auto retnode=root->left;
delete root;
return retnode;
}
else{
TreeNode*cur=root->right;
while(cur->left!=nullptr){
cur=cur->left;
}
cur->left=root->left;
auto tmp=root;
root=root->right;
delete tmp;
return root;
}
}
if(root->val>key)root->left=deleteNode(root->left,key);
if(root->val<key)root->right=deleteNode(root->right,key);
return root;
}
};
- 时间复杂度 :O(n)
- 空间复杂度 :O(n)
哈希表的应用:QQ账户查询
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main() {
int N;
cin >> N;
map<string, string> accounts;
for (int i = 0; i < N; ++i) {
char cmd;
string qq, pwd;
cin >> cmd >> qq >> pwd;
if (cmd == 'N') {
if (accounts.find(qq) != accounts.end()) {
cout << "ERROR: Exist" << endl;
} else {
accounts[qq] = pwd;
cout << "New: OK" << endl;
}
} else if (cmd == 'L') {
auto it = accounts.find(qq);
if (it == accounts.end()) {
cout << "ERROR: Not Exist" << endl;
} else {
if (it->second == pwd) {
cout << "Login: OK" << endl;
} else {
cout << "ERROR: Wrong PW" << endl;
}
}
}
}
return 0;
}
- 时间复杂度 :O(n)
- 空间复杂度 :O(n)
三、实验使用环境(本次实验所使用的平台和相关软件)
操作系统:Windows 11
编程语言:C++
开发工具:PTA,visual studio 2022
编译器:g++ 17
四、实验步骤和调试过程(实验步骤、测试数据设计、测试结果分析)
五、实验小结(实验中遇到的问题及解决过程、实验体会
实验体会和收获:
掌握了创建二叉树与树及二叉树上的基本操作
掌握了二叉树的层次遍历
BST树上的搜索、创建与删除
简单的哈希表应用