二叉树的层序遍历是解决各种问题的基础,因此对于考研党而言,熟练写出相关操作至关重要。以下是个人观摩王道考研指导书写出的相关代码(已验证,如有改进之处,欢迎指正!),代码为c++代码,目的是与考试的伪代码匹配上。
先放出运行结果:(注: 二叉树建立采用先序建立,#表示结点为NULL)
- 层序遍历涉及到队列和二叉树的结构定义,代码如下
typedef struct BiTNode { char data; struct BiTNode *lchild, *rchild; } BiTNode, *BiTree; typedef struct LinkNode //链式队列结点 { BiTNode *node; LinkNode *next; } LinkNode; typedef struct { LinkNode *front, *rear; //队列的队头和队尾指针 } LinkQueue;
2.链式队列的相关操作如下
void InitQueue(LinkQueue &Q) { Q.front = Q.rear = (LinkNode *)malloc(sizeof(LinkNode)); //建立头结点 Q.front->next = NULL; //初始化为空 } //判队空 bool isEmpty(LinkQueue Q) { if (Q.front == Q.rear) return true; else return false; } //入队 void EnQueue(LinkQueue &Q, BiTree p) { LinkNode *s = (LinkNode *)malloc(sizeof(LinkNode)); s->node = p; s->next = NULL; Q.rear->next = s; Q.rear = s; } //出队 bool DeQueue(LinkQueue &Q, BiTree &p) { if (isEmpty(Q)) return false; LinkNode *s = Q.front->next; p = s->node; //用变量x返回队头元素 cout << p->data; Q.front->next = s->next; //修改头结点的next指针 if (Q.rear == s) //此次是最后一个结点出队 Q.rear = Q.front; //修改rear指针 free(s); //释放结点空间 return true; }
3.二叉树创建和遍历操作如下
void createBiTNode(BiTree &T) { char ch; cin >> ch; if (ch == '#') T = NULL; else { T = new BiTNode; createBiTNode(T->lchild); T->data = ch; createBiTNode(T->rchild); } } void LevelOrder(BiTree T) { InitQueue(Q); BiTree p; EnQueue(Q, T); while (!isEmpty(Q)) { DeQueue(Q, p); //队头结点出栈 //访问出栈结点 if (p->lchild != NULL) //左子树不为空,则左子树根结点入队 EnQueue(Q, p->lchild); if (p->rchild != NULL) //右子树不为空,则右子树根结点入队 EnQueue(Q, p->rchild); } }
完整代码如下
#include <iostream> using namespace std; typedef struct BiTNode { char data; struct BiTNode *lchild, *rchild; } BiTNode, *BiTree; typedef struct LinkNode //链式队列结点 { BiTNode *node; LinkNode *next; } LinkNode; typedef struct { LinkNode *front, *rear; //队列的队头和队尾指针 } LinkQueue; void createBiTNode(BiTree &T); void InitQueue(LinkQueue &Q); bool isEmpty(LinkQueue Q); void EnQueue(LinkQueue &Q, BiTree p); bool DeQueue(LinkQueue &Q, BiTree &p); void LevelOrder(BiTree T); LinkQueue Q; int main() { BiTree T; createBiTNode(T); LevelOrder(T); return 0; } void InitQueue(LinkQueue &Q) { Q.front = Q.rear = (LinkNode *)malloc(sizeof(LinkNode)); //建立头结点 Q.front->next = NULL; //初始化为空 } //判队空 bool isEmpty(LinkQueue Q) { if (Q.front == Q.rear) return true; else return false; } //入队 void EnQueue(LinkQueue &Q, BiTree p) { LinkNode *s = (LinkNode *)malloc(sizeof(LinkNode)); s->node = p; s->next = NULL; Q.rear->next = s; Q.rear = s; } //出队 bool DeQueue(LinkQueue &Q, BiTree &p) { if (isEmpty(Q)) return false; LinkNode *s = Q.front->next; p = s->node; //用变量x返回队头元素 cout << p->data; Q.front->next = s->next; //修改头结点的next指针 if (Q.rear == s) //此次是最后一个结点出队 Q.rear = Q.front; //修改rear指针 free(s); //释放结点空间 return true; } void createBiTNode(BiTree &T) { char ch; cin >> ch; if (ch == '#') T = NULL; else { T = new BiTNode; createBiTNode(T->lchild); T->data = ch; createBiTNode(T->rchild); } } void LevelOrder(BiTree T) { InitQueue(Q); BiTree p; EnQueue(Q, T); while (!isEmpty(Q)) { DeQueue(Q, p); //队头结点出栈 //访问出栈结点 if (p->lchild != NULL) //左子树不为空,则左子树根结点入队 EnQueue(Q, p->lchild); if (p->rchild != NULL) //右子树不为空,则右子树根结点入队 EnQueue(Q, p->rchild); } }