树的先序、中序、后序和层次遍历-C++实现----面试题目-----同时也可以说是树的层次历遍

一、先序遍历
1、递归算法

				struct Tree
				{
					int date;
					Tree* lchild;
					Tree* rchild;
					Tree(int x):date(x),lchild(nullptr),rchild(nullptr){}
				};
				
				void PreOrder(Tree* root)
				{
					if (root != nullptr)
					{
						cout << root->date << " ";
						PreOrder(root->lchild);
						PreOrder(root->rchild);
					}
				}

2、非递归算法

					void PreOrder(Tree* root)
					{
						stack<Tree* > Stack;
						if (root == nullptr)
							return;
						while (root != nullptr || !Stack.empty())
						{
							while (root != nullptr)
							{
								Stack.push(root);
								cout << root->date << " ";
								root = root->lchild;
							}
							root = Stack.top();
							Stack.pop();
							root = root->rchild;
						}
					}

二、中序遍历
1、递归算法

					void InOrder(Tree* root)
					{
						if (root != nullptr)
						{
							InOrder(root->lchild);
							cout << root->date << " ";
							InOrder(root->rchild);
						}
					}

2、非递归算法

						void InOrder(Tree* root)
						{
							stack<Tree*> s;
							while (root != nullptr || !s.empty())
							{
								if (root != nullptr)
								{
									s.push(root);
									root = root->lchild;
								}
								else
								{
									root = s.top();s.pop();
									cout << root->date << " ";
									root = root->rchild;
								}
							}
						}

三、后序遍历
1、递归算法

					void PostOrder(Tree* root)
					{
						if (root != nullptr)
						{
							PostOrder(root->lchild);
							PostOrder(root->rchild);
							cout << root->date << " ";
						}
					}

2、非递归算法

						void PostOrder(Tree* root)
						{
							stack<Tree*> s;
							Tree* r = nullptr;//使用辅助指针,指向最近访问过的节点
							while (root != nullptr || !s.empty())
							{
								if (root != nullptr)
								{
									s.push(root);
									root = root->lchild;
								}
								else
								{
									root = s.top();
									if (root->rchild != nullptr && root->rchild != r)
										root = root->rchild;
									else
									{
										s.pop();
										cout << root->date << " ";
										r = root;
										root = nullptr;
									}
								}
							}
						}

四、层次遍历

1、方法1

					void LevelOrder(Tree* root)
					{
						if (root == nullptr)
							return;
						queue<Tree*> que;
						que.push(root);
						while (!que.empty())
						{
							root = que.front();
							cout << root->date << " ";
							que.pop();
							if (root->lchild != nullptr)
								que.push(root->lchild);
							if (root->rchild != nullptr)
								que.push(root->rchild);
						}
					}

2、方法2

						void LevelOrder(Tree* root)
						{
							if (root == nullptr)
								return;
							queue<Tree*> que;
							que.push(root);
							while (!que.empty())
							{
								int size = que.size();
								while (size)
								{
									root = que.front();
									cout << root->date << " ";
									que.pop();
									if (root->lchild != nullptr)
										que.push(root->lchild);
									if (root->rchild != nullptr)
										que.push(root->rchild);
									--size;
								}
							}
						}

猜你喜欢

转载自blog.csdn.net/N1314N/article/details/89412534
今日推荐