数据结构实验4:树、二叉树与查找
集美大学课程实验报告-实验4:树、二叉树与查找
| 项目名称 | 内容 |
|---|---|
| 课程名称 | 数据结构 |
| 班级 | 网安2512 |
| 学号 | 202521336052 |
| 实验项目名称 | 树、二叉树与查找 |
| 上机实践日期 | 2025年4月30日 |
| 上机实践时间 | 2学时 |
一、目的(本次实验所涉及并要求掌握的知识点)
- 掌握创建二叉树与树及二叉树上的基本操作
- 熟练掌握熟练掌握树的递归结构及在其上的递归算法
- 掌握二叉树的层次遍历
- 掌握BST树上的搜索、创建与删除
- 掌握哈希表、平衡树的应用
二、实验内容与设计思想
题目1:先序序列创建二叉树。
函数相关伪代码
创建一颗树
root = MakeNode(x);
Maketree(root->lchild, s, index);
Maketree(root->rchild, s, index);
中序遍历
InTraverse(root->lchild);
cout << root->data << " ";
InTraverse(root->rchild);
函数代码
#include <iostream>
#include <string>
using namespace std;
typedef char ElemType;
typedef struct BiTNode
{
ElemType data;
struct BiTNode* lchild, * rchild;
} BiTNode, * BiTree;
BiTNode* MakeNode(ElemType data)
{
BiTNode* node1 = new BiTNode;
node1->data = data;
node1->lchild = NULL;
node1->rchild = NULL;
return node1;
}
void InTraverse(BiTree root)
{
if (root != NULL)
{
InTraverse(root->lchild);
cout << root->data << " ";
InTraverse(root->rchild);
}
}
void Maketree(BiTree& root, string& s, int& index)
{
if (index >= s.length()) return;
char x = s[index];
index++;
if (x == '#')
{
root = NULL;
}
else
{
root = MakeNode(x);
Maketree(root->lchild, s, index);
Maketree(root->rchild, s, index);
}
}
void DestroyTree(BiTree& root)
{
if (root != NULL)
{
DestroyTree(root->lchild);
DestroyTree(root->rchild);
delete root;
root = NULL;
}
}
int main()
{
string input;
while (cin >> input)
{
BiTree root = NULL;
int index = 0;
Maketree(root, input, index);
InTraverse(root);
cout << endl;
DestroyTree(root);
}
return 0;
}
题目2:先序输出叶结点
函数相关伪代码
判断是否为叶子节点
BT->Left == NULL && BT->Right == NULL
先序遍历左右子树
PreorderPrintLeaves(BT->Left);
PreorderPrintLeaves(BT->Right);
函数代码
#include <stdio.h>
#include <stdlib.h>
typedef char ElementType;
typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
ElementType Data;
BinTree Left;
BinTree Right;
};
BinTree CreatBinTree(); /* 实现细节忽略 */
void PreorderPrintLeaves( BinTree BT );
int main()
{
BinTree BT = CreatBinTree();
printf("Leaf nodes are:");
PreorderPrintLeaves(BT);
printf("\n");
return 0;
}
void PreorderPrintLeaves(BinTree BT)
{
if (BT == NULL)
return;
if (BT->Left == NULL && BT->Right == NULL)
{
printf(" %c", BT->Data);
}
PreorderPrintLeaves(BT->Left);
PreorderPrintLeaves(BT->Right);
}
题目3:求二叉树高度
函数相关伪代码
树的高度 = 左右子树最大高度 + 1
(leftHeight > rightHeight ? leftHeight : rightHeight) + 1;
函数代码
#include <stdio.h>
#include <stdlib.h>
typedef char ElementType;
typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
ElementType Data;
BinTree Left;
BinTree Right;
};
BinTree CreatBinTree(); /* 实现细节忽略 */
int GetHeight( BinTree BT );
int main()
{
BinTree BT = CreatBinTree();
printf("%d\n", GetHeight(BT));
return 0;
}
int GetHeight(BinTree BT)
{
if (BT == NULL)
return 0;
int leftHeight = GetHeight(BT->Left);
int rightHeight = GetHeight(BT->Right);
return (leftHeight > rightHeight ? leftHeight : rightHeight) + 1;
}
题目4:二叉树层次遍历(广度优先)
函数相关伪代码
用queue来进行层序遍历
函数代码
#include <iostream>
#include <string>
#include <queue>
using namespace std;
typedef char ElementType;
typedef struct TNode
{
ElementType Data;
struct TNode* Left;
struct TNode* Right;
} TNode, * BinTree;
BinTree MakeNode(ElementType data)
{
BinTree node = new TNode;
node->Data = data;
node->Left = NULL;
node->Right = NULL;
return node;
}
BinTree BuildTreeFromArray(string& arr, int index, int len)
{
if (index >= len || arr[index] == '#')
return NULL;
BinTree root = MakeNode(arr[index]);
root->Left = BuildTreeFromArray(arr, 2 * index, len);
root->Right = BuildTreeFromArray(arr, 2 * index + 1, len);
return root;
}
void LevelOrderTraversal(BinTree BT)
{
if (BT == NULL)
{
cout << "NULL";
return;
}
queue<BinTree> q;
bool first = true;
q.push(BT);
while (!q.empty())
{
BinTree node = q.front();
q.pop();
if (first)
{
cout << node->Data;
first = false;
}
else
{
cout << " " << node->Data;
}
if (node->Left != NULL)
q.push(node->Left);
if (node->Right != NULL)
q.push(node->Right);
}
}
int main()
{
string arr;
cin >> arr;
int len = arr.length();
BinTree BT = BuildTreeFromArray(arr, 1, len);
LevelOrderTraversal(BT);
return 0;
}
题目5:创建二叉排序树并遍历。给定一串50 30 80 20 40 90 10 25 35 85 23 88 #创建二叉排序树并对其进行中序遍历。
函数相关伪代码
在T上搜索key。查找成功则返回相应节点,否则返回NULL;
在T上插入key。成功插入,则返回所插入节点;否则返回NULL
函数代码
#include<iostream>
using namespace std;
typedef int ElemType;
typedef struct BiTNode
{
ElemType data;
struct BiTNode* lchild, * rchild;
} BiTNode, * BiTree;
BiTNode* CreateNode(int v)
{
BiTNode* node = new BiTNode;
node->data = v;
node->lchild = NULL;
node->rchild = NULL;
return node;
}
BiTNode* SearchBST(BiTree T, ElemType key)
{
if (T == NULL) return NULL;
if (T->data == key) return T;
if (key < T->data)
{
return SearchBST(T->lchild, key);
}
else
{
return SearchBST(T->rchild, key);
}
}
BiTNode* InsertBST(BiTree& T, ElemType key)
{
if (T == NULL)
{
T = CreateNode(key);
return T;
}
if (key < T->data)
{
return InsertBST(T->lchild, key);
}
else
{
return InsertBST(T->rchild, key);
}
}
BiTree CreateBST()
{
BiTree root = NULL;
ElemType x;
while (1)
{
cin >> x;
if (x == -1) break;
else
{
InsertBST(root, x);
}
}
return root;
}
void InOrder(BiTree t)
{
if (t)
{
InOrder(t->lchild);
cout << t->data << " ";
InOrder(t->rchild);
}
}
int main()
{
// 建树测试数据:50 30 80 20 40 90 10 25 35 85 23 88 -1。
// 注意:-1代表输入结束,不插入-1。
BiTree root = CreateBST();
// 中序该BST应返回排序好后的数 10 20 23 25 30 35 40 50 80 85 88 90
InOrder(root);
cout << endl;
return 0;
}
题目7:哈希表、平衡树的应用:QQ账户查询
函数相关伪代码
构建账户数据库
for (int i = 0; i < n; i++) {
cin >> qq >> name;
accounts[qq] = name;
}
查询
for (int i = 0; i < m; i++) {
cin >> qq;
if (accounts.find(qq) != accounts.end()) {
cout << accounts[qq] << endl;
} else {
cout << "NOT FOUND" << endl;
}
}
函数代码
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
map<long long, string> accounts;
int n, m;
long long qq;
string name;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> qq >> name;
accounts[qq] = name;
}
cin >> m;
for (int i = 0; i < m; i++)
{
cin >> qq;
if (accounts.find(qq) != accounts.end())
{
cout << accounts[qq] << endl;
}
else
{
cout << "NOT FOUND" << endl;
}
}
return 0;
}
三、实验使用环境(本次实验所使用的平台和相关软件)
以下请根据实际情况编写
- 操作系统:Windows 11专业版
- 编程语言:C++
- 开发工具:[Visual Studio 2026]
- 编译器:MSVC 14.40
四、实验步骤和调试过程(实验步骤、测试数据设计、测试结果分析)
以下请根据实际情况编写
题目1:先序序列创建二叉树
本机运行截图

PTA提交截图

题目2:先序输出叶结点
本机运行截图
无
PTA提交截图

题目3:求二叉树高度
本机运行截图
无
PTA提交截图

题目4:二叉树层次遍历(广度优先)
本机运行截图

PTA提交截图

题目5:创建二叉排序树并遍历
本机运行截图

PTA提交截图
无
题目7:哈希表、平衡树的应用
本机运行截图

PTA提交截图
无
五、实验小结(实验中遇到的问题及解决过程、实验体会和收获)
遇到的问题及解决方法:
- 问题:程序崩溃找不到原因,
- 解决方法:使用打断点进行调试的方法。
- 问题:代码缩进不规范。
- 解决方法:使用IDE的格式化工具修复代码。
实验体会和收获:
- 掌握了基本的代码调试方法。
- 掌握了Visual Studio调试功能的基本使用
六、附件(参考文献和相关资料)
以下请根据实际情况编写
浙公网安备 33010602011771号