#include <iostream>
#include <cstring>
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
typedef BTree* pT;
struct BTree {
char data;
pT lchild, rchild;
};
void PreOrder(pT T) {
pT stk[105], node;
int top = 0;
if (T == NULL) {
cout << "空树";
return;
}
else {
top++;
stk[top] = T;
while (top > 0) {
node = stk[top--];
cout << node->data;
if (node->rchild)stk[++top] = node->rchild;
else if (node->lchild)stk[++top] = node->lchild;
}
}
}
void InOrder(pT T) {
pT stk[105], node;
int top = 0;
if (!T) {
cout << "空树";
return;
}
node = T;
while (node != NULL || top > 0) {
while (node != NULL) {
stk[++top] = node;
node = node->lchild;
}
node = stk[top--];
cout << node->data<<" ";
node = node->rchild;
}
}
void PostOrder(pT T) {
pT p = T, r = NULL;
pT stk[105];
int top = 0;
while (p || top > 0) {
while (p) {
stk[top++] = p;
p = p->lchild;
}
p = stk[top - 1];
if (p->rchild == NULL || p->rchild == r) {
cout << p->data;
top--;
r = p;
p = NULL;
}
else p = p->rchild;
}
}
int main()
{
return 0;
}
树的非递归遍历
最新推荐文章于 2024-07-23 12:24:57 发布