二叉树的横向显示

来源:互联网 发布:c声明类编程题及答案 编辑:程序博客网 时间:2024/05/29 17:28
#include <stdio.h>#include <malloc.h>typedef char ElemType;typedef struct Node{    ElemType data;    Node *Lchild,*Rchild;} BiTNode,*BiTree;BiTree CreateBiTree();//建立二叉树void PrintTree(BiTree T,int h);//二叉树的横向显示int main(void){    BiTree root=CreateBiTree();    PrintTree(root,1);    return 0;}BiTree CreateBiTree()//建立二叉树{    ElemType x;    BiTree T;    scanf("%c",&x);    if(x=='#')        T=NULL;    else    {        T=(BiTree)malloc(sizeof(BiTNode));        T->data=x;        T->Lchild=CreateBiTree();        T->Rchild=CreateBiTree();    }    return T;}void PrintTree(BiTree T,int h)//二叉树的横向显示{    if(T==NULL)        return;    PrintTree(T->Rchild,h+1);    for(int i=0; i<h; i++)        printf(" ");    printf("%c\n",T->data);    PrintTree(T->Lchild,h+1);}

原创粉丝点击