您的位置:首页 > 其它

二叉树基本操作及层次遍历

2015-11-02 17:05 295 查看
#include <iostream>
#include <queue>
#include<iomanip>
using namespace std;
template<class T>
struct Node{
Node(const T &x=0,Node<T> *a=NULL,Node<T> *b=NULL)
{
data = x;
lchild = a;
rchild = b;
}
Node<T> *lchild;
Node<T> *rchild;
T data;
};
int hcount=1,height=1,leaf=0;      //height是树的高度,leaf是叶子结点数
template<class T>
void deletetree(Node<T> * root);       //删除一棵二叉树
template<class T>
void calheight(Node<T> * root);        //求一棵树的高度
template<class T>
void calleaf(Node<T> * root);     //求一棵二叉树的叶子节点树
template<class T>
void copytree(Node<T>  &a,Node<T>  &b);     //复制一棵二叉树,将a复制给b
template<class T>
void exchange(Node<T> * root);     //交换二叉树的左右孩子
template<class T>
void bianli(Node<T> *root);    //遍历
template<class T>
void callqueue(queue<Node<T>*> &q);    //调用队列
template<class T>
void maketree(Node<T> &root,const T&x,Node<T> &lc,Node<T> &rc)    //建树
{
root.data=x;
if(lc.data==-1)
root.lchild==NULL;
else
root.lchild=&lc;
if(rc.data==-1)
root.rchild==NULL;
else
root.rchild=&rc;
}
int main()
{
Node<int> t[100],a(-1),b(-1),*root;
maketree(t[7],4,a,b);
maketree(t[6],5,a,b);
maketree(t[5],6,a,b);
maketree(t[4],7,a,b);
maketree(t[2],2,t[7],t[6]);
maketree(t[3],3,t[5],t[4]);
maketree(t[1],1,t[2],t[3]);
root=&t[1];
bianli(root);
calheight(root);
cout<<"高度"<<height<<endl;
calleaf(root);
cout<<"叶子结点数"<<leaf<<endl;  //虚假ab孩子除以2消去
copytree(t[1],t[0]);
exchange(&t[0]);
bianli(&t[0]);
deletetree(&t[0]);
bianli(&t[0]);
return 0;
}
template<class T>
void deletetree(Node<T> * root)    //删除一棵二叉树
{
if(!root->lchild&&!root->rchild)
{
root=NULL;
delete root;
return ;
}
if(root->lchild)
{
deletetree(root->lchild);
root->lchild=NULL;
}
if(root->rchild)
{
deletetree(root->rchild);
root->rchild=NULL;
}
root->data=-1;
}
template<class T>
void calheight(Node<T> * root)     //求一棵树的高度
{
if(!root->lchild&&!root->rchild)
{
if(hcount>height)
height=hcount;
return ;
}
if(root->lchild)
{
hcount++;
calheight(root->lchild);
hcount--;
}
if(root->rchild)
{
hcount++;
calheight(root->rchild);
hcount--;
}
}
template<class T>
void calleaf(Node<T> *root)    //求一棵二叉树的叶子节点树
{
if(!root->lchild&&!root->rchild)
{
leaf++;
return ;
}
if(root->lchild)
calleaf(root->lchild);
if(root->rchild)
calleaf(root->rchild);
}
template<class T>
void copytree(Node<T>  &a,Node<T>  &b)     //复制一棵二叉树,将a复制给b
{
if(NULL==&b)
{
Node<T> *m=new Node<T>;
b=*m;
}
b.data=a.data;
b.lchild=a.lchild;
b.rchild=a.rchild;
if(!a.lchild&&!a.rchild)
return;
if(a.lchild)
{
copytree(*a.lchild,*b.lchild);
}
if(a.rchild)
{
copytree(*a.rchild,*b.rchild);
}
}
template<class T>
void exchange(Node<T> * root)
{
if(root)
{
Node<T> * temp = root->lchild;
root->lchild=root->rchild;
root->rchild=temp;
exchange(root->lchild);
exchange(root->rchild);
}
}
template<class T>
void bianli(Node<T> * root) //遍历树
{
if(root->data==-1)
{
cout<<"nothing!"<<endl;
return ;
}
queue<Node<T>*> q;
q.push(root);   //push元素入队
callqueue(q);   //队列递归
cout<<endl;
}
template<class T>
void callqueue(queue<Node<T>*> &q)//队列递归
{
if(!q.empty())
{
Node<T>* temp=q.front();             //front()返回队头元素back()返回队尾元素
if(temp->lchild)
q.push(temp->lchild);          //pop()队头元素出队
if(temp->rchild)
q.push(temp->rchild);
cout<<setw(3)<<temp->data;
q.pop();
callqueue(q);
}
}
存一下自己写过的代码
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: