数的遍历c++实现
来源:互联网 发布:java应聘笔试编程题 编辑:程序博客网 时间:2024/06/06 04:56
#include#include#ifndef BINARY_SEARCH_TREE#define BINARY_SEARCH_TREEusing namespace std;templateclass Stack:public stack{ public:T pop(){ T tmp = top(); stack::pop(); return tmp;}void push(T el){stack::push(el);}};template class Queue:public queue{public:T dequeue(){T tmp=front();queue::pop();return tmp;}void enqueue(const T& el){push(el);}};templateclass BSTNode{public:BSTNode(){left=right=0;} BSTNode(const T&el,BSTNode *l=0,BSTNode *r=0) { key=el;left=l;right=r; } T key; BSTNode *left,*right;};templateclass BST{BST(){root=0;}~BST(){clear();}void clear(){clear(root); root=0;}bool isEmpty()const{return root=0;}void preorder(){preorder(root);}void inorder(){inorder(root);}void postorder(){postorder(root);}T & search(const T &el)const{return serach(root,el);}void breadthFirst();void iterativePreorder();void iterativeInorder();void iterativePostorder();void deleteByMerging(BSTNode *&);void findAndDeleteMerging(const T&);void deleteByCopying(BSTNode* &);void balance(T *,int,int);protected:BSTNoderoot;void clear(BSTNode*);T *serach(BSTNode*,const T&)const;void preorder(BSTNode*);void inorder(BSTNode*);void postorder(BSTNode*);virtual void visit(BSTNode*p){cout<key<<' ';}};templateT *BST::serach(BSTNode* p,const T &el)const {while(p!=0)if(el==p->key)return &p->key;else if(elkey)p=p->key;else p=p->right;return 0;}templateclass BST::breadthFirst() {Queue*>queue;BSTNode*p=root;if(p!=0){queue.enqueue(p);while(!queue.empty()){p=queue.dequeue(); // 广度优先算法visit(p);if(p->left!=0)queue.enqueue(p->left);if(p->right!=0)queue.enqueue(p->right); }}}templatevoid BST::inorder(BSTNode*p) // 中序深度优先{if(p!=0){inorder(p->left);visit(p);inorder(p->right);}}templatevoid BST::preorder(BSTNode* p) // 先序 深度优先{if(p!=0){visit(p);preorder(p->left);preorder(p->right);}}void BST::postorder(BSTNode* p) // 后序 深度优先{if(p!=0){postorder(p->left);postorder(p->right);visit(p);}}templatevoid BST::iterativePreorder() // 先序 非递归 堆栈方式实现{stack *>travStack;BSTNode*p =root;if(p!=0){travStack.push(p);while(travStack.empty()){p=travStack.pop();visit(p); if(p->right!=0); travStack.push(p->right); if(p->left!=0); travStack.push(p->left);}}}templatevoid BST::iterativePostorder() //后序 遍历 堆栈方式实现{stack*>travStack;BSTNode*p=root,*q=root;while(p!=0){for(;p->left!=0;p=p->right)travStack.push(p); while(p!=0&&(p->right==0 ||p->right==q) { visit(p); q=p; return; p=travStack.pop(); } travStack.push(p); p=p->right;}} templatevoid BST::iterativeInorder() //中序遍历 堆栈方式实现 {Stack*>travStack;BSTNode *p=root;while(p!=0){while(p!=0){if(p->right)travStack.push(p->right);p=p->left;}p=travStack.pop();while(!travStack.empty()&& p->right==0){visit(p);p=travStack.pop();}visit(p);if(!travStack.empty()) p=travStack.pop();else p=0;}}#endif
- 数的遍历c++实现
- java实现数的遍历
- 用java实现数的深度遍历和广度遍历
- c,水仙花数的实现
- 二叉树的实现和二叉数的遍历实现
- 八.二叉树各种操作的C语言实现 深度遍历求深度,广度遍历求深度,交换左右子树,求叶子节点数
- C语言实现二叉树的遍历
- c实现树的创建和遍历
- C实现二叉树的各种遍历
- 非递归方法实现的数的中序遍历
- JS实现数的遍历,查询,增加节点,删除节点
- 二叉数的遍历
- 二叉数的遍历
- C实现二叉树的先序遍历,中序遍历,后序遍历
- c语言实现二叉树的先序遍历,中序遍历,后序遍历
- C语言实现两数的交换
- c语言实现两个数的平均值
- 十进制数转二进制数的C实现代码
- JavaMail解析电子邮件初步示例(Web程序不完全版)
- JAVA 数据库连接池
- 开博了!
- HDU 1358 Period (KMP)
- 实现自己的Camera
- 数的遍历c++实现
- 实现图像的编辑和合成
- 调用Android自带的播放器播放Audio
- Android数据库设计
- 见识一下
- winxp;ppt2010为何运行微软拼音输入法会非常慢,有解决措施吗
- 利用Service实现背景音乐的播放
- 本博将专门记录本人在VC路上的技术
- Sqlite3支持的数据类型 日期函数 Sqlite3 函数