diff --git a/树与二叉树/二叉树的概念/P134.5.cpp b/树与二叉树/二叉树的概念/P134.5.cpp index fe1ae9e..bfe367d 100644 --- a/树与二叉树/二叉树的概念/P134.5.cpp +++ b/树与二叉树/二叉树的概念/P134.5.cpp @@ -70,7 +70,7 @@ int main() CreatTree(t, input); PreOrder(t,1); cout << endl; - cout << "公共祖先为:" << FindCom_Ancestor(t, 10, 4); + cout << "公共祖先为:" << FindCom_Ancestor(t, 10, 4) << endl; return 0; } /* diff --git a/树与二叉树/二叉树的遍历和线索二叉树/P149.3.cpp b/树与二叉树/二叉树的遍历和线索二叉树/P149.3.cpp new file mode 100644 index 0000000..86d09aa --- /dev/null +++ b/树与二叉树/二叉树的遍历和线索二叉树/P149.3.cpp @@ -0,0 +1,111 @@ +#include +#include +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 != NULL) { + InOrder(T->lchild); + visit(T); + InOrder(T->rchild); + } +} + +#pragma endregion + +//P149.3 +//编写后序遍历二叉树的非递归算法。 + +stack s; + +void PostOrder(BiTree T) { + BiTree p = T, r = NULL; + while (p || !s.empty()) { + if (p) { + s.push(p); + p = p->lchild; + } + else { + p = s.top(); + if (p->rchild && p->rchild != r) { + p = p->rchild; + s.push(p); + p = p->lchild; + } + else { + Node* q = s.top(); + s.pop(); + visit(q); + r = q; + p = NULL; + } + } + } + +} + + + + +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; + PostOrder(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 +*/ \ No newline at end of file diff --git a/树与二叉树/二叉树的遍历和线索二叉树/P149.4.cpp b/树与二叉树/二叉树的遍历和线索二叉树/P149.4.cpp new file mode 100644 index 0000000..1ee4fec --- /dev/null +++ b/树与二叉树/二叉树的遍历和线索二叉树/P149.4.cpp @@ -0,0 +1,103 @@ +#include +#include +#include +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 != NULL) { + InOrder(T->lchild); + visit(T); + InOrder(T->rchild); + } +} + +#pragma endregion + +//P149.4 +//试给出二叉树的自下而上、从右到左的层次遍历算法 + +queue q; +stack s; + +void LevelOrder(BiTree T) { + q.push(T); + while (!q.empty()) { + Node* t = q.front(); + q.pop(); + s.push(t); + if (t->lchild) + q.push(t->lchild); + if (t->rchild) + q.push(t->rchild); + } + while (!s.empty()) { + Node* x = s.top(); + s.pop(); + visit(x); + } +} + + +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; + LevelOrder(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 +*/ \ No newline at end of file diff --git a/树与二叉树/二叉树的遍历和线索二叉树/P149.5.cpp b/树与二叉树/二叉树的遍历和线索二叉树/P149.5.cpp new file mode 100644 index 0000000..bd00319 --- /dev/null +++ b/树与二叉树/二叉树的遍历和线索二叉树/P149.5.cpp @@ -0,0 +1,112 @@ +#include +#include +#include +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 != NULL) { + InOrder(T->lchild); + visit(T); + InOrder(T->rchild); + } +} + +#pragma endregion + +//P149.5 +//假设二叉树采用二叉链表存储结构,设计一个非递归算法求二叉树的高度 + +queue q; +stack s; +//非递归 +int GetHeight(BiTree T) { + if (!T) + return 0; + int level = 0; + Node* front, * last; + last = front = T; + q.push(T); + while (!q.empty()) { + front = q.front(); + if (front->lchild) + q.push(front->lchild); + if (front->rchild) + q.push(front->rchild); + if (front == last) { + level++; + last = q.back(); + } + q.pop(); + } + return level; +} + +//递归 +int GetH(BiTree T) { + if (T == NULL) + return 0; + int Lh = GetH(T->lchild); + int Lr = GetH(T->rchild); + return Lh > Lr ? (Lh + 1) : (Lr + 1); +} + +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 << "树的高度为:" << GetH(T) << endl; + cout << "树的高度为:" << GetHeight(T) << endl; + cout << endl; + return 0; +} + +/* + + A + B C + D E F G + H I Ø J Ø K Ø Ø + Ø Ø Ø Ø Ø Ø Ø Ø + + + 树的图片链接:https://s3.ax1x.com/2021/02/03/yKIbiq.png +*/ \ No newline at end of file diff --git a/树与二叉树/二叉树的遍历和线索二叉树/P149.6.cpp b/树与二叉树/二叉树的遍历和线索二叉树/P149.6.cpp new file mode 100644 index 0000000..75f8548 --- /dev/null +++ b/树与二叉树/二叉树的遍历和线索二叉树/P149.6.cpp @@ -0,0 +1,111 @@ +#include +#include +#include +#include +#include +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 != NULL) { + InOrder(T->lchild); + visit(T); + InOrder(T->rchild); + } +} + +void PostOrder(BiTree T) { + if (T != NULL) { + PostOrder(T->lchild); + PostOrder(T->rchild); + visit(T); + } +} + +#pragma endregion + +//P149.6 +//设一棵二又树中各结点的值互不相同,其先序遍历序列和中序遍历序列分别存于两个 +//维数组A[1…n]和B[1…n]中, 试编写算法建立该二叉树的二叉链表 + +queue q; +stack s; + +BiTree BuildTree(string A, string B, int l1, int h1, int l2, int h2) { + + // A = "ABDHIEJCFKG", B = "HDIBEJAFKCG"; + BiTree root = new Node; + root->val = A[l1]; + int i; + for (i = l2; B[i] != root->val; i++); + int llen = i - l2; + int rlen = h2 - i; + if (llen) + root->lchild = BuildTree(A, B, l1 + 1, l1 + llen, l2, l2 + llen - 1); + else + root->lchild = NULL; + if (rlen) + root->rchild = BuildTree(A, B, h1 - rlen + 1, h1, h2 - rlen + 1, h2); + else + root->rchild = NULL; + return root; +} + +int main() +{ + BiTree T; +// cout << "请输入先序遍历顺序下各个结点的值,'#'表示没有结点:" << endl; + string Pre = "ABDHIEJCFKG", In = "HDIBEJAFKCG"; +// input: A B D H # # I # # E # J # # C F # K # # G # # +// CreateBiTree(T); + T = BuildTree(Pre, In, 0, Pre.size() - 1, 0, In.size() - 1); + cout << "后序遍历验证:" << endl; + PostOrder(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 +*/ \ No newline at end of file diff --git a/树与二叉树/二叉树的遍历和线索二叉树/P150.7.cpp b/树与二叉树/二叉树的遍历和线索二叉树/P150.7.cpp new file mode 100644 index 0000000..f23fa8a --- /dev/null +++ b/树与二叉树/二叉树的遍历和线索二叉树/P150.7.cpp @@ -0,0 +1,126 @@ +#include +#include +#include +#include +#include +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 != NULL) { + InOrder(T->lchild); + visit(T); + InOrder(T->rchild); + } +} + +void PostOrder(BiTree T) { + if (T != NULL) { + PostOrder(T->lchild); + PostOrder(T->rchild); + visit(T); + } +} + +#pragma endregion + +//P150.7 +//二又树按二叉链表形式存储, 写一个判别给定二叉树是否是完全二叉树的算法 + +queue q; +stack s; + +bool Judge_Comtrees(BiTree T) { + q.push(T); + while (!q.empty()) { + Node* t = q.front(); + q.pop(); + if (t) { + q.push(t->lchild); + q.push(t->rchild); + } + else { + while (!q.empty()) { + Node* x = q.front(); + q.pop(); + if (x) + return false; + } + } + } + return true; +} + +int main() +{ + BiTree T; + cout << "请输入先序遍历顺序下各个结点的值,'#'表示没有结点:" << endl; +// input(完全二叉树): A B D H # # I # # E G # # K # # C F L # # M # # G N # # # +// input(普通二叉树): A B D H # # I # # E # J # # C F # K # # G # # + CreateBiTree(T); + cout << "中序遍历:" << endl; + InOrder(T); + cout << endl; + if (Judge_Comtrees(T)) + cout << "是完全二叉树" << endl; + else + cout << "不是完全二叉树" << 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 + +*/ \ No newline at end of file