二叉树及其基本功能的实现
#include<bits/stdc++.h>
using namespace std;
struct BinTree
{
int data;
BinTree* lchild;
BinTree* rchild;
};
int create(BinTree*(&T))
{
int num;
cin>>num;
if(num==-1)
{
T=NULL;
return 0;
}
else
{
T=(BinTree*)malloc(sizeof(BinTree));
T->data=num;
cout<<"请输入"<<num<<"的左节点的值:"<<endl;
create(T->lchild);
cout<<"请输入"<<num<<"的右节点的值:"<<endl;
create(T->rchild);
}
}
int Preordertraverse(BinTree* T)
{
if(T==NULL)
{
return 0;
}
else
{
cout<<T->data<<" ";
Preordertraverse(T->lchild);
Preordertraverse(T->rchild);
}
}
int Inordertraverse(BinTree* T)
{
if(T==NULL)
{
return 0;
}
else
{
Inordertraverse(T->lchild);
cout<<T->data<<" ";
Inordertraverse(T->rchild);
}
}
int Postordertraverse(BinTree* T)
{
if(T==NULL)
{
return 0;
}
else
{
Postordertraverse(T->lchild);
Postordertraverse(T->rchild);
cout<<T->data<<" ";
}
}
int Destroy(BinTree*(&T))
{
if(T!=NULL)
{
Destroy(T->lchild);
Destroy(T->rchild);
cout<<T->data<<" ";
free(T);
T=NULL;
}
}
int cnt=0;
int leafnum(BinTree* T)
{
if(T!=NULL)
{
if(T->lchild==NULL&&T->rchild==NULL)cnt++;
leafnum(T->lchild);
leafnum(T->rchild);
}
}
int leafcount(BinTree* T)
{
if(T==NULL)return 0;
else if(T->lchild==NULL&&T->rchild==NULL)return 1;
else
{
return leafcount(T->lchild)+leafcount(T->rchild);
}
}
int height(BinTree* T)
{
if(T==NULL)return 0;
else if(T->lchild==NULL&&T->rchild==NULL)return 1;
else
{
return height(T->lchild)+1>height(T->rchild)+1?height(T->lchild)+1:height(T->rchild)+1;
}
}
int Nodecount(BinTree* T)
{
if(T==NULL)return 0;
else return Nodecount(T->lchild)+Nodecount(T->rchild)+1;
}
int BFS(BinTree* T)
{
queue<BinTree*>Q;
BinTree* p;
if(T==NULL)return 0;
else
{
Q.push(T);
while(!Q.empty())
{
cout<<Q.front()->data<<" ";
if(Q.front()->lchild)Q.push(Q.front()->lchild);
if(Q.front()->rchild)Q.push(Q.front()->rchild);
Q.pop();
}
}
}
int largercount(BinTree* T,int x)
{
if(T==NULL)return 0;
else if(T->data>x)return largercount(T->lchild,x)+largercount(T->rchild,x)+1;
else return largercount(T->lchild,x)+largercount(T->rchild,x);
}
int cnt1=0;
int Largercount(BinTree* T,int x)
{
if(T==NULL)return 0;
else if(T->data>x)cnt1++;
Largercount(T->lchild,x);
Largercount(T->rchild,x);
}
int DeleteX(BinTree*(&T),int x)
{
if(T)
{
if(T->data==x)
{
Destroy(T);
}
else
{
DeleteX(T->lchild,x);
DeleteX(T->rchild,x);
}
}
}
BinTree* CopyTree(BinTree* T,BinTree*(&pt))
{
if(T==NULL)pt=NULL;
else
{
pt=new BinTree[1];
pt->data=T->data;
CopyTree(T->lchild,pt->lchild);
CopyTree(T->rchild,pt->rchild);
}
}
BinTree* copyTree(BinTree* T)
{
BinTree* pt;
if(T==NULL)return NULL;
else
{
pt=new BinTree[1];
pt->data=T->data;
pt->lchild=copyTree(T->lchild);
pt->rchild=copyTree(T->rchild);
}
}
int BinTreeEqual(BinTree* T1,BinTree* T2)
{
if(T1==NULL&&T2==NULL)return 1;
else if(T1==NULL||T2==NULL)return 0;
else if(T1->data==T2->data&&BinTreeEqual(T1->lchild,T2->lchild)
&&BinTreeEqual(T1->rchild,T2->rchild))return 1;
else return 0;
}
void Swap(BinTree* T)
{
if(T!=NULL)
{
BinTree* p;
p=T->lchild;
T->lchild=T->rchild;
T->rchild=p;
Swap(T->lchild);
Swap(T->rchild);
}
}
int Inordertraverse1(BinTree* T)
{
if(T)
{
BinTree* p;
stack<BinTree*>S;
S.push(T);
while(!S.empty())
{
while(S.top())S.push(S.top()->lchild);
S.pop();
if(!S.empty())
{
p=S.top();
cout<<p->data<<" ";
S.pop();
S.push(p->rchild);
}
}
}
}
int Inordertraverse2(BinTree* T)
{
stack<BinTree*>S;
BinTree* p=T;
while(p||!S.empty())
{
while(p){S.push(p);p=p->lchild;}
p=S.top();
cout<<p->data<<" ";
S.pop();
p=p->rchild;
}
}
int Preordertraverse1(BinTree* T)
{
stack<BinTree*>S;
BinTree* p=T;
while(p||!S.empty())
{
while(p)
{
cout<<p->data<<" ";
S.push(p);
p=p->lchild;
}
if(!S.empty())
{
p=S.top();
S.pop();
p=p->rchild;
}
}
}
int Postordertraverse1(BinTree* T)
{
stack<BinTree*>S;
BinTree* p=T;
BinTree* r=NULL;
while(p||!S.empty())
{
if(p)
{
S.push(p);
p=p->lchild;
}
else
{
p=S.top();
if(p->rchild!=r&&p->rchild)
{
p=p->rchild;
S.push(p);
p=p->lchild;
}
else
{
S.pop();
cout<<p->data<<" ";
r=p;
p=NULL;
}
}
}
}
int Postordertraverse2(BinTree* T)
{
stack<BinTree*>S;
vector<int>a;
if(!T)return 0;
S.push(T);
while(!S.empty())
{
BinTree* p=S.top();
a.push_back(p->data);
S.pop();
if(p->lchild)S.push(p->lchild);
if(p->rchild)S.push(p->rchild);
}
reverse(a.begin(),a.end());
for(int i=0;i<a.size();i++)cout<<a[i]<<" ";
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
BinTree* T;
cout<<"请输入根节点的值,若输入的值为-1则表示该树为空树:"<<endl;
create(T);
cout<<"先序遍历:"<<endl;
Preordertraverse1(T);
cout<<endl;
cout<<"中序遍历:"<<endl;
Inordertraverse2(T);
cout<<endl;
cout<<"后序遍历:"<<endl;
Postordertraverse2(T);
cout<<endl;
cout<<"广度优先遍历:"<<endl;
BFS(T);
cout<<endl;
cout<<"叶子节点总数为:"<<leafcount(T)<<endl;
cout<<"二叉树的高度为:"<<height(T)<<endl;
cout<<"二叉树的节点总数为:"<<Nodecount(T)<<endl;
cout<<"二叉树中所有大于1的节点总数为:"<< largercount(T,1)<<endl;
cout<<endl;
return 0;
}