标签:style io ar os sp strong on 数据 art
//数据结构中指针的应用}
//C世界的树
//这里写的是二叉树的三种遍历方式
#include <stdio.h>
#include <malloc.h>
typedef struct tree_node
{
char data;
struct tree_node *lchild, *rchild;
}BT_Node;
#define Tree_NodeLen sizeof(BT_Node)
BT_Node *tree;
BT_Node *Creat_BTree(BT_Node *t);
void visit_Node(BT_Node *tree);
void Pre_Order(BT_Node *tree);
void Mid_Order(BT_Node *tree);
void After_Order(BT_Node *tree);
int main()
{
printf("\n请输入输的节点\n");
tree = Creat_BTree(Tree);
if(tree)
{
printf("\n前序遍历\n");
Pre_Order(tree);
printf("\n");
printf("\n中序遍历\n");
Mid_Oder(tree);
printf("\n");
printf("\n中序遍历\n");
After_Order(tree);
printf("\n");
}
printf("\n");
return 0;
}
BT_Node *Create_BTree(BT_Node *tree)
{
char ch;
ch = getchar();
if(ch == ‘*‘)
{
tree = null;
}
else
{
tree = (BT_Node *)malloc(Tree_NodeLen);
tree->data = ch;
tree->lchild = Create_BTree(tree->lchild);
tree->rchild = Create_BTree(tree->rchild);
}
return(tree);
}
void Visited_Node(BT_Node *tree)
{
printf(" ");
putchar(tree->data);
printf("\t");
}
void Pre_Order(BT_Node *tree)
{
if(! tree)
{
return;
}
else
{
Visit_Node(tree);
Pre_Order(tree->lchild);
Pre_Order(tree->rchild);
}
}
void Mid_Order(BT_Node *tree)
{
if(!tree)
{
return;
}
else
{
Mid_Order(tree->lchild);
Visit_Node(tree);
Mid_Order(tree->rchild);
}
}
void After_Order(BT_Node *tree)
{
if(! tree)
{
return;
}
else
{
After_Order(tree->lchild);
After_Order(tree->rchild);
Visited_Node(tree);
}
}
标签:style io ar os sp strong on 数据 art
原文地址:http://blog.csdn.net/u012965373/article/details/41812637