c的二叉树

来源:互联网 发布:excel数据查重 编辑:程序博客网 时间:2024/05/12 08:52

希望这个二叉树以后有用:

#include <stdio.h>#include <stdlib.h>typedef struct Tre{    char data;    Tre *left;    Tre *right;}Tree  ;Tree *Creatlist(Tree *tree);void Printlist(Tree *tree);int main(){Tree *tr,*trr;trr=Creatlist(tr);Printlist(trr);printf("/n");    return 0;}Tree *Creatlist(Tree *tree){    char i;    scanf("%c",&i);    if(i=='?')    {    return  NULL;}    else    {        tree=(Tree*)malloc(sizeof(Tree));        tree->data=i;    tree->left=Creatlist(tree->left);//必须写成tree->left=Creatlist(tree->left)形式    tree->right=Creatlist(tree->right);//如果缺少tree->left或者tree->right就会出错。    }    return tree;    }void Printlist(Tree *tree){    if(tree)    {        printf("%c",tree->data);        Printlist(tree->left);        Printlist(tree->right);    }}

二叉树的执行过程如果不懂,可以在宁波大学网络教学上下一个演示软件,那个软件做的非常不错。

这只是用先序遍历写出来的程序。中序遍历和后序遍历自己以后在玩玩。呵呵

 

 

 

 

 

原创粉丝点击