From 89a01da71613c0458982801ea692d45222583b41 Mon Sep 17 00:00:00 2001 From: Ruvikm <1092045998@qq.com> Date: Tue, 9 Feb 2021 10:55:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=91=E4=B8=8E=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E8=BF=99=E4=B8=80=E7=AB=A0=E6=9B=B4=E6=96=B0=E5=AE=8C=E6=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 好耶~ --- 树与二叉树/树与二叉树的应用/P195.6.cpp | 134 ++++++++++++++++++++ 树与二叉树/树与二叉树的应用/P195.7.cpp | 138 ++++++++++++++++++++ 树与二叉树/树与二叉树的应用/P195.8.cpp | 162 ++++++++++++++++++++++++ 树与二叉树/树与二叉树的应用/P196.10.cpp | 130 +++++++++++++++++++ 树与二叉树/树与二叉树的应用/P196.12.cpp | 150 ++++++++++++++++++++++ 树与二叉树/树与二叉树的应用/P196.9.cpp | 134 ++++++++++++++++++++ 6 files changed, 848 insertions(+) create mode 100644 树与二叉树/树与二叉树的应用/P195.6.cpp create mode 100644 树与二叉树/树与二叉树的应用/P195.7.cpp create mode 100644 树与二叉树/树与二叉树的应用/P195.8.cpp create mode 100644 树与二叉树/树与二叉树的应用/P196.10.cpp create mode 100644 树与二叉树/树与二叉树的应用/P196.12.cpp create mode 100644 树与二叉树/树与二叉树的应用/P196.9.cpp diff --git a/树与二叉树/树与二叉树的应用/P195.6.cpp b/树与二叉树/树与二叉树的应用/P195.6.cpp new file mode 100644 index 0000000..043ce61 --- /dev/null +++ b/树与二叉树/树与二叉树的应用/P195.6.cpp @@ -0,0 +1,134 @@ +#include +#include +#include +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 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 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 + +*/ \ No newline at end of file diff --git a/树与二叉树/树与二叉树的应用/P195.7.cpp b/树与二叉树/树与二叉树的应用/P195.7.cpp new file mode 100644 index 0000000..eb88b03 --- /dev/null +++ b/树与二叉树/树与二叉树的应用/P195.7.cpp @@ -0,0 +1,138 @@ +#include +#include +#include +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 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 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 + +*/ \ No newline at end of file diff --git a/树与二叉树/树与二叉树的应用/P195.8.cpp b/树与二叉树/树与二叉树的应用/P195.8.cpp new file mode 100644 index 0000000..5223d21 --- /dev/null +++ b/树与二叉树/树与二叉树的应用/P195.8.cpp @@ -0,0 +1,162 @@ +#include +#include +#include +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 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 input{ 50,38,53};*/ + vector input{ 50,38,30,20 }; + /*vector 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 + +*/ \ No newline at end of file diff --git a/树与二叉树/树与二叉树的应用/P196.10.cpp b/树与二叉树/树与二叉树的应用/P196.10.cpp new file mode 100644 index 0000000..89f38b5 --- /dev/null +++ b/树与二叉树/树与二叉树的应用/P196.10.cpp @@ -0,0 +1,130 @@ +#include +#include +#include +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 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 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 + +*/ \ No newline at end of file diff --git a/树与二叉树/树与二叉树的应用/P196.12.cpp b/树与二叉树/树与二叉树的应用/P196.12.cpp new file mode 100644 index 0000000..7522452 --- /dev/null +++ b/树与二叉树/树与二叉树的应用/P196.12.cpp @@ -0,0 +1,150 @@ +#include +#include +#include +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 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 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 + +*/ \ No newline at end of file diff --git a/树与二叉树/树与二叉树的应用/P196.9.cpp b/树与二叉树/树与二叉树的应用/P196.9.cpp new file mode 100644 index 0000000..e4e5562 --- /dev/null +++ b/树与二叉树/树与二叉树的应用/P196.9.cpp @@ -0,0 +1,134 @@ +#include +#include +#include +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 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 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 + +*/ \ No newline at end of file