八.二叉树各种操作的C语言实现 树的一些基本的操作,包括,树的建立,树的深度,
2013-08-15 11:31
921 查看
首先是定义的树的数据结构: typedef struct BiTNode { TElemType data; struct BiTNode *lchild; struct BiTNode *rchild; }BiTNode,BiTree,*BiTree_1; 下面是树的一些基本的操作,包括,树的建立,树的深度,树的各种遍历的递归和非递归实现; Status InitBiTree(BiTree **T) {//构造空二叉树 (*T)=NULL; return OK; } void DestroyBiTree(BiTree **T) {//销毁二叉树 if(*T) { if((*T)->lchild) DestroyBiTree(&((*T)->lchild)); if((*T)->rchild) DestroyBiTree(&((*T)->rchild)); free(*T); (*T)=NULL; } } void CreateBiTree(BiTree **T) {// 按先序次序输入二叉树中节点的值,构造二叉链表示的二叉树T TElemType ch; #ifdef CHAR scanf("%c",&ch); #endif #ifdef INT scanf("%d",&ch); #endif if(ch=='#') (*T) = 0; else { (*T)=(BiTree *)malloc(sizeof(struct BiTNode)); if(!(*T)) exit(OVERFLOW); (*T)->data = ch; CreateBiTree(&((*T)->lchild));//构造左子树 CreateBiTree(&((*T)->rchild));//构造右子树 } } Status BiTreeEmpty(BiTree *T) { //判断二叉树是否为空 if(T) return FALSE; else return TRUE; } #define ClearBiTree DestroyBiTree int BiTreeDepth(BiTree *T) {//返回T的深度 int i,j; if(!T) return 0; if(T->lchild) i=BiTreeDepth(T->lchild); else i=0; if(T->rchild) j= BiTreeDepth(T->rchild); else j = 0; return i>j?i+1:j+1; } TElemType Root(BiTree *T) {// 返回T的根 if(BiTreeEmpty(T)) return Nil; else return T->data; } TElemType Value(BiTree *p) {//p指向T的某个节点,返回p所指向的节点的值 return (p->data); } void Assign(BiTree *p,TElemType value) { //给p所值的节点赋值 value p->data = value; } typedef BiTree_1 QElemType; #include "Queue.h" #include "Queue.c" TElemType Parent(BiTree *T,TElemType e) { // 若e是T 的非根节点,则返回它的双亲,否则返回空 LinkQueue *q; QElemType a; q = (LinkQueue *)malloc(sizeof(struct QNode)); if(T) { InitQueue(&q); EnQueue(q,(T)); } while(!QueueEmpty(q)) { DeQueue(q,&a); if(a->lchild && a->lchild->data == e || a->rchild && a->rchild->data==e) return a->data; else { if(a->lchild) EnQueue(q,a->lchild); if(a->rchild) EnQueue(q,a->rchild); } } free(q); } BiTree *Point(BiTree *T,TElemType s) { // 返回二叉树T中指向元素值为S的节点的指针 LinkQueue *q; BiTree *a; q = (LinkQueue *)malloc(sizeof(struct QNode)); a = (BiTree *)malloc(sizeof(struct BiTNode)); if(T) { InitQueue(&q); EnQueue(q,T); while(!QueueEmpty(q)) { DeQueue(q,&a); if(a->data==s) return a; if(a->lchild) EnQueue(q,a->lchild); if(a->rchild) EnQueue(q,a->rchild); } } free(q); free(a); return NULL; } TElemType LeftChild(BiTree *T,TElemType e) {//返回e的左孩子,若e无左孩子,则返回空 BiTree *a; if(T) { a = Point(T,e); if(a && a->lchild) return a->lchild->data; } return Nil; } TElemType RightChild(BiTree *T,TElemType e) {//返回e的右孩子,若e无左孩子,则返回空 BiTree *a; if(T) { a = Point(T,e); if(a && a->rchild) return a->rchild->data; } return Nil; } TElemType LeftSibling(BiTree *T,TElemType e) {//返回e的左兄弟,若e是T的左孩子或无左孩子,则返回空 TElemType a; BiTree *p; if(T) { a = Parent(T,e); p= Point(T,a); if(p->lchild && p->rchild && p->rchild->data ==e) return p->lchild->data; } return Nil; } TElemType RightSibling(BiTree *T,TElemType e) {//返回e的右兄弟,若e是T的左孩子或无左孩子,则返回空 TElemType a; BiTree *p; if(T) { a= Parent(T,e); p = Point(T,a); if(p->lchild && p->rchild && p->lchild->data ==e) return p->rchild->data; } return Nil; } Status InsertChild(BiTree *p,int LR,BiTree *c) {// 根据LR为0或1,插入c为T中p所指节点的左或右孩子,p所指节点的原有左或右子树,则成为c的右子树 if(p) { if(LR == 0) { c->rchild = p->lchild; p->lchild = c; } else { c->rchild = p->rchild; p->rchild =c; } return OK; } return ERROR; } Status DeleteChild(BiTree *p,int LR) {//根据LR为0或1,删除T中p所指向的左或右子树 if(p) { if(LR == 0) ClearBiTree(&(p->lchild)); else ClearBiTree(&(p->rchild)); return OK; } return ERROR; }
相关文章推荐
- 八.二叉树各种操作的C语言实现 深度遍历求深度,广度遍历求深度,交换左右子树,求叶子节点数
- 二叉树的建立(非递归建立与定义建立)与基本操作(广度和深度遍历,求叶子树高)实现
- 数据结构(C语言实现) - 二叉树的基本操作(建立,遍历,结点数,叶子结点数,高度,按树状打印,输出叶子结点等)
- C语言实现二叉树的基本操作---创建、遍历、求深度、求叶子结点
- 二叉树的建立与输出以及其他一些相关操作(递归算法实现) C语言
- 二叉树基本操作的递归实现(二叉树建立,先序,中序,后序,深度的递归遍历。广度优先,高度优先的非递归遍历)
- C语言实现二叉树的基本操作---创建、遍历、求深度、求叶子结点
- c语言实现二叉树的基本操作--二叉链表存储
- C语言实现二叉树的基本操作
- C语言实现二叉树的基本操作
- java语言实现的二叉树的各种操作(包括递归与非递归遍历二叉树,求二叉树的高度,节点总数,叶子节点等)
- 数据结构.二叉树的基本操作(C语言实现)
- 八.二叉树各种操作的C语言实现 二叉树中各种遍历的非递归和递归算法的实现
- C语言实现二叉树的各种遍历及求解深度
- C语言实现二叉树的基本操作
- c语言实现二叉树的基本操作
- 二叉树的一些基本操作(括号表示法,宽度,深度,结点个数,叶子节点个数)
- 二叉树实现(包括遍历等各种操作,递归与非递归)
- 打印菜单界面,用c语言实现二叉树的基本操作
- 算法与数据结构-二叉树的基本操作C语言实现