标签:
今天初步了解了二叉树的定义和创建及遍历方法,总结一下
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
//二叉树结点的定义
typedef struct BTNode
{
char data;
struct BTNode *lc;
struct BTNode *rc;
}BTNode,*BiTree;
void Creat(BiTree &T)
{
char ch;
scanf("%c",&ch);
if(ch==‘#‘)
T=NULL;
else{
T=new BTNode;
T->data=ch;
Creat(T->lc);
Creat(T->rc);
}
}
//先序遍历
void PreOrder(BiTree T)
{
if(T)
{
cout<<T->data<<‘ ‘;
PreOrder(T->lc);
PreOrder(T->rc);
}
}
//中序遍历
void InOrder(BiTree T)
{
if(T)
{
InOrder(T->lc);
cout<<T->data<<‘ ‘;
InOrder(T->rc);
}
}
//后序遍历
void PostOrder(BiTree T)
{
if(T)
{
PostOrder(T->lc);
PostOrder(T->rc);
cout<<T->data<<‘ ‘;
}
}
int main()
{
BiTree T;
printf("Input nodes of binary tree:(NULL==‘#‘)\n");
Creat(T);
cout<<"\nPreOrder traverse:\n";
PreOrder(T);
cout<<"\nInOrder traverse:\n";
InOrder(T);
cout<<"\nPostOrder traverse:\n";
PostOrder(T);
return 0;
}
标签:
原文地址:http://www.cnblogs.com/yfz1552800131/p/5413590.html