}
//C世界的樹
//這裡寫的是二叉樹的三種遍歷方式
#include
#include
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);
}
}