C / C ++ binary tree recursive and non-recursive traversal

Pre-order traversal non-recursive: encountered a node, you access the node and the node pushed onto the stack, and then drop to traverse its left subtree.
After traversing its left subtree of this node from the top of the stack holder, and according to the address indicated by linking it to go right
traverse right subtree of the node structure.

Non-recursive traversal sequence: encountered a node, put it pushed onto the stack, and to traverse its left subtree. After traversing the left subtree, from the top of the stack
to prop up the node and access. Then follow the links to the right address its instructions to go to traverse the right subtree of the node.

After the non-recursive traversal sequence: encountered a node, push it onto the stack, traverse its left subtree. After traversing the end, not immediately accessible
in the node top of the stack, but again it's the right link to the address indicated in the structure to traverse the right subtree of the node,
After traversing the right subtree to prop up the top of the stack and access node. Further, it is necessary for each element of the stack plus a
flag, so that when a node from the stack holder is returned from the difference between the left top of the stack (have to continue to traverse the right subtree),
or from right back (about that node have been traveling around)
feature that has entered the node's left subtree is left, from the back to the left.
Characterized by right represents the right subtree has entered the node, it will come back to the right.

#include <iostream>
#include <string>
#include <stack>
#include <vector>
using namespace std;
struct Node
{
	int val;
	Node* left;
	Node* right;
	Node(int x):val(x),left(nullptr),right(nullptr){};
};

void createBiTree(Node* &root)
{
	int x;
	cin>>x;
	if (x==-1)
	{
		root = nullptr;
		return;
	}
	root = new Node(x);
	createBiTree(root->left);
	createBiTree(root->right);
}

void visit(Node* T)
{
	if (T->val!=-1) 
		cout<<T<<val<<"  "; 
}

//递归方式遍历
//先序递归遍历
void preOrder(Node* root)
{
	if(root!=nullptr)
	{
		visit(root);
		preOrder(root->left);
		preOrder(root->right);
	}
}

//中序递归遍历
void inOrder(Node* root)
{
	if(root!=nullptr)
	{
		inOrder(root->left);
		visit(root);
		inOrder(root->right);
	}
}

//后序递归遍历
void postOrder(Node* root)
{
	if(root!=nullptr)
	{
		postOrder(root->left);
		postOrder(root->right);
		visit(root);
	}
}

//非递归方式遍历
//先序遍历
void preOrderF(Node* root)
{
	if (root==nullptr)
		return;
	stack<Node*> s;
	s.push(root);
	Node* p = nullptr;
	while(!s.empty())
	{
		p.s.top();
		s.pop();
		cout<<p->val<< "  ";
		if (p->right)
			s.push(p->right);
		if(p->left)
			s.push(p->left);
	}
}

//中序遍历
void inOredrF(Node* root)
{
	if(root == nullptr)
		return;
	stack<Node*> s;
	Node* p = root;
	while(p||!s.empty())
	{
		if(p)
		{
			s.push(p);
			p = p->left;
		}
		else
		{
			p = s.top();
			s.pop();
			cout<<p->val<<"  ";
			p = p->right;
		}
	}
}

//后序遍历
void postOrderF(Node* root)
{
	if(root == nullptr)
		return;
	stack<Node*> s;
	std::vector<int> rs;
	s.push(root);
	Node* p = nullptr;
	while(!s.empty())
	{
		p = s.top();
		s.pop();
		rs.insert(rs.begin(),p->val);
		if(p->left)
			s.push(p->left);
		if(p->right)
			s.push(p->right);
	}
	for(int i = 0;i<rs.size();i++)
		cout<<rs[i]<< "  ";
}

int main()
{
	Node* root;
	createBiTree(root);
	preOrderF(root);
	cout<<endl;
	inOredrF(root);
	cout<<endl;
	postOrderF(root);
	cout<<endl;

	system("pause");
	return 0;
}
Published 43 original articles · won praise 1 · views 2318

Guess you like

Origin blog.csdn.net/lpl312905509/article/details/102624998