//
// Created by Administrator on 2021/8/1.
//

#ifndef C__TEST01_NODE_HPP
#define C__TEST01_NODE_HPP
#include <stack>
#include <queue>

class Node{
public:
int value;
Node* left;
Node* right;
Node(int value):value(value){
left = NULL;
right = NULL;
}
};
//随便建一个二叉树
Node* treeInit(){
Node *head = new Node(1);
head->left = new Node(2);
head->right = new Node(3);
head->left->left = new Node(4);
head->left->right = new Node(5);
head->right->left = new Node(6);
head->right->right = new Node(7);
return head;
}

//前序遍历
void preOrderByRecur(Node *head){
if(head == NULL){
return;
}
cout<<head->value<<" ";
preOrderByRecur(head->left);
preOrderByRecur(head->right);
}

void preOrderNoRecur(Node *head){
if(head == NULL){
return;
}
stack<Node*> s;
s.push(head);
while(!s.empty()){
head = s.top();
cout<<head->value<<" ";
s.pop();
if(head->right != NULL){
s.push(head->right);
}
if(head->left != NULL){
s.push(head->left);
}
}
}

//中序遍历
void inOrderByRecur(Node* head){
if(head == NULL){
return;
}
inOrderByRecur(head->left);
cout<<head->value<<" ";
inOrderByRecur(head->right);
}


void inOrderByNoRecur(Node *head){
if(head == NULL){
return;
}
stack<Node *> s1;
while(!s1.empty() || head != NULL){
if(head!=NULL){
s1.push(head);
head = head->left;
}else{
head = s1.top();
cout<<head->value<<" ";
s1.pop();
head = head->right;
}
}
}
//后序遍历
void proOrderByRecur(Node *head){
if(head == NULL){
return;
}
proOrderByRecur(head->left);
proOrderByRecur(head->right);
cout<<head->value<<" ";
}

void proOrderByNoRecur(Node* head){
if(head == NULL){
return;
}
stack<Node*> s1;
stack<Node*> s2;
s1.push(head);
while(!s1.empty()){
head = s1.top();
s1.pop();
s2.push(head);
if(head->left!=NULL){
s1.push(head->left);
}
if(head->right!=NULL){
s1.push(head->right);
}
}
while(!s2.empty()){
cout<<s2.top()->value<<" ";
s2.pop();
}
}
//层次遍历
void boardOrder(Node *head){
if(head == NULL){
return;
}
queue<Node*> q;
q.push(head);
while(!q.empty()){
head = q.front();
cout<<head->value<<" ";
q.pop();
if(head->left != NULL){
q.push(head->left);
}
if(head->right != NULL){
q.push(head->right);
}
}
}
#endif //C__TEST01_NODE_HPP

主要是给自己看的,所以肯定会出现很多错误哈哈哈哈哈