-
条件:二叉树 -
题目:无 -
原理:无 -
代码:
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <map>
#include <queue>
#include <deque>
#include <string>
#include <cstring>
#include <stack>
#include <cmath>
#include <fstream>
#include <ctime>
#include <climits>
using namespace std;
class Solver {
public:
Solver() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
}
public:
void SaveCpp(string name) const {
fstream input;
fstream output;
input.open("moota.cpp", ios::in);
const string file = name + ".cpp";
output.open(file.c_str(), ios::out);
char c = 'O';
while (!input.eof()) {
input.get(c);
output << c;
}
input.close();
output.close();
}
protected:
virtual void BeginPlay() {
};
virtual void Playing() {
};
virtual void EndPlay() {
};
public:
void Play() {
BeginPlay();
Playing();
EndPlay();
}
};
class SpecialSolver : public Solver {
public:
typedef long long lld;
static const lld MAX = static_cast<lld>(1e4);
static const lld INF = static_cast<lld>(1e18);
private:
struct Node {
char value;
Node* l;
Node* r;
} * head;
void CreateBinaryTree(Node* & node) {
char c;
cin >> c;
if (c != '#') {
node = new Node;
node->value = c;
CreateBinaryTree(node->l);
CreateBinaryTree(node->r);
}
else {
node = nullptr;
}
}
void PreOrderG(Node* head) {
if (head != nullptr) {
cout << head->value << " ";
PreOrderG(head->l);
PreOrderG(head->r);
}
}
void InOrderG(Node* head) {
if (head != nullptr) {
InOrderG(head->l);
cout << head->value << " ";
InOrderG(head->r);
}
}
void PostOrderG(Node* head) {
if (head != nullptr) {
PostOrderG(head->l);
PostOrderG(head->r);
cout << head->value << " ";
}
}
void PreOrderD(Node* node) {
stack<Node*> s;
while (!s.empty() || node != nullptr) {
if (node != nullptr) {
cout << node->value << " ";
s.push(node);
node = node->l;
}
else {
node = s.top();
s.pop();
node = node->r;
}
}
}
void InOrderD(Node* node) {
stack<Node*> s;
while (!s.empty() || node != nullptr) {
if (node != nullptr) {
s.push(node);
node = node->l;
}
else {
node = s.top();
s.pop();
cout << node->value << " ";
node = node->r;
}
}
}
void PostOrderD(Node* node) {
struct NodeWithTag {
Node* node;
bool tag;
};
stack<NodeWithTag> s;
s.push(NodeWithTag{node, false});
while (!s.empty()) {
NodeWithTag nodeWithTag = s.top();
s.pop();
if (nodeWithTag.tag == false) {
if (nodeWithTag.node != nullptr) {
s.push(NodeWithTag{nodeWithTag.node, true});
s.push(NodeWithTag{nodeWithTag.node->r, false});
s.push(NodeWithTag{nodeWithTag.node->l, false});
}
}
else {
cout << nodeWithTag.node->value << " ";
}
}
}
void CellOrder(Node* node) {
queue<Node*> q;
q.push(node);
while (!q.empty()) {
Node* node = q.front();
q.pop();
cout << node->value << " ";
if (node->l != nullptr)q.push(node->l);
if (node->r != nullptr)q.push(node->r);
}
}
private:
protected:
virtual void BeginPlay() override {
Solver::BeginPlay();
CreateBinaryTree(head);
};
virtual void Playing() override {
Solver::Playing();
cout << "\n先序递归:";
PreOrderG(head);
cout << "\n先序迭代:";
PreOrderD(head);
cout << "\n中序递归:";
InOrderG(head);
cout << "\n中序迭代:";
InOrderD(head);
cout << "\n后序递归:";
PostOrderG(head);
cout << "\n后序迭代:";
PostOrderD(head);
cout << "\n层次遍历:";
CellOrder(head);
};
virtual void EndPlay() override {
Solver::EndPlay();
};
};
SpecialSolver specialSolver;
int main() {
specialSolver.Play();
}
|