一、二叉树的链式存储
树结点数据结构
typedef char BiElemType;
typedef struct BiTNode{BiElemType c;struct BiTNode *lchild;struct BiTNode *rchild;
}BiTNode,*BiTree;树中任何一个结点都是一个结构体,它的空间是通过malloc申请出来的
二、二叉树层次建树
#include <stdio.h>
#include <stdlib.h>typedef char BiElemType;
typedef struct BiTNode{BiElemType c;struct BiTNode *lchild;struct BiTNode *rchild;
}BiTNode,*BiTree;//tag结构体是辅助队列使用的
typedef struct tag{BiTree p; //树的某一个结点的地址值struct tag *pnext;
}tag_t,*ptag_t; int main(){BiTree pnew; //用来指向新申请的树结点 BiTree tree=NULL; //tree是指向树根,代表树 //phead队列头,ptail队列尾ptag_t phead=NULL,ptail=NULL,listpnew=NULL,pcur=NULL;char c;while(scanf("%c",&c)){if(c=='\n'){break; //读到换行就结束 }//calloc申请的空间大小是两个参数直接相乘,并对空间进行初始化,赋值为0 pnew=(BiTree)calloc(1,sizeof(BiTNode)); pnew->c=c;listpnew=(ptag_t)calloc(1,sizeof(tag_t)); //给队列结点申请空间 listpnew->p=pnew;//如果是树的第一个结点if(tree==NULL){tree=pnew; //tree指向树的根结点 phead=listpnew; //第一个结点既是队列头又是队列尾 ptail=listpnew;pcur=listpnew; //pcur要指向进入树的父亲元素 continue;}else{//让元素先入队列ptail->pnext=listpnew;ptail=listpnew;} //接下来把pnew结点放入树中 if(pcur->p->lchild==NULL){pcur->p->lchild=pnew;}else if(pcur->p->rchild==NULL){pcur->p->rchild=pnew;pcur=pcur->pnext;} }return 0;
}
三、二叉树的前序中序后序遍历
前序遍历:先打印自身,再打印左子树,再打印右子树(preOrder)深度优先遍历
中序遍历:先打印左子树,再打印当前结点,再打印右子树(InOrder)
后序遍历:先打印左子树,再打印右子树,最后打印当前结点(PostOrder)
#include <stdio.h>
#include <stdlib.h>typedef char BiElemType;
typedef struct BiTNode{BiElemType c;struct BiTNode *lchild;struct BiTNode *rchild;
}BiTNode,*BiTree;//tag结构体是辅助队列使用的
typedef struct tag{BiTree p; //树的某一个结点的地址值struct tag *pnext;
}tag_t,*ptag_t; //递归实现
//前序遍历就是深度优先遍历
void PreOrder(BiTree p){if(p!=NULL){//putchar(p->c);printf("%c",p->c);PreOrder(p->lchild); //打印左子树 PreOrder(p->rchild); //打印右子树 }
}
//中序遍历
void InOrder(BiTree p){if(p!=NULL){InOrder(p->lchild); //打印左子树 //putchar(p->c);printf("%c",p->c);InOrder(p->rchild); //打印右子树 }
} //后序遍历
void PostOrder(BiTree p){if(p!=NULL){PostOrder(p->lchild); //打印左子树 PostOrder(p->rchild); //打印右子树 //putchar(p->c);printf("%c",p->c);}
} int main(){BiTree pnew; //用来指向新申请的树结点 BiTree tree=NULL; //tree是指向树根,代表树 //phead队列头,ptail队列尾ptag_t phead=NULL,ptail=NULL,listpnew=NULL,pcur=NULL;char c;while(scanf("%c",&c)){if(c=='\n'){break; //读到换行就结束 }//calloc申请的空间大小是两个参数直接相乘,并对空间进行初始化,赋值为0 pnew=(BiTree)calloc(1,sizeof(BiTNode)); pnew->c=c;listpnew=(ptag_t)calloc(1,sizeof(tag_t)); //给队列结点申请空间 listpnew->p=pnew;//如果是树的第一个结点if(tree==NULL){tree=pnew; //tree指向树的根结点 phead=listpnew; //第一个结点既是队列头又是队列尾 ptail=listpnew;pcur=listpnew; //pcur要指向进入树的父亲元素 continue;}else{//让元素先入队列ptail->pnext=listpnew;ptail=listpnew;} //接下来把pnew结点放入树中 if(pcur->p->lchild==NULL){pcur->p->lchild=pnew;}else if(pcur->p->rchild==NULL){pcur->p->rchild=pnew;pcur=pcur->pnext;} }printf("前序遍历\n"); PreOrder(tree); printf("中序遍历\n"); InOrder(tree); printf("后序遍历\n"); PostOrder(tree); return 0;
}
四、二叉树的层次遍历
层序遍历、广度优先遍历
#include <stdio.h>
#include <stdlib.h>typedef char BiElemType;
typedef struct BiTNode{BiElemType c;struct BiTNode *lchild;struct BiTNode *rchild;
}BiTNode,*BiTree;//tag结构体是辅助队列使用的
typedef struct tag{BiTree p; //树的某一个结点的地址值struct tag *pnext;
}tag_t,*ptag_t;//队列的结构体
typedef BiTree ElemType;
typedef struct LinkNode{ElemType data;struct LinkNode *next;
}LinkNode;
typedef struct{LinkNode *front,*rear; //链表头 链表尾
}LinkQueue; //先进先出//队列的初始化,使用的是带头结点的链表来实现的
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;elsereturn false;
}//入队,尾部插入法
void EnQueue(LinkQueue &Q,ElemType x){LinkNode *s=(LinkNode*)malloc(sizeof(LinkNode));s->data=x;s->next=NULL;Q.rear->next=s;Q.rear=s;
} //出队,头部删除法
bool DeQueue(LinkQueue &Q,ElemType &x){if(Q.front==Q.rear)return false;LinkNode *p=Q.front->next;x=p->data;Q.front->next=p->next;if(Q.rear==p)Q.rear=Q.front;free(p);return true;
} //层序遍历
//层次遍历,层序遍历,广度优先遍历
void LevelOrder(BiTree T){LinkQueue Q;InitQueue(Q);BiTree p; //存储出队的结点 EnQueue(Q,T); //把根入队 while(!IsEmpty(Q)){DeQueue(Q,p);putchar(p->c);if(p->lchild){EnQueue(Q,p->lchild);} if(p->rchild){EnQueue(Q,p->rchild);}}
}
int main(){BiTree pnew; //用来指向新申请的树结点 BiTree tree=NULL; //tree是指向树根,代表树 //phead队列头,ptail队列尾ptag_t phead=NULL,ptail=NULL,listpnew=NULL,pcur=NULL;char c;while(scanf("%c",&c)){if(c=='\n'){break; //读到换行就结束 }//calloc申请的空间大小是两个参数直接相乘,并对空间进行初始化,赋值为0 pnew=(BiTree)calloc(1,sizeof(BiTNode)); pnew->c=c;listpnew=(ptag_t)calloc(1,sizeof(tag_t)); //给队列结点申请空间 listpnew->p=pnew;//如果是树的第一个结点if(tree==NULL){tree=pnew; //tree指向树的根结点 phead=listpnew; //第一个结点既是队列头又是队列尾 ptail=listpnew;pcur=listpnew; //pcur要指向进入树的父亲元素 continue;}else{//让元素先入队列ptail->pnext=listpnew;ptail=listpnew;} //接下来把pnew结点放入树中 if(pcur->p->lchild==NULL){pcur->p->lchild=pnew;}else if(pcur->p->rchild==NULL){pcur->p->rchild=pnew;pcur=pcur->pnext;} }printf("层序遍历\n"); LevelOrder(tree);return 0;
}