#include <iostream>
#include <vector>
#include <stack>
using namespace std;

struct BtreeNode{

	BtreeNode(int value, struct BtreeNode *parent, struct BtreeNode *left, struct BtreeNode *right){
		this->value = value;
		this->parent = parent;
		this->left = left;
		this->right = right;

	}
	int value;
	struct BtreeNode *parent;
	struct BtreeNode *left;
	struct BtreeNode *right;
};


void insertNode(struct BtreeNode **root, int value){

	if(NULL == *root){

		struct BtreeNode *tmp = new struct BtreeNode(value, NULL, NULL, NULL);
		tmp->value = value;
		*root = tmp;

	}else{
		struct BtreeNode *tmp = *root;

		while(tmp){

			if(tmp->value < value){

				if(tmp->right == NULL){
				
					struct BtreeNode *tmp1 = new struct BtreeNode(value, NULL, NULL, NULL);
					tmp1->value = value;
					tmp1->parent = tmp;
					tmp->right = tmp1;
					return ;
				}
				tmp = tmp->right;
			
			}else if(tmp->value > value){

				if(NULL == tmp->left){

					struct BtreeNode *tmp1 = new struct BtreeNode(value, NULL, NULL, NULL);
					tmp1->value = value;
					tmp1->parent = tmp;
					tmp->left = tmp1;
					return ;
				}
				tmp = tmp->left;
			}else{
				return ;			
			}
		}
		
	}
}


void preTraverse(struct BtreeNode *root){
	
	if(root == NULL)
		return ;
	
	cout<<root->value<<" ";
	preTraverse(root->left);
	preTraverse(root->right);
	
}

void preTraverseWithoutRecursion(struct BtreeNode *root){

	if(NULL == root){
		return;
	}
	
	stack<struct BtreeNode *> record;
	cout<<root->value<<" ";
	
	if(root->right != NULL)
		record.push(root->right);
	if(root->left != NULL)
		record.push(root->left);
	
	while(!record.empty()){
	
		struct BtreeNode * tmp = record.top();
		record.pop();
		cout<<tmp->value<<" ";
		
		//先右后左
		if(tmp->right != NULL)
			record.push(tmp->right);
		if(tmp->left != NULL)
			record.push(tmp->left);
	}
}

void inTraverse(struct BtreeNode *root){
	
	if(root == NULL)
		return ;
		
	inTraverse(root->left);
	cout<<root->value<<" ";
	inTraverse(root->right);
	
}


void inTraverseWithoutRecursion(struct BtreeNode *root){

	if(NULL == root){
		return;
	}
	
	stack<struct BtreeNode *> record;
	
	//一直压到最左边的子节点
	struct BtreeNode *tmp = root;
	while(tmp != NULL){
		record.push(tmp);
		tmp = tmp->left;
	}
	
	while(!record.empty()){
		
		tmp = record.top();
		record.pop();
		cout<<tmp->value<<" ";
		
		tmp = tmp->right;
		while(tmp != NULL){
			record.push(tmp);
			tmp = tmp->left;
		}
	}
	
}

void AfterTraverse(struct BtreeNode *root){
	
	if(root == NULL)
		return ;
		
	AfterTraverse(root->left);
	AfterTraverse(root->right);
	cout<<root->value<<" ";
}


void AfterTraverseWithoutRecursion(struct BtreeNode *root){

	if(NULL == root){
		return;
	}
	
	stack<struct BtreeNode *> record;
	
	struct BtreeNode *pre = NULL;
	
	
	struct BtreeNode *tmp = root;
	while(tmp != NULL){
		record.push(tmp);
		tmp = tmp->left;
	}
	
	while(!record.empty()){
	
		tmp = record.top();
		
		//右子树为空,可以直接输出
		if(tmp->right == NULL){
			cout<<tmp->value<<" ";
			pre = tmp;
			record.pop();
		}else{
			//右子树已经全部访问了,可以直接输出
			if(tmp->right == pre){
				cout<<tmp->value<<" ";
				pre = tmp;
				record.pop();
			}else{
			
				tmp = tmp->right;
				while(tmp != NULL){
					record.push(tmp);
					tmp = tmp->left;
				}
			}
		}
	}
}


int main(){

	struct BtreeNode *root = NULL;
	
	insertNode(&root, 6);
	insertNode(&root, 9);
	insertNode(&root, 3);
	//insertNode(&root, 2);
	insertNode(&root, 5);
	insertNode(&root, 7);
	//insertNode(&root, 12);
	
	preTraverse(root);
	cout<<endl;
	
	preTraverseWithoutRecursion(root);
	cout<<endl;
	
	inTraverse(root);
	cout<<endl;
	
	inTraverseWithoutRecursion(root);
	cout<<endl;
	
	AfterTraverse(root);
	cout<<endl;
	
	AfterTraverseWithoutRecursion(root);
	cout<<endl;
}