二叉树的遍历:前序、中序、后序

#include<stdio.h>
#include<stdlib.h>
typedef struct Bintree
{
int data;
struct Bintree *lchild;
struct Bintree *rchild;
}treenode,*b_tree;
b_tree root=NULL;
/* 函数的声明*/
b_tree creat_tree(int n);
void print1();
void preorder(b_tree pointer);//前序遍历
void print2();
void inorder(b_tree pointer);//中序遍历
void print3();
void postorder(b_tree pointer);//后序遍历
/*主函数*/
int main(){
int n;
printf("please input nums of Bintrees'node:\n");
scanf("%d",&n);
/*creat a Bintree*/
root=creat_tree(n);
/*print 3 kinds of order*/
print1();
print2();
print3();
/*order a bintree*/
//preorder(root);//前序遍历
//inorder(root);//中序遍历
//postorder(root);//后序遍历
system("pause");
return 0;
}
/*函数的定义*/
b_tree creat_tree(int n)
{
int i;
b_tree currentnode,parentnode,newnode;
for(i=0;i<n;i++)
 {
  newnode=malloc(sizeof(treenode));
  if(!newnode) return;
  printf("please intial num %d node:\n",i+1);
  scanf("%d",&(newnode->data));
  newnode->lchild=NULL;
  newnode->rchild=NULL;
 if(i==0)
   root=newnode;
 else
  {
   currentnode=root;
   while(currentnode!=NULL)//当前节点进行遍历
    {
     parentnode=currentnode;//双亲节点即使最后的根节点用于和新节点连接
     if(currentnode->data>newnode->data)
       currentnode=currentnode->lchild;
     else
       currentnode=currentnode->rchild;
    }
   if(parentnode->data>newnode->data)
      parentnode->lchild=newnode;
   else
      parentnode->rchild=newnode;
  }
 }
return root;
}
void print1()
{
printf("前序遍历:\n");
preorder(root);
printf("\n");
}
void print2()
{
printf("中序遍历:\n");
inorder(root);
printf("\n");
}
void print3()
{
printf("后序遍历:\n");
postorder(root);
printf("\n");
}
void preorder(b_tree pointer)//前序遍历
{
if(pointer!=NULL)//递归的条件
 {
  printf("[%d]=>",pointer->data);
  preorder(pointer->lchild);
  preorder(pointer->rchild);
 }
}
void inorder(b_tree pointer)//中序遍历
{
if(pointer!=NULL)
 {
  inorder(pointer->lchild);
  printf("[%d]=>",pointer->data);
  inorder(pointer->rchild);
 }
}
void postorder(b_tree pointer)//后序遍历
{
if(pointer!=NULL)
 {
  postorder(pointer->lchild);
  postorder(pointer->rchild);
  printf("[%d]=>",pointer->data);
 }
}


//另外一种程序

#include<stdio.h>
#include<stdlib.h>
typedef struct Bintree
{
int data;
struct Bintree *lchild;
struct Bintree *rchild;
}treenode,*b_tree;
b_tree root=NULL;
/* 函数的声明*/
void creat_tree(int n);
void print1();
void preorder(b_tree pointer);//前序遍历
void print2();
void inorder(b_tree pointer);//中序遍历
void print3();
void postorder(b_tree pointer);//后序遍历
/*主函数*/
int main(){
int n;
printf("please input nums of Bintrees'node:\n");
scanf("%d",&n);
/*creat a Bintree*/
creat_tree(n);
/*print 3 kinds of order*/
print1();
print2();
print3();
/*order a bintree*/
//preorder(root);//前序遍历
//inorder(root);//中序遍历
//postorder(root);//后序遍历
system("pause");
return 0;
}
/*函数的定义*/
void creat_tree(int n)
{
int i;
b_tree currentnode,parentnode,newnode;
for(i=0;i<n;i++)
 {
  newnode=malloc(sizeof(treenode));
  if(!newnode) return;
  printf("please intial num %d node:\n",i+1);
  scanf("%d",&(newnode->data));
  newnode->lchild=NULL;
  newnode->rchild=NULL;
 if(i==0)
   root=newnode;
 else
  {
   currentnode=root;
   while(currentnode!=NULL)//当前节点进行遍历
    {
     parentnode=currentnode;//双亲节点即使最后的根节点用于和新节点连接
     if(currentnode->data>newnode->data)
       currentnode=currentnode->lchild;
     else
       currentnode=currentnode->rchild;
    }
   if(parentnode->data>newnode->data)
      parentnode->lchild=newnode;
   else
      parentnode->rchild=newnode;
  }
 }
}
void print1()
{
printf("前序遍历:\n");
preorder(root);
printf("\n");
}
void print2()
{
printf("中序遍历:\n");
inorder(root);
printf("\n");
}
void print3()
{
printf("后序遍历:\n");
postorder(root);
printf("\n");
}
void preorder(b_tree pointer)//前序遍历
{
if(pointer!=NULL)//递归的条件
 {
  printf("[%d]=>",pointer->data);
  preorder(pointer->lchild);
  preorder(pointer->rchild);
 }
}
void inorder(b_tree pointer)//中序遍历
{
if(pointer!=NULL)
 {
  inorder(pointer->lchild);
  printf("[%d]=>",pointer->data);
  inorder(pointer->rchild);
 }
}
void postorder(b_tree pointer)//后序遍历
{
if(pointer!=NULL)
 {
  postorder(pointer->lchild);
  postorder(pointer->rchild);
  printf("[%d]=>",pointer->data);
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值