二叉树的部分操作实现
来源:互联网 发布:按键精灵后台源码 编辑:程序博客网 时间:2024/05/17 05:50
#pragma once
#include <iostream>
#include <string>
#include <queue>
#include <stack>
using namespace std;
template<class T>
struct BiNode
{
T data;
BiNode* left,*right;
BiNode():left(NULL),right(NULL){};
BiNode(const T& item,BiNode<T>* lptr=NULL,BiNode<T>* rptr=NULL)
:data(item),left(lptr),right(rptr){}
};
template<class T>
class BinaryTree
{
//friend ostream& operator<<(ostream& out,BinaryTree<T> &_root);
public:
BinaryTree();
BinaryTree(const BinaryTree<T>& _root);
void PreorderTraverse();//先序遍历二叉树
void InorderTraverse();//中序遍历二叉树
void PostorderTraverse();//后序遍历二叉树
void LevelorderTraverse();//层次遍历二叉树
void countLeaf(int &count);//统计二叉树中的叶节点数
void nonRecursivePreOrder();
void nonRecursiveInOrder();
void nonRecursivePostOrder();
int depth();//计算树的深度
~BinaryTree();
private:
BiNode<T>* root;
BiNode<T>* copyTree(BiNode<T>* t);//复制二叉树
void deleteTree(BiNode<T>* t);//删除二叉树
void preTraverse(BiNode<T>* _root);
void inTraverse(BiNode<T>* _root);
void postTraverse(BiNode<T>* _root);
void countLeafNum(BiNode<T>* _root,int &count);
int treeDepth(BiNode<T>* _root);
};
template<class T>
BinaryTree<T>::BinaryTree():root(NULL)
{
BiNode<T> *b,*c,*d,*e,*f,*g,*h,*i;
T _item;
cout<<"Please input the value of Node g:";
cin>>_item;
g=new BiNode<T>(_item);
cout<<"Please input the value of Node h:";
cin>>_item;
h=new BiNode<T>(_item);
cout<<"Please input the value of Node i:";
cin>>_item;
i=new BiNode<T>(_item);
cout<<"Please input the value of Node d:";
cin>>_item;
d=new BiNode<T>(_item,(BiNode<T>*)NULL,g);
cout<<"Please input the value of Node e:";
cin>>_item;
e=new BiNode<T>(_item,h,i);
cout<<"Please input the value of Node f:";
cin>>_item;
f=new BiNode<T>(_item);
cout<<"Please input the value of Node b:";
cin>>_item;
b=new BiNode<T>(_item,d,(BiNode<T>*)NULL);
cout<<"Please input the value of Node c:";
cin>>_item;
c=new BiNode<T>(_item,e,f);
cout<<"Please input the value of Node root:";
cin>>_item;
root=new BiNode<T>(_item,b,c);
}
template<class T>
BiNode<T>* BinaryTree<T>::copyTree(BiNode<T> *_root)
{
BiNode<T>* newLeft,*newRight,*newRoot;
if(_root==NULL)
return NULL;
newLeft=copyTree(_root->left);
newRight=copyTree(_root->right);
newRoot=new BiNode<T>(_root->data,newLeft,newRight);
return newRoot;
}
template<class T>
BinaryTree<T>::BinaryTree(const BinaryTree<T> &_root)
{
root=copyTree(_root.root);
}
template<class T>
void BinaryTree<T>::deleteTree(BiNode<T> *_root)
{
if(_root!=NULL)
{
deleteTree(_root->left);
deleteTree(_root->right);
delete _root;
}
}
template<class T>
BinaryTree<T>::~BinaryTree()
{
deleteTree(root);
}
template<class T>
void BinaryTree<T>::preTraverse(BiNode<T>* _root)
{
if(_root!=NULL)
{
cout<<_root->data<<"——>";
preTraverse(_root->left);
preTraverse(_root->right);
}
}
template<class T>
void BinaryTree<T>::inTraverse(BiNode<T>* _root)
{
if(_root!=NULL)
{
inTraverse(_root->left);
cout<<_root->data<<"——>";
inTraverse(_root->right);
}
}
template<class T>
void BinaryTree<T>::postTraverse(BiNode<T>* _root)
{
if(_root!=NULL)
{
postTraverse(_root->left);
postTraverse(_root->right);
cout<<_root->data<<"——>";
}
}
template<class T>
void BinaryTree<T>::PreorderTraverse()
{
preTraverse(root);
}
template<class T>
void BinaryTree<T>::InorderTraverse()
{
inTraverse(root);
}
template<class T>
void BinaryTree<T>::PostorderTraverse()
{
postTraverse(root);
}
template<class T>
void BinaryTree<T>::LevelorderTraverse()
{
queue<BiNode<T> *> q;
BiNode<T>* p;
q.push(root);
while(!q.empty())
{
p=q.front();
q.pop();
cout<<p->data<<"——>";
if(p->left!=NULL)
q.push(p->left);
if(p->right!=NULL)
q.push(p->right);
}
}
template<class T>
void BinaryTree<T>::countLeafNum(BiNode<T> *_root,int &count)
{
if(_root!=NULL)
{
if(_root->left==NULL&&_root->right==NULL)
count++;
countLeafNum(_root->left,count);
countLeafNum(_root->right,count);
}
}
template<class T>
void BinaryTree<T>::countLeaf(int &count)
{
countLeafNum(root,count);
}
template<class T>
int BinaryTree<T>::treeDepth(BiNode<T> *_root)
{
int depthleft,depthright,depthval;
if(_root==NULL)
depthval=-1;
else
{
depthleft=treeDepth(_root->left);
depthright=treeDepth(_root->right);
depthval=1+(depthleft>depthright?depthleft:depthright);
}
return depthval;
}
template<class T>
int BinaryTree<T>::depth()
{
return treeDepth(root);
}
template<class T>
void BinaryTree<T>::nonRecursivePreOrder()
{
stack<BiNode<T> *> sNode;
BiNode<T> *q,*p=root;
if(p!=NULL)
{
cout<<p->data<<"——>";
sNode.push(p);
}
p=p->left;
while (!sNode.empty()||p!=NULL )
{
while(p!=NULL)
{
cout<<p->data<<"——>";
sNode.push(p);
p=p->left;
}
if(!sNode.empty())
{
q=sNode.top();
sNode.pop();
p=q->right;
}
}
}
template<class T>
void BinaryTree<T>::nonRecursiveInOrder()
{
stack<BiNode<T> *> sNode;
BiNode<T> *p=root,*q;
if(p!=NULL)
sNode.push(p);
p=p->left;
while(!sNode.empty()||p!=NULL)
{
while(p!=NULL)
{
sNode.push(p);
p=p->left;
}
if(!sNode.empty())
{
q=sNode.top();
sNode.pop();
cout<<q->data<<"——>";
p=q->right;
}
}
}
template<class T>
void BinaryTree<T>::nonRecursivePostOrder()
{
stack<BiNode<T> *> sNode;
BiNode<T> *p=root,*q,*pre=NULL;
if(p!=NULL)
sNode.push(p);
p=p->left;
while(!sNode.empty()||p!=NULL)
{
while(p!=NULL)
{
sNode.push(p);
p=p->left;
}
if(!sNode.empty())
{
q=sNode.top();
p=q->right;
if(p==NULL||p==pre)
{
cout<<q->data<<"——>";
sNode.pop();
pre=q;
p=NULL;
}
}
}
}
- 二叉树的部分操作实现
- 二叉树的部分操作
- 有关二叉树的部分操作
- 数据结构中二叉树实现及部分操作
- 数据结构面试之五—二叉树的常见操作(递归实现部分)
- 数据结构面试之五—二叉树的常见操作(递归实现部分
- 数据结构面试之五—二叉树的常见操作(递归实现部分)
- 二叉树的基本操作及部分面试题
- 二叉树的基本操作及部分面试题
- 二叉树操作实现
- 数据结构:二叉树线索化的部分实现
- 二叉树的简单实现及其部分面试题
- 二叉树基本操作的程序实现
- 实现二叉树操作的主函数
- java实现二叉树的常见操作
- java实现二叉树的常见操作
- java实现二叉树的常见操作
- Java实现二叉树的基本操作
- AjaxPro.NET框架完成服务端即时数据验证(Asp.net 2.0)(示例代码下载)
- 2440从NANDFlash启动之bootloader运行以前
- 配置及运行你的第一个java程序
- 探求真正的SOA
- 关于 www.6700.cn病毒的查杀与感想
- 二叉树的部分操作实现
- 26个字母的爱情诠释
- 用grub4dos做usb-cdrom U盘启动,引导maxdos,并集成Ubuntu操作系统、I386(XP)
- 总结必须学习的10项.NET技术
- 一个好用的内存泄漏检测工具-Visual Leak Detector
- 微软资深软件架构师成长之道
- AXIS生成框架及发布
- commons-fileupload.jar 使用说明
- 文房四宝