8608 实现二叉排序树的各种算法(2)
时间限制:1000MS 代码长度限制:10KB
提交次数:2559 通过次数:1396
题型: 编程题 语言: G++;GCC
Description
用函数实现如下二叉排序树算法:
(1) 插入新结点
(2) 前序、中序、后序遍历二叉树
(3) 中序遍历的非递归算法
(4) 层次遍历二叉树
(5) 在二叉树中查找给定关键字(函数返回值为成功1,失败0)
(6) 交换各结点的左右子树
(7) 求二叉树的深度
(8) 叶子结点数
输入格式
第一行:准备建树的结点个数n
第二行:输入n个整数,用空格分隔
第三行:输入待查找的关键字
第四行:输入待查找的关键字
第五行:输入待插入的关键字
输出格式
第一行:二叉树的先序遍历序列
第二行:二叉树的中序遍历序列
第三行:二叉树的后序遍历序列
第四行:查找结果
第五行:查找结果
第六行~第八行:插入新结点后的二叉树的先、中、序遍历序列
第九行:插入新结点后的二叉树的中序遍历序列(非递归算法)
第十行:插入新结点后的二叉树的层次遍历序列
第十一行~第十三行:第一次交换各结点的左右子树后的先、中、后序遍历序列
第十四行~第十六行:第二次交换各结点的左右子树后的先、中、后序遍历序列
第十七行:二叉树的深度
第十八行:叶子结点数
输入样例
7
40 20 60 18 50 56 90
18
35
30
输出样例
40 20 18 60 50 56 90
18 20 40 50 56 60 90
18 20 56 50 90 60 40
1
0
40 20 18 30 60 50 56 90
18 20 30 40 50 56 60 90
18 30 20 56 50 90 60 40
18 20 30 40 50 56 60 90
40 20 60 18 30 50 90 56
40 60 90 50 56 20 30 18
90 60 56 50 40 30 20 18
90 56 50 60 30 18 20 40
40 20 18 30 60 50 56 90
18 20 30 40 50 56 60 90
18 30 20 56 50 90 60 40
4
4
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <string>
#include <queue>
#include <stack>
using namespace std;
// 二叉树节点结构体
struct Node {
int data; // 节点数据
Node *lchild, *rchild; // 左右孩子指针
};
// 向二叉搜索树中插入节点
Node* in(Node *T, int key) {
if (!T) { // 如果树为空,创建新节点
Node *p = new Node;
p->data = key;
p->lchild = p->rchild = nullptr;
return p;
}
if (key < T->data) { // 插入左子树
T->lchild = in(T->lchild, key);
} else if (key > T->data) { // 插入右子树
T->rchild = in(T->rchild, key);
}
// 如果相等则不插入
return T;
}
// 递归先序遍历
void pre(Node *T) {
if (!T) {
return;
}
cout << T->data << " ";
pre(T->lchild);
pre(T->rchild);
}
// 递归中序遍历
void mi(Node *T) {
if (!T) {
return;
}
mi(T->lchild);
cout << T->data << " ";
mi(T->rchild);
}
// 递归后序遍历
void beh(Node *T) {
if (!T) {
return;
}
beh(T->lchild);
beh(T->rchild);
cout << T->data << " ";
}
// 非递归中序遍历
void nmi(Node *T) {
if (!T) {
return;
}
stack<Node*> st; // 辅助栈
Node *curr = T; // 当前节点
while (curr || !st.empty()) {
// 1. 遍历到最左节点,沿途入栈
while (curr) {
st.push(curr);
curr = curr->lchild;
}
// 2. 弹出栈顶节点并访问
curr = st.top();
st.pop();
cout << curr->data << " ";
// 3. 转向右子树
curr = curr->rchild;
}
}
// 非递归先序遍历
void npre(Node *T) {
if (!T) {
return;
}
stack<Node*> st; // 辅助栈
st.push(T); // 根节点入栈
while (!st.empty()) {
Node *curr = st.top(); // 访问栈顶节点
st.pop();
cout << curr->data << " ";
// 右孩子先入栈,左孩子后入栈,保证左孩子先被访问
if (curr->rchild) {
st.push(curr->rchild);
}
if (curr->lchild) {
st.push(curr->lchild);
}
}
}
// 非递归后序遍历
void nbeh(Node *T) {
if (!T) {
return;
}
stack<Node*> st; // 辅助栈
stack<int> res; // 结果栈
st.push(T);
while (!st.empty()) {
Node *curr = st.top();
st.pop();
res.push(curr->data); // 将节点值存入结果栈
// 左孩子先入栈,右孩子后入栈,保证右孩子先被访问
if (curr->lchild) {
st.push(curr->lchild);
}
if (curr->rchild) {
st.push(curr->rchild);
}
}
// 输出结果栈中的内容(即后序遍历结果)
while (!res.empty()) {
cout << res.top() << " ";
res.pop();
}
}
// 查找节点
bool keyfind(Node *T, int key) {
if (!T) {
return 0;
}
while (T) {
if (key == T->data) {
return 1;
} else if (key > T->data) {
T = T->rchild;
continue;
} else if (key < T->data) {
T = T->lchild;
continue;
}
}
return 0;
}
// 层序遍历
void classprint(Node *T) {
if (!T) {
return;
}
queue<Node *> st;
Node *curr = T;
st.push(curr);
while (curr || !st.empty()) {
if (curr->lchild) st.push(curr->lchild);
if (curr->rchild) st.push(curr->rchild);
cout << curr->data << " ";
st.pop();
curr = st.front();
}
}
// 反转二叉树
void re(Node *T) {
if (!T) {
return;
}
if (T->lchild) {
re(T->lchild);
}
if (T->rchild) {
re(T->rchild);
}
Node *p = T->lchild;
T->lchild = T->rchild;
T->rchild = p;
}
// 计算树的高度
int height(Node *T) {
if (!T) return 0;
int dl = height(T->lchild);
int dr = height(T->rchild);
return max(dl, dr) + 1;
}
// 计算叶子节点数量
int cleaf(Node* T) {
if (!T) return 0;
if (!T->lchild && !T->rchild) return 1;
return cleaf(T->lchild) + cleaf(T->rchild);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
Node* root = nullptr;
for (int i = 0, x; i < n; i++) {
cin >> x;
root = in(root, x);
}
int a, b, c;
cin >> a >> b >> c;
pre(root); cout << endl;
mi(root); cout << endl;
beh(root); cout << endl;
root = in(root, c);
cout << keyfind(root, a) << endl;
cout << keyfind(root, b) << endl;
pre(root); cout << endl;
mi(root); cout << endl;
beh(root); cout << endl;
nmi(root); cout << endl;
classprint(root); cout << endl;
re(root);
pre(root); cout << endl;
mi(root); cout << endl;
beh(root); cout << endl;
re(root);
pre(root); cout << endl;
mi(root); cout << endl;
beh(root); cout << endl;
cout << height(root) << endl;
cout << cleaf(root) << endl;
return 0;
}