skyilyskyily.blog.chinaunix.net
skyily
全部博文(144)
2010年(16)
2009年(128)
Zane_Yu
tasteswe
zwrvvv
xiao888l
zimuqing
leilelei
Phyllis6
jonathan
denghai1
wbdwbd04
itTangze
lifj1234
18141908
AAABug
分类: C/C++
2009-07-09 19:47:46
二叉树的实现:
#include<stdio.h>#include<stdlib.h>typedef struct BiT{ char data; struct BiT *lchild; struct BiT *rchild;}BiTe;BiTe* CreateBiTree(BiTe *T) //构造二叉链表表示的二叉树T{ char ch; printf("please input char\n"); ch = getchar(); getchar(); //去掉回车符的干扰 if(ch == '#') T = NULL; //出口,子节点为空 else { T = (BiTe *)malloc(sizeof(BiTe)); T->data = ch; printf("%c->left \n", T->data); T->lchild=CreateBiTree(T->lchild); printf("%c->right \n", T->data); T->rchild=CreateBiTree(T->rchild); } return T;}
void PreOrderTraverse(BiTe *T) // 先序遍历二叉树T{ if (T) { printf("%c", T->data); PreOrderTraverse(T->lchild); PreOrderTraverse(T->rchild); }}void InOrderTraverse(BiTe *T) // 中序遍历二叉树T{ if (T) { InOrderTraverse(T->lchild); printf("%c", T->data); InOrderTraverse(T->rchild); }}void PostOrderTraverse(BiTe *T) // 后序遍历二叉树T{ if (T) { PostOrderTraverse(T->lchild); PostOrderTraverse(T->rchild); printf("%c", T->data); }} void main() { BiTe *T = NULL; printf("先序建树: "); T = CreateBiTree(T); printf("\n先序遍历: "); PreOrderTraverse(T); printf("\n中序遍历: "); InOrderTraverse(T); printf("\n后序遍历: "); PostOrderTraverse(T); getchar();}
void PreOrderTraverse(BiTe *T) // 先序遍历二叉树T{ if (T) { printf("%c", T->data); PreOrderTraverse(T->lchild); PreOrderTraverse(T->rchild); }}void InOrderTraverse(BiTe *T) // 中序遍历二叉树T{ if (T) { InOrderTraverse(T->lchild); printf("%c", T->data); InOrderTraverse(T->rchild); }}void PostOrderTraverse(BiTe *T) // 后序遍历二叉树T{ if (T) { PostOrderTraverse(T->lchild); PostOrderTraverse(T->rchild); printf("%c", T->data); }}
void main() { BiTe *T = NULL; printf("先序建树: "); T = CreateBiTree(T); printf("\n先序遍历: "); PreOrderTraverse(T); printf("\n中序遍历: "); InOrderTraverse(T); printf("\n后序遍历: "); PostOrderTraverse(T); getchar();}
上一篇:C语言中的位域
下一篇:硬盘安装linux系统
登录 注册