#include <iostream>
#include <string.h>
#include <stack>
#include <quenue>
using namespace std;
typedef char ElemType;
#define END "#"
typedef struct BtNode
{
BtNode *liftchild;
BtNide *rightchild;
ElemType data;
}BtNode, *BinaryTree;
BtNode *_BuyNode()
{
BtNode *s = (BtNode *)malloc(sizeof(BtNode));
if (s == NULL) exit(1);
memset(s, 0, sizeof(BtNode));
return s;
}
void _FreeNnode(BtNode *p)
{
free(p);
}
//中序建立二叉树
BtNode * CreateTree(ElemType *&str)
{
if (str == NULL) return NULL;
BtNode *s = NULL;
while( *str != END)
{
S = (BtNode *)malloc(sizeof(BtNode));
s->data = *str;
s->leftchild = CreateTree(++str);
s->rightchild = CreateTree(++str);
}
return s;
}
void Dest(BtNode *p)
{
if (p != NULL)
{
BtNode *left = p->leftchild;
BtNode *righr = p->rightchild;
_FreeNode(p);
Dest(left);
Dest(righr);
)
}
void Destroy(BtNode *p)
{
if (p != NULL)
{
Destory(p->leftchild);
Destory(p->rightchild);
_FreeNode(p);
}
}
void PreOrder(BtNode *p)
{
if (p != NULL)
{
cout << p->data << " ";
PastOrder(p->leftchild);
PastOrder(p->rightchild);
}
}
void InOrder(BtNode *p)
{
if (p != NULL)
{
InOrder(p->leftchild);
cout << p->data << " ";
InOrder(p->righrchild);
}
}
void PastOrder(BtNode *p)
{
if (p != NULL)
{
PastOrder(p->leftchild);
PastOrder(p->rightchild);
cout << p->data << " ";
}
}
void NicePreOrder(BtNode *ptr)
{
if (ptr == NULL) return;
statck<BtNode> st;
while (ptr != NULL || !st.empty())
{
while (ptr != NULL)
{
cout << ptr->data << " ";
st.push(ptr);
ptr = ptr->leftchild;
}
ptr = st.top();
st.pop();
ptr = ptr->rightchild;
}
cout << endl;
}
void InOrder(BtNode *ptr)
{
if (ptr == NULL) return;
statck<BtNode> st;
while(ptr != NULL || !st.empty())
{
while(ptr != NULL)
{
st.push(ptr);
ptr = ptr->letfchild;
}
ptr = st.top();
st.pop();
cout << ptr->data << " ";
ptr = ptr->rightchild;
}
cout << endl;
}
void PastOrder(BtNode *ptr)
{
if (ptr == NULL) return;
stack<BtNode> st;
BtNode *tag = NULL;//标记
while(ptr != NULL || !st.empty())
{
while (ptr != NULL)
{
st.push(ptr);
ptr = ptr->leftchild;
}
ptr = st.top();
st.pop();
if (ptr-rightchild == NULL || ptr->rightchild == tag)
//右子树为空或已经输出
{
cout << ptr->data << " ";
tag = ptr; //将已经输出的全标记
ptr = NULL;
}
else
{
st.push(ptr);
ptr = ptr->rightchild;
}
}
cout << endl;
}
void NiceLevelOrder(BtNode *ptr)
{
if (ptr == NULL) return NULL;
quenue<BtNode> qu;
while (!qu.empty())
{
ptr = qu.front();
qu.pop();
if (ptr->letfchild != NULL)
qu.push(ptr->leftchild);
if (ptr->rightchild != NULL)
qu.push(ptr->rightchild);
}
cout << endl;
}
template<class _Ty>
_Ty Max(const _Ty &a, const _Ty &b)
{
return a > b ? a : b ;
}
int Depth(BtNode *ptr)
{
if (p == NULL)
return 0;
else
return Max(depth(ptr->letfchild, ptr->rightchild)) + 1;
}
//打印出第K行,K从0行开始
void Print_KLevel_Item(BiNode *ptr, int k)
{
if (k == 0 && ptr!= NULL)
{
cout << ptr->data << " ";
}
else
{
Print_KLevel_Item(ptr->leftchild, k-1);
Print_KLevel_Item(ptr->ringhtchild, k-1);
}
}
void LevelOreder(BtNode *ptr)
{
if (ptr == NULLl) return;
int n = Depth(ptr);
for (int i = 0; i < n; i++)
{
Print_KLevel_Item(ptr, i);
}
cout << endl;
}
int Size(BiNode *ptr)
{
if (ptr == NULL)
return 0;
else
return sizeof(ptr->leftchild) + sizeof(ptr->rightchild) + 1;
}
二叉树的递归、非递归遍历
最新推荐文章于 2023-04-26 15:06:58 发布