6-1.采用先序遍历方法建立一棵二叉树,对其进行中序遍历,输出结果。
code:
#include <bits/stdc++.h>
using namespace std;
#define TElemType char
#define Status int
typedef struct BiTNode
{
TElemType data;
struct BiTNode *lchild, *rchild;
} BiTNode, *BiTree;
Status CreateBiTree(BiTree &T){
TElemType ch;
scanf("%c", &ch);
if (ch == '#')
T = NULL;
else
{
T = (BiTree)malloc(sizeof(BiTNode));
if (!T)
exit(_OVERFLOW);
T->data = ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
return 1;
}
Status Visit(TElemType e){
cout << e <<" ";
return 1;
}
Status InOrderTraverse(BiTree T,Status Visit(TElemType e)){
if(T!=NULL){
InOrderTraverse(T->lchild, Visit);
Visit(T->data);
InOrderTraverse(T->rchild, Visit);
}
return 1;
}
int main(){
printf("测试代码\n");
BiTree T;
T = (BiTree)malloc(sizeof(BiTNode));
printf("请给二叉树按照先序方式依次输入结点的值(空结点为#):\n");
CreateBiTree(T);
printf("中序方式遍历结果:\n");
InOrderTraverse(T,Visit);
printf("\n");
return 0;
}
6-2.实现二叉树的层序遍历的算法
code:
#include <bits/stdc++.h>
using namespace std;
#define TElemType char
#define Status int
typedef struct BiTNode
{
TElemType data;
struct BiTNode *lchild, *rchild;
} BiTNode, *BiTree;
Status CreateBiTree(BiTree &T){
TElemType ch;
scanf("%c", &ch);
if (ch == '#')
T = NULL;
else
{
T = (BiTree)malloc(sizeof(BiTNode));
if (!T)
exit(_OVERFLOW);
T->data = ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
return 1;
}
Status Visit(TElemType e){
cout << e <<" ";
return 1;
}
#define QElemType BiTree
#define Status int
typedef struct QNode{
QElemType data;
struct QNode *next;
} QNode, *QueuePtr;
typedef struct{
QueuePtr front;
QueuePtr rear;
} LinkQueue;
Status QueueEmpty(LinkQueue Q){
if (Q.front == Q.rear)
return 1;
else
return 0;
}
Status InitQueue(LinkQueue &Q){
Q.front = Q.rear = (QueuePtr)malloc(sizeof(QNode));
if (!Q.front)
exit(_OVERFLOW);
Q.front->next = NULL;
return 1;
}
Status EnQueue(LinkQueue &Q, QElemType e){
QueuePtr p = (QueuePtr)malloc(sizeof(QNode));
if (!p)
exit(_OVERFLOW);
p->data = e;
p->next = NULL;
Q.rear->next = p;
Q.rear = p;
return 1;
}
Status DeQueue(LinkQueue &Q, QElemType &e){
QueuePtr p;
if (Q.front == Q.rear)
return 0;
p = Q.front->next;
e = p->data;
Q.front->next = p->next;
if (Q.rear == p)
Q.rear = Q.front;
free(p);
return 1;
}
Status LevelOrderTraverse(BiTree &T){
LinkQueue lq;
InitQueue(lq);
QElemType q;
EnQueue(lq, T);
while (QueueEmpty(lq) != 1){
DeQueue(lq, q);
printf("%c ", q->data);
if(q->lchild)
EnQueue(lq, q->lchild);
if(q->rchild)
EnQueue(lq, q->rchild);
}
return 1;
}
int main(){
printf("测试代码\n");
BiTree T;
T = (BiTree)malloc(sizeof(BiTNode));
printf("请给二叉树按照先序方式依次输入结点的值(空结点为#):\n");
CreateBiTree(T);
printf("层序方式遍历结果:\n");
LevelOrderTraverse(T);
printf("\n");
return 0;
}
6-3.建立二叉树,实现二叉树的先序、中序、后序遍历的递归算法。
code:
#include <bits/stdc++.h>
using namespace std;
#define TElemType char
#define Status int
typedef struct BiTNode
{
TElemType data;
struct BiTNode *lchild, *rchild;
} BiTNode, *BiTree;
Status CreateBiTree(BiTree &T){
TElemType ch;
scanf("%c", &ch);
if (ch == '#')
T = NULL;
else
{
T = (BiTree)malloc(sizeof(BiTNode));
if (!T)
exit(_OVERFLOW);
T->data = ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
return 1;
}
Status Visit(TElemType e){
cout << e <<" ";
return 1;
}
Status PreOrderTraverse(BiTree T,Status Visit(TElemType e)){
if(T){
Visit(T->data);
PreOrderTraverse(T->lchild, Visit);
PreOrderTraverse(T->rchild, Visit);
}
return 1;
}
Status InOrderTraverse(BiTree T,Status Visit(TElemType e)){
if(T!=NULL){
InOrderTraverse(T->lchild, Visit);
Visit(T->data);
InOrderTraverse(T->rchild, Visit);
}
return 1;
}
Status PostOrderTraverse(BiTree T,Status Visit(TElemType e)){
if(T!=NULL){
PostOrderTraverse(T->lchild, Visit);
PostOrderTraverse(T->rchild, Visit);
Visit(T->data);
}
return 1;
}
int main(){
printf("测试代码\n");
BiTree T;
T = (BiTree)malloc(sizeof(BiTNode));
printf("请给二叉树按照先序方式依次输入结点的值(空结点为#):\n");
CreateBiTree(T);
printf("先序方式遍历结果:\n");
PreOrderTraverse(T,Visit);
printf("\n");
printf("中序方式遍历结果:\n");
InOrderTraverse(T,Visit);
printf("\n");
printf("后序方式遍历结果:\n");
PostOrderTraverse(T,Visit);
printf("\n");
return 0;
}
6-4. 建立二叉树,实现二叉树的先序、中序遍历的非递归算法。
code:
#include <bits/stdc++.h>
using namespace std;
#define TElemType char
#define Status int
typedef struct BiTNode
{
TElemType data;
struct BiTNode *lchild, *rchild;
} BiTNode, *BiTree;
Status CreateBiTree(BiTree &T){
TElemType ch;
scanf("%c", &ch);
if (ch == '#')
T = NULL;
else
{
T = (BiTree)malloc(sizeof(BiTNode));
if (!T)
exit(_OVERFLOW);
T->data = ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
return 1;
}
#define SElemType BiTree
typedef struct SNode {
SElemType data;
struct SNode *next;
} SNode, *LinkStack;
Status InitStack(LinkStack &S) {
S = (LinkStack)malloc(sizeof(SNode));
if(!S)
exit(_OVERFLOW);
S->next = NULL;
return 1;
}
Status DestroyStack(LinkStack &S) {
LinkStack p = S->next, ptmp;
while(p) {
ptmp = p->next;
free(p);
p = ptmp;
}
free(S);
return 1;
}
Status ClearStack(LinkStack &S) {
LinkStack p = S->next, ptmp;
while(p) {
ptmp = p->next;
free(p);
p = ptmp;
}
S->next = NULL;
return 1;
}
Status StackEmpty(LinkStack S) {
if(S->next == NULL)
return 1;
else
return 0;
}
int StackLength(LinkStack S) {
int n = 0;
LinkStack p = S->next;
while(p) {
n++;
p = p->next;
}
return n;
}
Status GetTop(LinkStack S, SElemType &e) {
if ( S->next == NULL )
return 0;
e = S->next->data;
return 1;
}
Status Push(LinkStack &S, SElemType e) {
LinkStack p = (LinkStack)malloc(sizeof(SNode));
p->data = e;
p->next = S->next;
S->next = p;
return 1;
}
Status Pop(LinkStack &S, SElemType &e) {
if (S->next == NULL)
return 0;
e = S->next->data;
LinkStack ptmp = S->next->next;
free(S->next);
S->next = ptmp;
return 1;
}
Status Visit(TElemType e){
cout << e << " ";
return 1;
}
Status PreOrderTraverse1(BiTree T,Status Visit(TElemType e))
{
if (T == NULL)
return 0;
BiTree p;
LinkStack s;
InitStack(s);
Push(s, T);
while (!StackEmpty(s))
{
while(GetTop(s,p) && p){
if(!Visit(p->data))
return 0;
Push(s, p->lchild);
}
Pop(s, p);
if(!StackEmpty(s)){
Pop(s, p);
Push(s, p->rchild);
}
}
return 1;
}
Status PreOrderTraverse2(BiTree T,Status Visit(TElemType e))
{
if (T == NULL)
return 0;
BiTree p = T, e;
LinkStack s;
InitStack(s);
while (p || !StackEmpty(s))
{
if(p){
if(!Visit(p->data))
return 0;
Push(s, p);
p = p->lchild;
}else{
Pop(s, p);
p = p->rchild;
}
}
return 1;
}
Status InOrderTraverse1(BiTree T,Status Visit(TElemType e))
{
if (T == NULL)
return 0;
BiTree p;
LinkStack s;
InitStack(s);
Push(s, T);
while (!StackEmpty(s))
{
while(GetTop(s,p) && p){
Push(s, p->lchild);
}
Pop(s, p);
if(!StackEmpty(s)){
Pop(s, p);
if(!Visit(p->data))
return 0;
Push(s, p->rchild);
}
}
return 1;
}
Status InOrderTraverse2(BiTree T,Status Visit(TElemType e))
{
if (T == NULL)
return 0;
BiTree p = T, e;
LinkStack s;
InitStack(s);
while (p || !StackEmpty(s))
{
if(p){
Push(s, p);
p = p->lchild;
}else{
Pop(s, p);
if(!Visit(p->data))
return 0;
p = p->rchild;
}
}
return 1;
}
Status PostOrderrTraverse2(BiTree T, Status(*Visit)(TElemType e))
{
if (T == NULL)
return 0;
BiTree p = T, r = NULL;
LinkStack s;
InitStack(s);
while ( p != NULL || !StackEmpty(s))
{
if (p) {
Push(s, p);
p = p->lchild;
}
else
{
GetTop(s, p);
if (p->rchild && p->rchild != r) {
p = p->rchild;
Push(s, p);
p = p->lchild;
}
else
{
Pop(s, p);
Visit(p->data);
r = p;
p = NULL;
}
}
}
return 1;
}
int main(){
printf("测试代码\n");
BiTree T;
T = (BiTree)malloc(sizeof(BiTNode));
printf("请给二叉树按照先序方式依次输入结点的值(空结点为#):\n");
CreateBiTree(T);
printf("先序方式遍历结果:\n");
PreOrderTraverse2(T,Visit);
printf("\n");
printf("中序方式遍历结果:\n");
InOrderTraverse2(T,Visit);
printf("\n");
printf("后序方式遍历结果:\n");
PostOrderrTraverse2(T,Visit);
printf("\n");
return 0;
}
6-5.(选做) 建立二叉树,实现二叉树的后序遍历的非递归算法。
code见6.4;
💖推荐💖:
🚩🚩🚩有问题欢迎评论区留言讨论,看到后我就会回复的!!!!