递归、栈、非递归非栈实现二叉树的遍历

来源:互联网 发布:上海优越感知乎 编辑:程序博客网 时间:2024/06/09 20:17

基于迎春花开365天利用栈实现二叉树的先序、中序、后序遍历的非递归操作

以及  Alvin-Qu非递归,不用栈实现二叉树中序遍历 的算法,本人对二叉树的遍历做出系统的总结(C++实现),包含先序、中序、后序的递归、栈实现、非递归非栈实现。其中以双栈作为辅助数据结构的后序遍历算法,来源于jimolangge123二叉树的后序遍历实现(基于两个栈实现)


先序遍历的三种算法:

//递归实现先序遍历void preorderTree(binNode<Elem>* root){if(root==NULL){return;}else{cout<<root->getElement();//print the valuepreorderTree(root->left());preorderTree(root->right());}}//非递归实现先序遍历(使用栈作为辅助数据结构)void preorderTree(binNode<Elem>* root){stack<binNode<Elem>*> Stack;if(root==NULL){return;}while(root||!Stack.empty()){while(root){Stack.push(root);cout<<root->getElement();//遍历root=root->left();}root=Stack.top();Stack.pop();root=root->right();}}//不使用栈,非递归先序遍历BST/* *每个节点有个parent指针进行回溯*/void preorderTree(binNode<Elem>* root){if(root==NULL){return;}while(root->left()){cout<<root->getElement();//遍历root=root->left();}while(root!=NULL){if(root->right(){//该节点有右孩子root=root->right();cout<<root->getElement();//遍历while(root->left()){//指向该子树的最左端root=root->left();cout<<root->getElement();//遍历}}else{//没有右孩子    binNode<Elem>* temp=NULL;        do{//溯回temp=root;root=root->parent();}while(root!=NULL&&temp==root->right());}}}


中序遍历:

//递归实现中序遍历void inorderTree(binNode<Elem>* root){if(root==NULL){return;}else{inorderTree(root->left());cout<<root->getElement();//print the valueinorderTree(root->right());}}//非递归实现中序遍历(使用栈作为辅助数据结构)void inorderTree(binNode<Elem>* root){stack<binNode<Elem>*> Stack;if(root==NULL){return;}while(root||!Stack.empty()){while(root){Stack.push(root);root=root->left();}root=Stack.top();Stack.pop();cout<<root->getElement();root=root->right();}}//不使用栈,非递归中序遍历BST/* *每个节点有个parent指针进行回溯*/void inorderTree(binNode<Elem>* root){if(root==NULL){return;}while(root->left()){root=root->left();}while(root!=NULL){cout<<root->getElement();//遍历if(root->right(){//该节点有右孩子root=root->right();while(root->left()){//指向该子树的最左端root=root->left();}}else{//没有右孩子    binNode<Elem>* temp=NULL;        do{//溯回temp=root;root=root->parent();}while(root!=NULL&&temp==root->right());}}}

后序遍历:

//递归实现后序遍历void postorderTree(binNode<Elem>* root){if(root==NULL){return;}else{inorderTree(root->left());inorderTree(root->right());cout<<root->getElement();//print the value}}//非递归实现后序遍历/**使用双栈作为辅助数据结构*/void postorderTree(binNode<Elem>* root){stack<binNode<Elem>*> Stack1;stack<binNode<Elem>*> Stack2;if(root==NULL){return;}Stack1.push(root);while(!Stack1.empty()){root=Stack1.top();Stack2.push(root);Stack1.pop();if(root->left()) Stack1.push(root->left());if(root->right()) Stack1.push(root->right());}while(!Stack2.empty()){root=Stack2.top();Stack2.pop();cout<<root->getElement();}}//不使用栈,非递归后序序遍历BST/* *每个节点有个parent指针进行回溯*/void postorderTree(binNode<Elem>* root){if(root==NULL){return;}while(root->left()){root=root->left();}while(root!=NULL){if(root->right()){//该节点有右孩子root=root->right();while(root->left()){//指向该子树的最左端root=root->left();}}else{//没有右孩子     //溯回操作    binNode<Elem>* temp=root;root=root->parent();if(temp==root->left()){cout<<temp->getElement();}        while(temp==root->right()||root){cout<<temp->getElement();temp=root;root=root->parent();}}}}


0 0