#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);
}
}
二叉树的遍历:前序、中序、后序
最新推荐文章于 2024-10-23 16:28:20 发布