树与二叉树这一章更新完毕

好耶~
This commit is contained in:
Ruvikm
2021-02-09 10:55:53 +08:00
parent add6da7d1f
commit 89a01da716
6 changed files with 848 additions and 0 deletions

View File

@@ -0,0 +1,134 @@
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
#pragma region 构造链式存储的二叉搜索树
#define MaxSize 100
#define ElemType int
#define _for(i,a,b) for(int i=(a);i<(b);i++)
typedef struct node { //树的结点
ElemType val;
node* lchild;
node* rchild;
}Node, * BST_Tree;
void CreateBiTree(BST_Tree& T)//要改变指针,C++可以把指针的引用传进来
{
ElemType ch;
cin >> ch;
if (ch == -1)
T = NULL;
else
{
T = new Node;
T->val = ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
bool BST_Insert(BST_Tree& T, ElemType X) {
if (!T) {
T = new Node;
T->val = X;
T->lchild = T->rchild = NULL;
return true;
}
else if (T->val == X)
return false;
else if (T->val > X) {
return BST_Insert(T->lchild, X);
}
else
return BST_Insert(T->rchild, X);
}
void CreateBST_Tree(BST_Tree& T, vector<int> input) {
T = NULL;
_for(i, 0, input.size())
BST_Insert(T, input[i]);
}
void visit(Node* BiNode) {
cout << BiNode->val << " ";
}
void InOrder(BST_Tree T) {
if (T) {
InOrder(T->lchild);
visit(T);
InOrder(T->rchild);
}
}
void PostOrder(BST_Tree T) {
if (T) {
PostOrder(T->lchild);
PostOrder(T->rchild);
visit(T);
}
}
void FindX(BST_Tree 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
//P195.6
//试编写一个算法, 判断给定的二叉树是否是二叉排序树
ElemType pre = INT_MIN;
bool IsBST(BST_Tree T) {
if (!T) {
return true;
}
else {
if (!IsBST(T->lchild) || pre > T->val) {
return false;
}
pre = T->val;
return IsBST(T->rchild);
}
}
int main()
{
BST_Tree T;
vector<int> input{ 50,38,30,45,40,48,70,60,75,80 };
//CreateBST_Tree(T, input);
CreateBiTree(T);//创建普通的树
// input(普通的树): 1 2 4 8 -1 -1 9 -1 -1 5 -1 10 -1 -1 3 6 -1 11 -1 -1 7 -1 -1
cout << "中序遍历:" << endl;
InOrder(T);
cout << endl;
if (IsBST(T))
cout << "是二叉搜索树" << endl;
else
cout << "不是二叉搜索树" << endl;
return 0;
}
/*
二叉搜索树:
50
38 70
30 45 60 75
40 48 80
*/

View File

@@ -0,0 +1,138 @@
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
#pragma region 构造链式存储的二叉搜索树
#define MaxSize 100
#define ElemType int
#define _for(i,a,b) for(int i=(a);i<(b);i++)
typedef struct node { //树的结点
ElemType val;
node* lchild;
node* rchild;
}Node, * BST_Tree;
void CreateBiTree(BST_Tree& T)//要改变指针,C++可以把指针的引用传进来
{
ElemType ch;
cin >> ch;
if (ch == -1)
T = NULL;
else
{
T = new Node;
T->val = ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
bool BST_Insert(BST_Tree& T, ElemType X) {
if (!T) {
T = new Node;
T->val = X;
T->lchild = T->rchild = NULL;
return true;
}
else if (T->val == X)
return false;
else if (T->val > X) {
return BST_Insert(T->lchild, X);
}
else
return BST_Insert(T->rchild, X);
}
void CreateBST_Tree(BST_Tree& T, vector<int> input) {
T = NULL;
_for(i, 0, input.size())
BST_Insert(T, input[i]);
}
void visit(Node* BiNode) {
cout << BiNode->val << " ";
}
void InOrder(BST_Tree T) {
if (T) {
InOrder(T->lchild);
visit(T);
InOrder(T->rchild);
}
}
void PostOrder(BST_Tree T) {
if (T) {
PostOrder(T->lchild);
PostOrder(T->rchild);
visit(T);
}
}
void FindX(BST_Tree T, ElemType X, Node*& r) {
if (T) {
while (T->val != X) {
if (X < T->val)
T = T->lchild;
else
T = T->rchild;
}
r = T;
}
}
#pragma endregion
//P195.7
//设计一个算法,求出指定结点在给定二叉排序树中的层次
int Level(BST_Tree T, int level, Node* X) {
if (T->val == X->val) {
return level;
}
else if (X->val < T->val)
return Level(T->lchild, level + 1, X);
else if (X->val > T->val)
return Level(T->rchild, level + 1, X);
}
int GetLevel(BST_Tree T, Node* X) {
return Level(T, 1, X);
}
int main()
{
BST_Tree T;
vector<int> input{ 50,38,30,45,40,48,70,60,75,80 };
CreateBST_Tree(T, input);
/* CreateBiTree(T);*/
// input(普通的树): 1 2 4 8 -1 -1 9 -1 -1 5 -1 10 -1 -1 3 6 -1 11 -1 -1 7 -1 -1
cout << "中序遍历:" << endl;
InOrder(T);
cout << endl;
Node* x;
FindX(T, 48, x);
cout << "所在层次为:" << GetLevel(T, x) << endl;
return 0;
}
/*
二叉搜索树:
50
38 70
30 45 60 75
40 48 80
*/

View File

@@ -0,0 +1,162 @@
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
#pragma region 构造链式存储的二叉搜索树
#define MaxSize 100
#define ElemType int
#define _for(i,a,b) for(int i=(a);i<(b);i++)
typedef struct node { //树的结点
ElemType val;
node* lchild;
node* rchild;
}Node, * BST_Tree;
void CreateBiTree(BST_Tree& T)//要改变指针,C++可以把指针的引用传进来
{
ElemType ch;
cin >> ch;
if (ch == -1)
T = NULL;
else
{
T = new Node;
T->val = ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
bool BST_Insert(BST_Tree& T, ElemType X) {
if (!T) {
T = new Node;
T->val = X;
T->lchild = T->rchild = NULL;
return true;
}
else if (T->val == X)
return false;
else if (T->val > X) {
return BST_Insert(T->lchild, X);
}
else
return BST_Insert(T->rchild, X);
}
void CreateBST_Tree(BST_Tree& T, vector<int> input) {
T = NULL;
_for(i, 0, input.size())
BST_Insert(T, input[i]);
}
void visit(Node* BiNode) {
cout << BiNode->val << " ";
}
void InOrder(BST_Tree T) {
if (T) {
InOrder(T->lchild);
visit(T);
InOrder(T->rchild);
}
}
void PostOrder(BST_Tree T) {
if (T) {
PostOrder(T->lchild);
PostOrder(T->rchild);
visit(T);
}
}
void FindX(BST_Tree T, ElemType X, Node*& r) {
if (T) {
while (T->val != X) {
if (X < T->val)
T = T->lchild;
else
T = T->rchild;
}
r = T;
}
}
#pragma endregion
//P195.8
//利用二叉树遍历的思想编写一个判断二叉树是否是平衡二叉树的算法
void IsBBT(BST_Tree T,int &balance,int &h) {
int bl, br, hl, hr;
bl = br = hl = hr = 0;
if (!T) {
h = 0;
balance = 1;
}
else if (!T->lchild && !T->rchild) {
h = 1;
balance = 1;
}
else {
IsBBT(T->lchild, bl, hl);
IsBBT(T->rchild, br, hr);
h = (hl > hr ? hl : hr) + 1;
if (abs(hl - hr) < 2) {
balance = bl && br;
}
else {
balance = 0;
}
}
}
bool Judge_AVL(BST_Tree T) {
int balance = 0, h = 0;
IsBBT(T, balance, h);
if (balance) {
cout << "是二叉平衡树" << endl;
return true;
}
else {
cout << "不是二叉平衡树" << endl;
return false;
}
}
int main()
{
BST_Tree T;
/*vector<int> input{ 50,38,53};*/
vector<int> input{ 50,38,30,20 };
/*vector<int> input{ 50,38,30,45,40,48,70,60,75,80 };*/
CreateBST_Tree(T, input);
/* CreateBiTree(T);*/
// input(普通的树): 1 2 4 8 -1 -1 9 -1 -1 5 -1 10 -1 -1 3 6 -1 11 -1 -1 7 -1 -1
cout << "中序遍历:" << endl;
InOrder(T);
cout << endl;
Judge_AVL(T);
return 0;
}
/*
二叉搜索树:
50
38 70
30 45 60 75
40 48 80
*/

View File

@@ -0,0 +1,130 @@
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
#pragma region 构造链式存储的二叉搜索树
#define MaxSize 100
#define ElemType int
#define _for(i,a,b) for(int i=(a);i<(b);i++)
typedef struct node { //树的结点
ElemType val;
node* lchild;
node* rchild;
}Node, * BST_Tree;
void CreateBiTree(BST_Tree& T)//要改变指针,C++可以把指针的引用传进来
{
ElemType ch;
cin >> ch;
if (ch == -1)
T = NULL;
else
{
T = new Node;
T->val = ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
bool BST_Insert(BST_Tree& T, ElemType X) {
if (!T) {
T = new Node;
T->val = X;
T->lchild = T->rchild = NULL;
return true;
}
else if (T->val == X)
return false;
else if (T->val > X) {
return BST_Insert(T->lchild, X);
}
else
return BST_Insert(T->rchild, X);
}
void CreateBST_Tree(BST_Tree& T, vector<int> input) {
T = NULL;
_for(i, 0, input.size())
BST_Insert(T, input[i]);
}
void visit(Node* BiNode) {
cout << BiNode->val << " ";
}
void InOrder(BST_Tree T) {
if (T) {
InOrder(T->lchild);
visit(T);
InOrder(T->rchild);
}
}
void PostOrder(BST_Tree T) {
if (T) {
PostOrder(T->lchild);
PostOrder(T->rchild);
visit(T);
}
}
void FindX(BST_Tree T, ElemType X, Node*& r) {
if (T) {
while (T->val != X) {
if (X < T->val)
T = T->lchild;
else
T = T->rchild;
}
r = T;
}
}
#pragma endregion
//P195.10
//设计一个算法,从大到小输出二叉排序树中所有值不小于k的关键字
void NotLessThan_K(BST_Tree T, int k) {
if (T) {
NotLessThan_K(T->rchild, k);
if (T->val >= k)
cout << T->val<<" ";
NotLessThan_K(T->lchild, k);
}
}
int main()
{
BST_Tree T;
vector<int> input{ 50,38,30,45,40,48,70,60,75,80 };
CreateBST_Tree(T, input);
cout << "中序遍历:" << endl;
InOrder(T);
cout << endl;
cout << "不小于56的值有:" << endl;
NotLessThan_K(T, 56);
return 0;
}
/*
二叉搜索树:
50
38 70
30 45 60 75
40 48 80
*/

View File

@@ -0,0 +1,150 @@
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
#pragma region 构造链式存储的二叉搜索树
#define MaxSize 100
#define ElemType int
#define _for(i,a,b) for(int i=(a);i<(b);i++)
typedef struct node { //树的结点
ElemType val;
int count = 0;
node* lchild;
node* rchild;
}Node, * BST_Tree;
int NodeNum(BST_Tree T) {
if (!T) {
return 0;
}
return 1 + NodeNum(T->lchild) + NodeNum(T->rchild);
}
void CreateBiTree(BST_Tree& T)//要改变指针,C++可以把指针的引用传进来
{
ElemType ch;
cin >> ch;
if (ch == -1)
T = NULL;
else
{
T = new Node;
T->val = ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
bool BST_Insert(BST_Tree& T, ElemType X) {
if (!T) {
T = new Node;
T->val = X;
T->lchild = T->rchild = NULL;
return true;
}
else if (T->val == X)
return false;
else if (T->val > X) {
return BST_Insert(T->lchild, X);
}
else
return BST_Insert(T->rchild, X);
}
void CreateBST_Tree(BST_Tree& T, vector<int> input) {
T = NULL;
_for(i, 0, input.size())
BST_Insert(T, input[i]);
}
void visit(Node* BiNode) {
cout << BiNode->val << " ";
BiNode->count = NodeNum(BiNode);
}
void InOrder(BST_Tree T) {
if (T) {
InOrder(T->lchild);
visit(T);
InOrder(T->rchild);
}
}
void PostOrder(BST_Tree T) {
if (T) {
PostOrder(T->lchild);
PostOrder(T->rchild);
visit(T);
}
}
void FindX(BST_Tree T, ElemType X, Node*& r) {
if (T) {
while (T->val != X) {
if (X < T->val)
T = T->lchild;
else
T = T->rchild;
}
r = T;
}
}
#pragma endregion
//P195.12
//编写一个递归算法,在一棵有n个结点的、随机建立起来的二叉排序树上查找第k(1≤
//≤n)小的元素, 并返回指向该结点的指针。要求算法的平均时间复杂度为O(log2n)
//二叉排序树的每个结点中除data, lchild, child等数据成员外, 增加一个 count
//成员, 保存以该结点为根的子树上的结点个数
Node* Find_KthMin(BST_Tree T, int k) {
if (k < 1 || k > T->count)
return NULL;
if (!T->lchild) {
if (k == 1)
return T;
else
return Find_KthMin(T->rchild, k - 1);
}
else {
if (T->lchild->count == k - 1)
return T;
if (T->lchild->count > k - 1)
return Find_KthMin(T->lchild, k);
if (T->lchild->count < k - 1)
return Find_KthMin(T->rchild, k - (T->lchild->count + 1));
}
}
int main()
{
BST_Tree T;
vector<int> input{ 50,38,30,45,40,48,70,60,75,80 };
CreateBST_Tree(T, input);
cout << "中序遍历:" << endl;
InOrder(T);
cout << endl;
cout << "5th min is :" << " " << Find_KthMin(T, 5)->val << endl;
return 0;
}
/*
二叉搜索树:
50
38 70
30 45 60 75
40 48 80
*/

View File

@@ -0,0 +1,134 @@
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
#pragma region 构造链式存储的二叉搜索树
#define MaxSize 100
#define ElemType int
#define _for(i,a,b) for(int i=(a);i<(b);i++)
typedef struct node { //树的结点
ElemType val;
node* lchild;
node* rchild;
}Node, * BST_Tree;
void CreateBiTree(BST_Tree& T)//要改变指针,C++可以把指针的引用传进来
{
ElemType ch;
cin >> ch;
if (ch == -1)
T = NULL;
else
{
T = new Node;
T->val = ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
bool BST_Insert(BST_Tree& T, ElemType X) {
if (!T) {
T = new Node;
T->val = X;
T->lchild = T->rchild = NULL;
return true;
}
else if (T->val == X)
return false;
else if (T->val > X) {
return BST_Insert(T->lchild, X);
}
else
return BST_Insert(T->rchild, X);
}
void CreateBST_Tree(BST_Tree& T, vector<int> input) {
T = NULL;
_for(i, 0, input.size())
BST_Insert(T, input[i]);
}
void visit(Node* BiNode) {
cout << BiNode->val << " ";
}
void InOrder(BST_Tree T) {
if (T) {
InOrder(T->lchild);
visit(T);
InOrder(T->rchild);
}
}
void PostOrder(BST_Tree T) {
if (T) {
PostOrder(T->lchild);
PostOrder(T->rchild);
visit(T);
}
}
void FindX(BST_Tree T, ElemType X, Node*& r) {
if (T) {
while (T->val != X) {
if (X < T->val)
T = T->lchild;
else
T = T->rchild;
}
r = T;
}
}
#pragma endregion
//P195.9
//设计一个算法,求出给定二叉排序树中最小和最大的关键字
void BestValue(BST_Tree T,int &min,int &max) {
BST_Tree p = T, q = T;
while (p->lchild) {
p = p->lchild;
}
while (q->rchild) {
q = q->rchild;
}
min = p->val;
max = q->val;
}
int main()
{
BST_Tree T;
vector<int> input{ 50,38,30,45,40,48,70,60,75,80 };
CreateBST_Tree(T, input);
cout << "中序遍历:" << endl;
InOrder(T);
cout << endl;
int min = 0, max = 0;
BestValue(T, min, max);
cout << "MIN:" << min << endl;
cout << "MAX:" << max << endl;
return 0;
}
/*
二叉搜索树:
50
38 70
30 45 60 75
40 48 80
*/