【資料結構】二叉樹的相關操作(待更)
阿新 • • 發佈:2018-11-06
#include "stdio.h" #include "stdlib.h" typedef struct node { char data; struct node *rchild,*lchild; }bintnode; typedef bintnode *bintree;//指向該結構體的指標 /*二叉樹遍歷的遞迴實現*/ /*前序遍歷*/ void preorder(bintree t) { if(t) { printf("%d",t->data); preorder(t->lchild); preorder(t->rchild); } } /*中序遍歷*/ void inorder(bintree t) { if(t) { inorder(t->lchild); printf("%d",t->data); inorder(t->rchild); } } /*後序遍歷*/ void postorder(bintree t) { if(t) { postorder(t->lchild); postorder(t->rchild); printf("%d",t->data); } } /*根據前序遍歷的結果建立一棵二叉樹*/ bintree creatbintree() { char ch; bintree t; if((ch=getchar())=='#') { t=NULL; } else { t=(bintnode*)malloc(sizeof(bintnode)); t->data=ch; t->lchild=creatbintree(); t->rchild=creatbintree(); } return t; } int main () { bintree node; return 0; }