C++ 实现求二叉树的深度及遍历(递归与非递归算法)
2013-03-29 14:48
543 查看
#include <iostream> #include <deque> #include <stack> using namespace std; struct BSTNode { int data; BSTNode * LChild,* RChild; }; class BST { private: BSTNode *T; public: ~BST(); BSTNode * GetRoot(); void BSTCreate(); void BSTInsert(BSTNode *); int BSTDepth(BSTNode *);//求树的深度 递归 void Depth();//树的深度 非递归 void BSTBFS();//广度优先遍历 void PreOrder(BSTNode *);//递归 前序遍历 void BSTPre();//前序遍历 非递归 void InOrder(BSTNode *);//递归 中序遍历 void BSTIn();//中序遍历 非递归 void PostOrder(BSTNode *);// 递归 后续遍历 void BSTPost();//后续遍历 双栈法 void Post();//后序遍历 单栈 void BSTSearch();//查找 递归算法 BSTNode * Search(BSTNode *,int); }; BST::~BST() { deque<BSTNode *> de; BSTNode *p=T; if (p) { if(p->LChild) de.push_back(p->LChild); if(p->RChild) de.push_back(p->RChild); delete p; while (!de.empty()) { p=de.front(); de.pop_front(); if(p->LChild) de.push_back(p->LChild); if(p->RChild) de.push_back(p->RChild); delete p; } } } BSTNode * BST::GetRoot() { return T; } void BST::BSTInsert(BSTNode *s ) { BSTNode *f=T; BSTNode *p=T; while (p) { f=p; if(s->data<p->data) p=p->LChild; else p=p->RChild; } if(T==NULL) T=s; else if(s->data<f->data) f->LChild=s; else f->RChild=s; } void BST::BSTCreate() { T=NULL; int data; BSTNode *s; cout<<"请输入节点值并以0作为结束标志,创建一个二叉排序树"<<endl; cin>>data; while(data) { s=new BSTNode; s->LChild=s->RChild=NULL; s->data=data; BSTInsert(s); cout<<"请输入下一个节点的值:"<<endl; cin>>data; } } int BST::BSTDepth(BSTNode *p) { if(p) { int left=BSTDepth(p->LChild); int right=BSTDepth(p->RChild); if(left>right) return left+1; else return right+1; } return 0; } void BST::Depth() { BSTNode * store[50]; memset(store,0,50); int length=0; int depth=0; stack<BSTNode *> st; if (T) { st.push(T); while (!st.empty()) { depth++; length=0; while (!st.empty()) { BSTNode * p=st.top(); st.pop(); if (p->LChild) store[length++]=p->LChild; if (p->RChild) store[length++]=p->RChild; } for (int i=0;i<length;i++) st.push(store[i]); } cout<<"树的深度为:"<<depth<<endl; } else cout<<"还未创建树!!"<<endl; } void BST::BSTBFS() { cout<<"广度优先遍历"<<endl; deque<BSTNode *> de; BSTNode *p=T; de.push_back(p); while (!de.empty()) { p=de.front(); cout<<p->data<<"--->"; de.pop_front(); if (p->LChild) de.push_back(p->LChild); if(p->RChild) de.push_back(p->RChild); } cout<<endl; } void BST::PreOrder(BSTNode * p) { if(p) { cout<<p->data<<"--->"; PreOrder(p->LChild); PreOrder(p->RChild); } } void BST::BSTPre() { cout<<"前序遍历 非递归"<<endl; stack<BSTNode *> st; st.push(T); while(!st.empty()) { BSTNode *P=st.top(); st.pop(); cout<<P->data<<"--->"; if (P->RChild) st.push(P->RChild); if(P->LChild) st.push(P->LChild); } cout<<endl; } void BST::InOrder(BSTNode * p) { if(p) { InOrder(p->LChild); cout<<p->data<<"--->"; InOrder(p->RChild); } } void BST::BSTIn() { cout<<"中序遍历 非递归"<<endl; stack<BSTNode *>st; st.push(T); while (!st.empty()) { BSTNode *p=st.top(); while (p->LChild) { p=p->LChild; st.push(p); } while (!st.empty()) { BSTNode *q=st.top(); st.pop(); cout<<q->data<<"--->"; if(q->RChild) { q=q->RChild; st.push(q); break; } } } cout<<endl; } void BST::PostOrder(BSTNode * p) { if(p) { PostOrder(p->LChild); PostOrder(p->RChild); cout<<p->data<<"--->"; } } void BST::BSTPost() { cout<<"后序遍历 非递归"<<endl; stack<BSTNode*> st2; stack<BSTNode *> st; st.push(T); while(!st.empty()) { BSTNode *P=st.top(); st.pop(); st2.push(P); if(P->LChild) st.push(P->LChild); if (P->RChild) st.push(P->RChild); } while (!st2.empty()) { BSTNode *q=st2.top(); st2.pop(); cout<<q->data<<"---->"; } cout<<endl; } void BST::Post() { cout<<"后续遍历 非递归"<<endl; stack<BSTNode *>st; st.push(T); while (!st.empty()) { BSTNode *p=st.top(); while (p->LChild||p->RChild) { if (p->LChild) p=p->LChild; else p=p->RChild; st.push(p); } while (!st.empty()) { BSTNode *q=st.top(); st.pop(); if(!st.empty()) { BSTNode *f=st.top(); cout<<q->data<<"--->"; if (q==f->LChild) { if (f->RChild) { st.push(f->RChild); break; } } } else cout<<q->data<<endl; } } } BSTNode * BST::Search(BSTNode *p,int x) { if(p==NULL||p->data==x) return p; else if(x<p->data) return Search(p->LChild,x); else return Search(p->RChild,x); } void BST::BSTSearch() { int key; cout<<"请输入所要查找结点的关键字:"<<endl; cin>>key; BSTNode * p=Search(T,key); if (p) { cout<<"查找结点的数据为:"<<p->data; } else cout<<"所查找的节点不存在!"<<endl; } int main() { BST bst; bst.BSTCreate(); cout<<"树的深度为:"<<bst.BSTDepth(bst.GetRoot())<<endl; bst.Depth(); //bst.PreOrder(bst.GetRoot()); //bst.InOrder(bst.GetRoot()); //bst.PostOrder(bst.GetRoot()); //bst.BSTBFS(); //bst.BSTPre(); //bst.BSTIn(); //bst.BSTPost(); //bst.Post(); //bst.BSTSearch(); return 0; }
相关文章推荐
- C++ 实现求二叉树的深度及便利(递归与非递归算法)
- 数据结构(六)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 二叉树(前中后序递归非递归遍历,层次遍历,C++实现)
- C++实现二叉树的递归与非递归遍历
- 二叉树前序、中序、后序遍历的递归与非递归算法实现
- C++实现——二叉树的四种遍历(非递归写法)
- 数据结构(六)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 二叉树 前中后序遍历非递归版本 C++实现
- 二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 二叉树的广度优先遍历、深度优先遍历的递归和非递归实现方式
- C++实现二叉树的递归遍历与非递归遍历(先序、中序、后序、层序)
- 二叉树的建立(非递归建立与定义建立)与基本操作(广度和深度遍历,求叶子树高)实现
- java 实现二叉树深度优先遍历的 前、中、后序遍历(递归)
- C++实现二叉树的递归和非递归遍历方法
- c++实现二叉树的查找,插入,删除,深度,叶子节点数,度为1的节点数(递归方法)及运行实例结果
- 数据结构(六)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 二叉树的递归遍历与非递归算法实现
- c语言实现二叉树先序,中序,后序(递归),层次遍历,求叶子节点个数及树的深度,下一篇写非递归的遍历
- 二叉树的广度优先遍历、深度优先遍历的递归和非递归实现方式 二叉树的遍历方式: 1、深度优先:递归,非递归实现方式 1)先序遍历:先访问根节点,再依次访问左子树和右子树 2)中序遍
- 二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现