做了7道题

This commit is contained in:
Ruvikm
2021-02-05 11:45:40 +08:00
parent 992e9578c7
commit b7e51e8d1e
7 changed files with 762 additions and 0 deletions

View File

@@ -0,0 +1,99 @@
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
#pragma region 构造链式存储的二叉树
#define MaxSize 100
#define ElemType char
#define _for(i,a,b) for(int i=(a);i<(b);i++)
typedef struct node { //树的结点
ElemType val;
node* lchild;
node* rchild;
}Node, * BiTree;
void CreateBiTree(BiTree& T)//要改变指针,C++可以把指针的引用传进来
{
ElemType ch;
cin >> ch;
if (ch == '#')
T = NULL;
else
{
T = new Node;
T->val = ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
void visit(Node* BiNode) {
cout << BiNode->val << " ";
}
void InOrder(BiTree T) {
if (T) {
InOrder(T->lchild);
visit(T);
InOrder(T->rchild);
}
}
#pragma endregion
//P150.10
//假设二叉树采用二叉链存储结构存储, 设计一个算法, 求先序遍历序列中第k(1≤k≤
//二叉树中结点个数)个结点的值
queue<Node*> q;
stack<Node*> s;
int Count = 1;
void Visit(BiTree& T,int K) {
if (Count == K)
cout << T->val;
Count++;
}
void GetNumK(BiTree& T,int K) {
if (T) {
Visit(T,K);
GetNumK(T->lchild,K);
GetNumK(T->rchild,K);
}
}
int main()
{
BiTree T;
cout << "请输入先序遍历顺序下各个结点的值,'#'表示没有结点:" << endl;
// input: A B D H # # I # # E # J # # C F # K # # G # #
CreateBiTree(T);
cout << "中序遍历:" << endl;
InOrder(T);
cout << endl;
int k = 4;
cout << "先序遍历序列中第" << k << "个结点的值为" << endl;
GetNumK(T,k);
return 0;
}
/*
A
B C
D E F G
H I Ø J Ø K Ø Ø
Ø Ø Ø Ø Ø Ø Ø Ø
树的图片链接:https://s3.ax1x.com/2021/02/03/yKIbiq.png
*/

View File

@@ -0,0 +1,119 @@
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
#pragma region 构造链式存储的二叉树
#define MaxSize 100
#define ElemType char
#define _for(i,a,b) for(int i=(a);i<(b);i++)
typedef struct node { //树的结点
ElemType val;
node* lchild;
node* rchild;
}Node, * BiTree;
void CreateBiTree(BiTree& T)//要改变指针,C++可以把指针的引用传进来
{
ElemType ch;
cin >> ch;
if (ch == '#')
T = NULL;
else
{
T = new Node;
T->val = ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
void visit(Node* BiNode) {
cout << BiNode->val << " ";
}
void InOrder(BiTree T) {
if (T) {
InOrder(T->lchild);
visit(T);
InOrder(T->rchild);
}
}
#pragma endregion
//P150.11
//已知二叉树以二叉链表存储, 编写算法完成:对于树中每个元素值为x的结点, 删去以
//它为根的子树, 并释放相应的空间
queue<Node*> q;
stack<Node*> s;
void DeleteNode(BiTree &T) {
if (T) {
DeleteNode(T->lchild);
DeleteNode(T->rchild);
delete T;
}
}
void DeleteX(BiTree& T, ElemType X) {
q.push(T);
while (!q.empty()) {
BiTree p = q.front();
q.pop();
if (p->lchild) {
if (p->lchild->val == X) {
DeleteNode(p->lchild);
p->lchild = NULL;
}
else
q.push(p->lchild);
}
if (p->rchild) {
if (p->rchild->val == X) {
DeleteNode(p->rchild);
p->rchild = NULL;
}
else
q.push(p->rchild);
}
}
}
int main()
{
BiTree T;
cout << "请输入先序遍历顺序下各个结点的值,'#'表示没有结点:" << endl;
// input: A B D H # # I # # E # J # # C F # K # # G # #
CreateBiTree(T);
cout << "中序遍历:" << endl;
InOrder(T);
cout << endl;
char X = 'D';
cout << "删除" << X << "后中序遍历为" << endl;
DeleteX(T, X);
InOrder(T);
cout << endl;
return 0;
}
/*
A
B C
D E F G
H I Ø J Ø K Ø Ø
Ø Ø Ø Ø Ø Ø Ø Ø
树的图片链接:https://s3.ax1x.com/2021/02/03/yKIbiq.png
*/

View File

@@ -0,0 +1,104 @@
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
#pragma region 构造链式存储的二叉树
#define MaxSize 100
#define ElemType char
#define _for(i,a,b) for(int i=(a);i<(b);i++)
typedef struct node { //树的结点
ElemType val;
node* lchild;
node* rchild;
}Node, * BiTree;
void CreateBiTree(BiTree& T)//要改变指针,C++可以把指针的引用传进来
{
ElemType ch;
cin >> ch;
if (ch == '#')
T = NULL;
else
{
T = new Node;
T->val = ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
void visit(Node* BiNode) {
cout << BiNode->val << " ";
}
void InOrder(BiTree T) {
if (T) {
InOrder(T->lchild);
visit(T);
InOrder(T->rchild);
}
}
#pragma endregion
//P150.12
//在二又树中查找值为x的结点, 试编写算法(用C语言)打印值为x的结点的所有祖
//先, 假设值为x的结点不多于一个。
queue<Node*> q;
stack<Node*> s;
bool FindAncestors(BiTree T,ElemType X) {
if (!T)
return false;
if (T->val == X)
return true;
if (FindAncestors(T->lchild, X) || FindAncestors(T->rchild, X)) {
s.push(T);
return true;
}
return false;
}
void print() {
while (!s.empty()) {
cout << s.top()->val << " ";
s.pop();
}
}
int main()
{
BiTree T;
cout << "请输入先序遍历顺序下各个结点的值,'#'表示没有结点:" << endl;
// input: A B D H # # I # # E # J # # C F # K # # G # #
CreateBiTree(T);
cout << "中序遍历:" << endl;
InOrder(T);
cout << endl;
char X = 'I';
FindAncestors(T, X);
cout << X << "的祖先结点为:" << endl;
print();
return 0;
}
/*
A
B C
D E F G
H I Ø J Ø K Ø Ø
Ø Ø Ø Ø Ø Ø Ø Ø
树的图片链接:https://s3.ax1x.com/2021/02/03/yKIbiq.png
*/

View File

@@ -0,0 +1,119 @@
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
#pragma region 构造链式存储的二叉树
#define MaxSize 100
#define ElemType char
#define _for(i,a,b) for(int i=(a);i<(b);i++)
typedef struct node { //树的结点
ElemType val;
node* lchild;
node* rchild;
}Node, * BiTree;
void CreateBiTree(BiTree& T)//要改变指针,C++可以把指针的引用传进来
{
ElemType ch;
cin >> ch;
if (ch == '#')
T = NULL;
else
{
T = new Node;
T->val = ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
void visit(Node* BiNode) {
cout << BiNode->val << " ";
}
void InOrder(BiTree T) {
if (T) {
InOrder(T->lchild);
visit(T);
InOrder(T->rchild);
}
}
void FindX(BiTree T,ElemType X,Node* &r) {
if (T) {
FindX(T->lchild,X,r);
if (T->val == X)
r = T;
FindX(T->rchild,X,r);
}
}
#pragma endregion
//P150.13
//设一棵二叉树的结点结构为(LLINK, INFO, RLINK), ROOT为指向该二叉树根结点的
//指针, p和q分别为指向该二叉树中任意两个结点的指针, 试编写算法
//ANCESTOR(ROOT, p, q, r), 找到p和q的最近公共祖先结点r。
queue<Node*> q1, q2;
stack<Node*> s1, s2;
bool FindAncestors(BiTree T, Node* X, queue<Node*> &q) {
if (!T)
return false;
if (T == X)
return true;
if (FindAncestors(T->lchild, X, q) || FindAncestors(T->rchild, X, q)) {
q.push(T);
return true;
}
return false;
}
void ANCESTOR(BiTree ROOT, Node* p, Node* q, Node* &r) {
FindAncestors(ROOT, p, q1);
FindAncestors(ROOT, q, q2);
while (!q1.empty() && !q2.empty() && q1.front() != q2.front()) {
q1.pop();
}
r = q1.front();
return;
}
int main()
{
BiTree T;
cout << "请输入先序遍历顺序下各个结点的值,'#'表示没有结点:" << endl;
// input: A B D H # # I # # E # J # # C F # K # # G # #
CreateBiTree(T);
cout << "中序遍历:" << endl;
InOrder(T);
cout << endl;
Node* p = NULL, * q = NULL;
FindX(T, 'K', p);
FindX(T, 'G', q);
Node* r = NULL;
ANCESTOR(T, p, q, r);
cout << p->val << "和" << q->val << "的最近公共祖先结点为:" << r->val << endl;
return 0;
}
/*
A
B C
D E F G
H I Ø J Ø K Ø Ø
Ø Ø Ø Ø Ø Ø Ø Ø
树的图片链接:https://s3.ax1x.com/2021/02/03/yKIbiq.png
*/

View File

@@ -0,0 +1,131 @@
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
#pragma region 构造链式存储的二叉树
#define MaxSize 100
#define ElemType char
#define _for(i,a,b) for(int i=(a);i<(b);i++)
typedef struct node { //树的结点
ElemType val;
node* lchild;
node* rchild;
}Node, * BiTree;
void CreateBiTree(BiTree& T)//要改变指针,C++可以把指针的引用传进来
{
ElemType ch;
cin >> ch;
if (ch == '#')
T = NULL;
else
{
T = new Node;
T->val = ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
void visit(Node* BiNode) {
cout << BiNode->val << " ";
}
void InOrder(BiTree T) {
if (T) {
InOrder(T->lchild);
visit(T);
InOrder(T->rchild);
}
}
void FindX(BiTree T, ElemType X, Node*& r) {
if (T) {
FindX(T->lchild, X, r);
if (T->val == X)
r = T;
FindX(T->rchild, X, r);
}
}
#pragma endregion
//P150.14
//假设二叉树采用二叉链表存储结构, 设计一个算法, 求非空二叉树b的宽度(即具有结
//点数最多的那一层的结点个数
queue<Node*> q;
stack<Node*> s;
int GetWidth(BiTree T) {
int width = 0;
int Nowidth = 1;
BiTree front, last;
front = last = T;
q.push(T);
while (!q.empty()) {
front = q.front();
if (front->lchild) {
q.push(front->lchild);
Nowidth++;
}
if (front->rchild) {
q.push(front->rchild);
Nowidth++;
}
if (front == last) {
if (Nowidth > width)
width = Nowidth;
Nowidth = 0;
last = q.back();
}
q.pop();
}
return width;
}
int main()
{
BiTree T;
cout << "请输入先序遍历顺序下各个结点的值,'#'表示没有结点:" << endl;
// input(普通二叉树): A B D H # # I # # E # J # # C F # K # # G # #
// input(完全二叉树): A B D H # # I # # E G # # K # # C F L # # M # # G N # # #
CreateBiTree(T);
cout << "中序遍历:" << endl;
InOrder(T);
cout << endl;
cout << "二叉树的宽度为:" << GetWidth(T) << endl;
return 0;
}
/*
完全二叉树:
A
B C
D E F G
H I G K L M N Ø
普通二叉树:
A
B C
D E F G
H I Ø J Ø K Ø Ø
Ø Ø Ø Ø Ø Ø Ø Ø
普通二叉树的图片链接:https://s3.ax1x.com/2021/02/03/yKIbiq.png
*/

View File

@@ -0,0 +1,90 @@
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
#pragma region 构造链式存储的二叉树
#define MaxSize 100
#define ElemType char
#define _for(i,a,b) for(int i=(a);i<(b);i++)
typedef struct node { //树的结点
ElemType val;
node* lchild;
node* rchild;
}Node, * BiTree;
void CreateBiTree(BiTree& T)//要改变指针,C++可以把指针的引用传进来
{
ElemType ch;
cin >> ch;
if (ch == '#')
T = NULL;
else
{
T = new Node;
T->val = ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
void visit(Node* BiNode) {
cout << BiNode->val << " ";
}
void InOrder(BiTree T) {
if (T) {
InOrder(T->lchild);
visit(T);
InOrder(T->rchild);
}
}
#pragma endregion
//P150.8
//假设二叉树采用二叉链表存储结构存储,试设计一个算法,
//计算一棵给定二叉树的所有双分支结点个数
queue<Node*> q;
stack<Node*> s;
int num = 0;
int Get_DouNum(BiTree T) {
if (T) {
Get_DouNum(T->lchild);
if (T->lchild && T->rchild)
num++;
Get_DouNum(T->rchild);
}
return num;
}
int main()
{
BiTree T;
cout << "请输入先序遍历顺序下各个结点的值,'#'表示没有结点:" << endl;
// input: A B D H # # I # # E # J # # C F # K # # G # #
CreateBiTree(T);
cout << "中序遍历:" << endl;
InOrder(T);
cout << endl;
cout << "双结点的个数为:" << Get_DouNum(T) << endl;
return 0;
}
/*
A
B C
D E F G
H I Ø J Ø K Ø Ø
Ø Ø Ø Ø Ø Ø Ø Ø
树的图片链接:https://s3.ax1x.com/2021/02/03/yKIbiq.png
*/

View File

@@ -0,0 +1,100 @@
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
#pragma region 构造链式存储的二叉树
#define MaxSize 100
#define ElemType char
#define _for(i,a,b) for(int i=(a);i<(b);i++)
typedef struct node { //树的结点
ElemType val;
node* lchild;
node* rchild;
}Node, * BiTree;
void CreateBiTree(BiTree& T)//要改变指针,C++可以把指针的引用传进来
{
ElemType ch;
cin >> ch;
if (ch == '#')
T = NULL;
else
{
T = new Node;
T->val = ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
void visit(Node* BiNode) {
cout << BiNode->val << " ";
}
void InOrder(BiTree T) {
if (T) {
InOrder(T->lchild);
visit(T);
InOrder(T->rchild);
}
}
#pragma endregion
//P150.9
//设树B是一棵采用链式结构存储的二叉树, 编写一个把树B中所有结点的左、右子树进
//行交换的函数
queue<Node*> q;
stack<Node*> s;
void Swap(BiTree & a, BiTree& b) {
BiTree t = a;
a = b;
b = t;
}
void SwapNode(BiTree &T) {
if (T) {
SwapNode(T->lchild);
SwapNode(T->rchild);
Swap(T->lchild, T->rchild);
}
}
int main()
{
BiTree T;
cout << "请输入先序遍历顺序下各个结点的值,'#'表示没有结点:" << endl;
// input: A B D H # # I # # E # J # # C F # K # # G # #
CreateBiTree(T);
cout << "中序遍历:" << endl;
InOrder(T);
cout << endl;
cout << "交换完后(中序遍历):" << endl;
SwapNode(T);
InOrder(T);
cout << endl;
return 0;
}
/*
A
B C
D E F G
H I Ø J Ø K Ø Ø
Ø Ø Ø Ø Ø Ø Ø Ø
树的图片链接:https://s3.ax1x.com/2021/02/03/yKIbiq.png
*/