首页 > 代码库 > 二叉树

二叉树

#include<stdio.h>
#include<iostream>
using namespace std;
struct BitreeNode{
    int value;
    BitreeNode *left,*right;
};
BitreeNode creat(BitreeNode *&t)
{
    int ch;
    scanf("%d",&ch);
    if(ch==0) //以0为结束判断
    t=NULL;//根为则判为空树
    else
    {
        t=new BitreeNode;
        t->value=http://www.mamicode.com/ch;
        creat(t->left);
        creat(t->right);
    }
}
void xshow(BitreeNode *t)//先序遍历 
{
    if(t!=NULL)
    {
        printf("%d\t",t->value);
        xshow(t->left);
        xshow(t->right);        
    }
}
void zshow(BitreeNode *t)//中序遍历二叉树
{
    if(t!=NULL)
    {
        zshow(t->left);
        printf("%d\t",t->value);
        zshow(t->right);        
    }
}
void hshow(BitreeNode *t)////后序遍历二叉树
{
    if(t!=NULL)
    {
        hshow(t->left);
        hshow(t->right);    
                printf("%d\t",t->value);    
    }
}
int main()
{
    BitreeNode *t;
    creat(t);
    xshow(t);
    zshow(t);
    hshow(t);
    return 0;
}        

 

二叉树