本文解决的问题是:
构建一个二叉排序树,随机产生20个树,并实现该二叉树的三种深度遍历(递归和非递归)和一种广度遍历。
实现代码如下:
#include<stdio.h> #include<stdlib.h> #define MaxSize 100 typedef struct btnode { int data; int BF; struct btnode *lchild,*rchild; }Btnode,*bitree; //定义队列 typedef struct{ bitree data[MaxSize]; int front,rear; }SqQueue; //定义栈 typedef struct{ bitree data[MaxSize]; int top; }SqStack; //创建二叉树 bitree creatTree(bitree root) { int i; int temp; bitree r,s,pre; for(i=0;i<20;i++) { temp=rand()%90+11; printf("本次插入的是:%d\n",temp); r=(bitree)malloc(sizeof(btnode)); r->data=temp; r->lchild=NULL; r->rchild=NULL; pre = NULL; s=root; if(root->data == NULL) { root=r; continue; } while(s) { if(temp==s->data) { //当前结点与新传入的值相等 i--; pre=NULL; printf("本次%d为重复结点,请重新生成结点!!\n",temp); break; }else if(temp<s->data) { //当前结点大于新传入值,遍历左结点 pre = s; s = s->lchild; }else{ //当前结点小于新传入值,遍历右结点 pre = s; s = s->rchild; } } if(pre != NULL) { //找到结点(父节点)插入位置 if(temp<pre->data) { //插入左子树 pre->lchild = r; }else{ //插入右子树 pre->rchild = r; } } } return root; } //前序遍历递归 void preErgodic_recu(bitree root) { if(root) { printf("%d ",root->data); preErgodic_recu(root->lchild); preErgodic_recu(root->rchild); } } //前序遍历非递归 void preErgodic_recu_no(bitree root) { SqStack q; q.top=0; q.data[q.top]=root; bitree p=root; while(p||q.top != 0) { if(p) { printf("%d ",p->data); q.data[q.top++] = p; p=p->lchild; }else{ p=q.data[--q.top]; p=p->rchild; } } } //中序遍历递归 void midErgodic_recu(bitree root) { if(root) { midErgodic_recu(root->lchild); printf("%d ",root->data); midErgodic_recu(root->rchild); } } //中序遍历非递归 void midErgodic_recu_no(bitree root) { SqStack q; q.top=0; q.data[q.top]=root; bitree p=root; while(p||q.top != 0) { if(p) { q.data[q.top++] = p; p=p->lchild; }else{ p=q.data[--q.top]; printf("%d ",p->data); p=p->rchild; } } } //后序遍历递归 void postErgodic_recu(bitree root) { if(root) { postErgodic_recu(root->lchild); postErgodic_recu(root->rchild); printf("%d ",root->data); } } //后序遍历非递归 void postErgodic_recu_no(bitree root) { SqStack q; q.top=0; q.data[q.top]=root; bitree p=root; bitree r=NULL; while(p||q.top != 0) { if(p) { q.data[q.top++] = p; p=p->lchild; }else{ p=q.data[q.top-1]; if(p->rchild&&p->rchild != r) { p=p->rchild; q.data[q.top++] = p; p=p->lchild; }else { p=q.data[--q.top]; printf("%d ",p->data); r=p; p=NULL; } } } } //入队 void push_queue(SqQueue *q,bitree root) { q->data[q->rear]=root; q->rear++; } //出队 bitree pop_queue(SqQueue *q) { return q->data[q->front++]; } //广度遍历 void breafthTraversal(bitree root) { SqQueue q; q.front = q.rear = 0; if(!root) { printf("二叉树为空!!\n"); return; } //根节点入队 push_queue(&q,root); while(q.front != q.rear) { bitree temp=pop_queue(&q); printf("%d ",temp->data); if(temp->lchild != NULL) { push_queue(&q,temp->lchild); } if(temp->rchild != NULL) { push_queue(&q,temp->rchild); } } } int main() { bitree root; root=(bitree)malloc(sizeof(btnode)); root->data=NULL; root=creatTree(root); //打印树 /*****************************三种深度遍历******************************************/ /******************1.1 前序深度遍历*****************/ printf("前序遍历--递归:\n"); preErgodic_recu(root); printf("\n"); printf("前序遍历--非递归:\n"); preErgodic_recu_no(root); printf("\n"); printf("\n"); printf("\n"); /******************1.2 中序深度遍历*****************/ printf("中序遍历--递归:\n"); midErgodic_recu(root); printf("\n"); printf("中序遍历--非递归:\n"); midErgodic_recu_no(root); printf("\n"); printf("\n"); printf("\n"); /******************1.3 后序深度遍历*****************/ printf("后序遍历--递归:\n"); postErgodic_recu(root); printf("\n"); printf("后序遍历--非递归:\n"); postErgodic_recu_no(root); printf("\n"); printf("\n"); printf("\n"); /*****************************一种广度遍历******************************************/ printf("广度遍历的结果为:\n"); breafthTraversal(root); printf("\n"); return 0; }如有疑问,请及时提出,谢谢!
---------------------------------------------------------------------------------------------------------------------