void DispBTNode(BTNode *b) { if (b!=NULL) { printf("%c",b->data); if (b->lchild!=NULL || b->rchild!=NULL) { printf("("); /*有孩子结点时才输出(*/ DispBTNode(b->lchild); /*递归处理左子树*/ if (b->rchild!=NULL) printf(","); /*有右孩子结点时才输出,*/ DispBTNode(b->rchild); /*递归处理右子树*/ printf(")"); /*有孩子结点时才输出)*/ } } }
void DispBTNode(BTNode *b) { if (b) { printf("%c", b->data); if (b->lchild || b->rchild) { printf("("); DispBTNode(b->lchild); if (b->rchild) printf(","); DispBTNode(b->rchild); printf(")"); } } }
void main() { BTNode *b; CreateBTNode(b,"A(B(D(,G)),C(E,F))"); printf("b:");DispBTNode(b);printf("\n"); printf("层次遍历序列:");LevelOrder(b);printf("\n"); }
void DispBTNode(BTNode *b) //输出二叉树DispBTNode(*b) { if (b != NULL) { printf("%c", b->data); if (b->lchild != NULL || b->rchild != NULL) { printf("("); DispBTNode(b->lchild); //递归处理左子树 if (b->rchild != NULL) printf(","); DispBTNode(b->rchild); //递归处理右子树 printf(")"); } } }
void main() { BTNode *b; ElemType x='G'; CreateBTNode(b,"A(B(D(,G)),C(E,F))"); printf("b:");DispBTNode(b);printf("\n"); printf("结点%c的所有祖先:",x); ancestor(b,'G');printf("\n"); }
void main() { BTNode *b; char s[MaxSize]="a+b*c-e/f"; printf("\n"); printf(" 代数表达式%s\n",s); b=CRTree(s,0,strlen(s)-1); printf(" 对应二叉树:"); DispBTNode(b); printf("\n\n"); }
void main() { BTNode *b; int h; ElemType x; CreateBTNode(b,"A(B(D(,G)),C(E,F))"); printf("b:");DispBTNode(b);printf("\n"); printf("结点值:"); scanf("%c",&x); h=Level(b,x,1); if (h==0) printf("b中不存在%c结点\n",x); else printf("在b中%c结点的层次为%d\n",x,h); }
void main() { BTNode *b; ElemType path[MaxSize],longpath[MaxSize]; int i,longpathlen=0; CreateBTNode(b,"A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))"); printf("二叉树b:");DispBTNode(b);printf("\n"); printf("b的叶子节点:");DispLeaf(b);printf("\n"); printf("AllPath:\n");AllPath(b); printf("AllPath1:\n");AllPath1(b,path,0); LongPath(b,path,0,longpath,longpathlen); printf("第一条最长逆路径长度:%d\n",longpathlen); printf("第一条最长逆路径:"); for (i=longpathlen-1;i>=0;i--) printf("%c ",longpath[i]); printf("\n"); DestroyBTNode(b); }
int main(int argc, char **argv) { BTNode *bt; int height, width, nleaf, nnode; char exp[] = "A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))"; bt = CreateBTNode(exp); DispBTNode(bt); puts(""); nnode = CountBTNode(bt); nleaf = CountLeaf(bt); printf("%d %d\n", nnode, nleaf); FindNode(bt, 'H'); height = BTNodeHeight(bt); width = CountWidth(bt); printf("%d %d\n", height, width-1); return 0; }