#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
遍历二叉树算法
typedef char ElemType;
typedef struct BtNode //定义二叉树的结点
{
BtNode *leftchild;
BtNode *rightchild;
ElemType data;
}BtNode, *BinaryTree;
//先序遍历:根左右
void PreOrder(BtNode *p)
{
if(p != NULL)
{
cout<<p->data<<" ";
PreOrder(p->leftchild);
PreOrder(p->rightchild);
}
}
//中序遍历:左根右
void InOrder(BtNode *p)
{
if(p != NULL)
{
InOrder(p->leftchild);
cout<<p->data<<" ";
InOrder(p->rightchild);
}
}
//后序遍历:左右根
void PastOrder(BtNode *p)
{
if(p != NULL)
{
PastOrder(p->leftchild);
PastOrder(p->rightchild);
cout<<p->data<<" ";
}
}
//购买结点
BtNode *BuyNode()
{
BtNode *s = (BtNode*)malloc(sizeof(BtNode));
if(s == NULL) exit(1);
memset(s, 0, sizeof(BtNode)); //初始化内存空间
return s;
}
//释放节
分别用先序、中序、后序遍历二叉树(递归实现)
最新推荐文章于 2022-10-23 18:53:43 发布