改进的二叉树算法——接受输入
2004-11-13 00:29
281 查看
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define ElementType int
//node structure constructor
typedef struct bt {
ElementType data;
struct bt *lchild, *rchild;
} BinaryTreeNode,*BTRoot;
//function declear
InOrder(BTRoot root);
PreOrder(BTRoot root);
PostOrder(BTRoot root);
//the main function to excute
main()
{
BTRoot r=malloc(sizeof(BinaryTreeNode));
CreateTree(r,1);
printf("/nPreorder :");
PreOrder(r);
printf("/nInorder :");
InOrder(r);
printf("/nPostorder:");
PostOrder(r);
while(1) ;
return 0;
}
//inorder function
InOrder(BTRoot root)
{
if(!root) return 0;
InOrder(root->lchild);
printf("%4d",root->data);
InOrder(root->rchild);
}
//preorder function
PreOrder(BTRoot root)
{
if(!root) return 0;
printf("%4d",root->data);
PreOrder(root->lchild);
PreOrder(root->rchild);
}
//postorder function
PostOrder(BTRoot root)
{
if(!root) return 0;
PostOrder(root->lchild);
PostOrder(root->rchild);
printf("%4d",root->data);
}
/*receive the input data and create a node each time!*/
CreateTree(BTRoot root,int NodeNum) /* Preorder */
{
ElementType x;
int i,layer;
layer=log(NodeNum)/log(2)+1;
printf("/nInput data for Node %d in layer %d :",NodeNum,layer);
scanf("%d",&x);
root->data=x;
printf("create lchild for Node %d in layer %d (0 for No)?",NodeNum,layer);
scanf("%d",&i);
if(i==0) root->lchild=NULL;
else
{root->lchild=(BTRoot)malloc(sizeof(BinaryTreeNode));
CreateTree(root->lchild,2*NodeNum);
}
printf("create rchild for Node %d in layer %d (0 for No)?",NodeNum,layer);
scanf("%d",&i);
if(i==0) root->rchild=NULL;
else
{root->rchild=(BTRoot)malloc(sizeof(BinaryTreeNode));
CreateTree(root->rchild,2*NodeNum+1);
}
return 0;
}
#include <stdlib.h>
#include <math.h>
#define ElementType int
//node structure constructor
typedef struct bt {
ElementType data;
struct bt *lchild, *rchild;
} BinaryTreeNode,*BTRoot;
//function declear
InOrder(BTRoot root);
PreOrder(BTRoot root);
PostOrder(BTRoot root);
//the main function to excute
main()
{
BTRoot r=malloc(sizeof(BinaryTreeNode));
CreateTree(r,1);
printf("/nPreorder :");
PreOrder(r);
printf("/nInorder :");
InOrder(r);
printf("/nPostorder:");
PostOrder(r);
while(1) ;
return 0;
}
//inorder function
InOrder(BTRoot root)
{
if(!root) return 0;
InOrder(root->lchild);
printf("%4d",root->data);
InOrder(root->rchild);
}
//preorder function
PreOrder(BTRoot root)
{
if(!root) return 0;
printf("%4d",root->data);
PreOrder(root->lchild);
PreOrder(root->rchild);
}
//postorder function
PostOrder(BTRoot root)
{
if(!root) return 0;
PostOrder(root->lchild);
PostOrder(root->rchild);
printf("%4d",root->data);
}
/*receive the input data and create a node each time!*/
CreateTree(BTRoot root,int NodeNum) /* Preorder */
{
ElementType x;
int i,layer;
layer=log(NodeNum)/log(2)+1;
printf("/nInput data for Node %d in layer %d :",NodeNum,layer);
scanf("%d",&x);
root->data=x;
printf("create lchild for Node %d in layer %d (0 for No)?",NodeNum,layer);
scanf("%d",&i);
if(i==0) root->lchild=NULL;
else
{root->lchild=(BTRoot)malloc(sizeof(BinaryTreeNode));
CreateTree(root->lchild,2*NodeNum);
}
printf("create rchild for Node %d in layer %d (0 for No)?",NodeNum,layer);
scanf("%d",&i);
if(i==0) root->rchild=NULL;
else
{root->rchild=(BTRoot)malloc(sizeof(BinaryTreeNode));
CreateTree(root->rchild,2*NodeNum+1);
}
return 0;
}
相关文章推荐
- 将15位身份证补全为18位身份证的算法示例详解
- C++算法系列之日历生成的算法代码
- VC与Delphi之间动态链接库互相调用
- javascript表单之间的数据传递!
- 英雄无敌程序员版
- 统一权限系统URight1.5
- delphi中的时间操作技术(1)
- delphi中的时间操作技术(2)
- VB五子棋问题,请高手赐教
- 计算器原代码的问题
- IP地址的隐藏(delphi实现 )
- 解析IP地址为主机域名
- 将某一主机域名解析为IP地址
- 关于目前的方向
- 引用一个人的说法看中国软件需求基础
- 有关TrueDBGrid的问题向各位大虾请教
- Ant应用(1)
- 非对称加密算法中求解大正整数模大正整数的余数的快速计算法
- 用VB和MTS开发多层数据库应用系统
- 使用C#开发COM+组件