#includeusing namespace std; #define TElemType char; #define Status int; typedef struct BiTNode { TElemType data; struct BiTNode* lchild, * rchild; }BiTNode,*BiTree; Status InitBiTree(BiTree &T); Status DestroyBiTree(BiTree& T); Status CreateBiTree(BiTree& T); void ClearBiTree(BiTree& T); Status BiTreeEmpty(BiTree T); int BiTreeDepth(BiTree T); Status Root(BiTree T); Status Value(BiTree T,TElemType e); Status Assign(BiTree& T,TElemType e,TElemType value); Status Parent(BiTree T, TElemType e); Status LeftChild(BiTree T, TElemType e); Status RightChild(BiTree T, TElemType e); Status LeftSibling(BiTree T, TElemType e); Status RightSibling(BiTree T, TElemType e); Status InsertChild(BiTree T, BiTree p, int LR, BiTree c); Status DeleteChild(BiTree T, BiTree p, int LR); Status InitBiTree(BiTree& T)//构造空二叉树 { T = NULL; return 1; } Status DestroyBiTree(BiTree& T)//销毁二叉树 { if ((T)->lchild) DetroyBiTree(T->lchild); if ((T)->rchild) DetroyBiTree(T->rchild); free(T); T = NULL; return 1; } Status CreateBiTree(BiTree& T)//根据定义构造二叉树 { TElemType ch; scanf("%c", &ch); if (ch == ' ') T = NULL; else { T = (BiTree)malloc(sizeof(BiNode)); if (!T) exit(_OVERFLOW); T->data = ch; CreateBiTree(T->lchild); CreateBiTree(T->rchild); } return 1; } void ClearBiTree(BiTree& T)//将二叉树T清为空树 { if (T) { if ((T)->lchild) ClearBiTree(T->lchild); if ((T)->rchild) ClearBiTree(T->rchild); free(T); T = NULL; } } Status BiTreeEmpty(BiTree& T) { if (T) return 0; else return 1; } int BiTreeDepth(BiTree T) { int i, j; if (!T) return 0; if (T->lchild) i = BiTreeDepth(T->lchild); else i = 0; if (T->rchild) i = BiTreeDepth(T->rchild); else i = 0; return i > j ? i + 1 : j + 1; } Status Root(BiTree T)//二叉树存在则返回二叉树的根 { cout << T->data << endl; return 1; } Status Value(BiTree T, TElemType e)//二叉树存在,e是T中某个结点,返回e的值 { if (T->data == e) return 1; else { int i = 1, j = 0; if (T->lchild) i = Value(T->lchlid, e); if (T->rchild) j = Value(T->rchlid, e); if (i || j) return 1; else return 0; } return 0; } Status Assign(BiTree& T, TElemType e, TElemType value)//结点e赋值为value { if (T->data == e) { T->data = value; return 1; } else { int i = 0, j = 0; if (T->lchild) { i = Assign(T->lchlid, e, value); } if (T->rchild) { j = Assign(T->rchild, e, value); } if (i || j)return 1; else return 0; } return 0; } Status Parent(BiTree T, TElemType e)//若e是T的非根结点,则返回它的双亲,否则返回空 { if (T->lchild && T->lchild->data == e) { printf("双亲结点为:%cn", T->data); return 1; } else if (T->rchild && T->rchild->data == e) { printf("双亲结点为:%cn", T->data); return 1; } else { if (T->lchild) { Parent(T->lchlid, e); } if (T->rchild) { Parent(T->rchild, e); } } return 0; } Status LeftChild(BiTree T, TElemType e)//返回e的左孩子,若e无左孩子,则返回空 { if (T->data == e && T->lchild) { printf("左孩子结点为:%cn", T->lchlid->data); return 1; } else { if (T->lchild) { LeftChild(T->lchild, e); } if (T->rchild) { LeftChild(T->rchild, e); } } return 0; } Status RightChild(BiTree T, TElemType e)//返回e的右孩子,若e无右孩子,则返回空 { if (T->data == e && T->rchild) { printf("右孩子结点为:%cn", T->rchild->data); return 1; } else { if (T->lchild) { RightChild(T->lchild, e); } if (T->rchild) { RightChild(T->rchild, e); } } return 0; } Status LeftSibling(BiTree T, TElemType e)//返回e的左兄弟,若e是T的左孩子或无左兄弟,则返回空 { if (T->lchild && T->rchild->data == e) { printf("左兄弟结点为:%cn", T->lchild->data); return 1; } else { if (T->lchild) { LeftSiblint(T->ichild, e); } if (T->rchild) { LeftSibling(T->rchild, e); } } return 0; } Status RightSibling(BiTree T, TELemType e)//返回e的右兄弟,若e是T的右孩子或无右兄弟,则返回空 { if (T->rchild && T->lchild->data == e) { printf("右兄弟结点为:%cn", T->rchild->data); return 1; } else { if (T->lchild) { RightSibling(T->lchild, e); } if (T->rchild) { RightSibling(T->rchild, e); } } return 0; } //二叉树T存在,p指向T中某个结点,LR为0或1,非空二叉树c与T不相交且右子树为空 //返回结果:根据LR为0或1,插入c为T中p所指向结点的左或右子树,p所指向结点的原有左或右子树则成为c的右子树 Status InsertChild(BiTree T, BiTree p, int LR, BiTree c) { if (LR == 0)//c的右子树为空,使p的左子树成为c的右子树,并使增加了右子树的c成为p的左子树 { c->rchild = p->lchild; p->lchild = c; } else//c的右子树为空,使p的右子树成为c的右子树,并使增加了右子树的c成为p的右子树 { c->rchild = p->rchild; p->rchild = c; } return 1; } Status DeleteChild(BiTree T, BiTree p, int LR) { if (LR == 0)//删除p的左子树 { DestroyBiTree(p->lchild); } else//删除p的右子树 { DestroyBiTree(p->rchild); } return 1; }



