有关二叉树的递归算法

来源:互联网 发布:什么数据库8000多 编辑:程序博客网 时间:2024/06/01 17:39

此二叉树可实现一下功能:

判断二叉树是否为完全二叉树

计算度为1的结点的个数

计算度为2的结点的个数

计算度为0(叶子结点)的结点的个数

统计二叉树的高度(默认根的高度为1)

统计二叉树的宽度

计算各结点中最大元素的值

交换每个结点的左孩子和右孩子

从二叉树中删去所有叶子结点


建树按照先序建树 输入0结束

输入样例:

1 3 6 8 0 0 0 4 0 0 2 7 0 0 0

输出样例:

不是完全二叉树

度为0的结点的个数为:3

度为1的结点的个数为:2

度为2的结点的个数为:2

树的高度为:4

树的宽度为:3

最大的元素值为:8

交换后的先序为:1 2 7 3 4 6 8 

删除叶子后的先序为:1 2 3 6 



上代码:

#include<algorithm>#include<iostream>#include<cstdlib>#include<cstring>#include<cstdio>#include<string>#include<stack>#include<queue>#include<cmath>#include<stack>#include<list>#include<map>#include<set>#define up(i,x,y) for(i=x;i<=y;i++)#define down(i,x,y) for(i=x;i>=y;i--)#define mem(a,x) memset(a,x,sizeof(a))#define gi(x) scanf("%d",&x)#define gi2(x,y) scanf("%d%d",&x,&y)#define gll(x) scanf("%lld",&x)#define gll2(x,y) scanf("%lld%lld",&x,&y)#define gc(x) scanf("%c",&x)#define gc2(x,y) scanf("%c%c",&x,&y)using namespace std;const double eps=1e-8; typedef long long ll;const int MAXN=100005;const int mod=1000000007;const ll llinf = (ll)(1e18) + 500;const int inf=0x3f3f3f3f;int num[3];int num_of_node=0;int cnt=0;int b[100];int h=0;int maxn=-MAXN;template <class T>class Node {public:Node *lson;Node *rson;T data;};template <class T>class binarytree{private:Node<T>* root;public:binarytree(){root=NULL;}void Create(){create(root);}Node<T>* getroot(){return root;}void create(Node<T>* &p){T c;cin>>c;if(c==0){p=NULL;return;}p=new Node<T>;p->data=c;create(p->lson);create(p->rson);}bool isEmpty(){if(root==NULL)return true;return false;}void bdisplay(){//宽度优先queue< Node<T>* >q;q.push(root);while(!q.empty()){Node<T>* u=q.front();q.pop();cout<<u->data<<endl;if(u->lson){q.push(u->lson);}if(u->rson){q.push(u->rson);}}}void display(){//深度优先if(isEmpty())printf("NULL\n");else visit(root),cout<<endl;}void visit(Node<T>* p){if(p==NULL)return;cout<<p->data<<' ';visit(p->lson),visit(p->rson);}void caldu(){//前6个小题if(isEmpty()){printf("NULL\n");return;}//b[1]=1;calduvisit(root,1);for(int i=0;i<3;i++){printf("度为%d的结点的个数为:",i);cout<<num[i]<<endl;}cout<<"树的高度为:"<<h<<endl;int breadth=0;for(int i=1;i<=h;i++){breadth=max(breadth,b[i]);}cout<<"树的宽度为:"<<breadth<<endl;cout<<"最大的元素值为:"<<maxn<<endl;}void calduvisit(Node<T>* p,int s){if(p==NULL)return;b[s]++;maxn=max(maxn,p->data);h=max(s,h);if(p->lson==NULL&&p->rson==NULL){num[0]++;}else if(p->lson!=NULL&&p->rson!=NULL){num[2]++;}else {num[1]++;}calduvisit(p->lson,s+1),calduvisit(p->rson,s+1);}void deleteleaf(){if(isEmpty()){printf("NULL\n");return;}if(root->lson==NULL&&root->rson==NULL){cout<<"只有一个根节点"<<endl;return ;}deleleaf(root);}void deleleaf(Node<T>* p){if(p==NULL)return;if(p->lson!=NULL){Node<T>* plson=p->lson;if(plson->lson==NULL&&plson->rson==NULL){delete p->lson;p->lson=NULL;}}if(p->rson!=NULL){Node<T>* prson=p->rson;if(prson->lson==NULL&&prson->rson==NULL){delete p->rson;p->rson=NULL;}}deleleaf(p->lson),deleleaf(p->rson);}void Exchange(){if(root==NULL){printf("NULL\n");return;}else exchange(root);}void exchange(Node<T>* &p){if(p==NULL)return ;swap(p->lson, p->rson);exchange(p->lson),exchange(p->rson);}void deletefree(Node<T>* &p){if(p==NULL)return ;deletefree(p->lson);deletefree(p->rson);//cout<<p->data<<' ';delete p;}bool is_complete(Node<T>* p){if(isEmpty()){printf("NULL\n");return false;}queue<Node<T>* >q;q.push(p);//p=q.pop();while(q.front()!=NULL){p=q.front();q.pop();q.push(p->lson);q.push(p->rson);}while (!q.empty()) {p=q.front();if(p!=NULL)return false;q.pop();}return true;}~binarytree(){deletefree(root);}};//1 3 6 8 0 0 0 4 0 0 2 7 0 0 0int main(){mem(num, 0);mem(b,0);binarytree<int>tree;tree.Create();if(tree.is_complete(tree.getroot())){printf("是完全二叉树\n");}else printf("不是完全二叉树\n");tree.caldu();tree.Exchange();cout<<"交换后的先序为:";tree.display();tree.deleteleaf();cout<<"删除叶子后的先序为:";tree.display();return 0;}


原创粉丝点击