通过先序法创建二叉树;然后实现先序、中序、后序和按层遍二叉树;实现求二叉树的总结点数、叶子节点树、单、双分支节点个数;深度、交换二叉树的左右子树等操作。 有操作菜单

看看咯

#include

using namespace std;
//定义二叉树binary 二元的
typedef struct node{
    char data;
    struct node *lchild,*rchild;
}BiTNode,*BiTree;
BiTree init();
void Creatbitree(BiTree &T);
void pre_t(BiTree root);
void in_t(BiTree root);
void pos_t(BiTree root);
void level_t(BiTree p);
int CountNodes(BiTree T);
int CountLeaves(BiTree T);
int CountSingleNode(BiTree T);
int	CountDoubleNode(BiTree T);
int Depth(BiTree T);
void ReChange(BiTree root);



//主方法
int main(){
    int input,n;
    BiTree T= init();
    cout<<"欢迎来到树的操作系统 "<>input;
    switch(input){
        case 1:
        cout<<"请先序创建二叉树"<lchild=NULL;
        T->rchild=NULL;
    }
    return T;
}
//以先序的方式创建二叉树
void Creatbitree(BiTree &T){//指针的指针来实现传值
    char ch;
    cin>>ch;
    if(ch=='#')
        T=NULL;//T指针所指向的空间内容
    else{
        T=new BiTNode;       
        T->data=ch;
        Creatbitree(T->lchild);
        Creatbitree(T->rchild);
        
    }
    
}
//先序遍历
void pre_t(BiTree root){
    if(root!=NULL){
        cout<data;
        pre_t(root->lchild);
        pre_t(root->rchild);
    }
}
//中序遍历
void in_t(BiTree root){
    if(root!=NULL){
        in_t(root->lchild);
        cout<data;
        in_t(root->rchild);
    }
}
//后序遍历
void pos_t(BiTree root){
    if(root!=NULL){
        pos_t(root->lchild);        
        pos_t(root->rchild);
        cout<data;
    }
}
//层序遍历
void level_t(BiTree p){
    BiTree stack[100];
    int front,real;
    front=real=0;
    BiTree curr,pre;
    pre=p;
    while(pre!=NULL){
        cout<data;
        curr=pre->lchild;
        if(curr!=NULL){
            stack[real++]=curr;
        }
        curr = pre->rchild;
		if(curr != NULL){
			stack[real++] = curr;			
		}		
		if( front != real) 
			pre = stack[front++];
		else 
			pre = NULL;
    }

}
//求总结点数
int CountNodes(BiTree T){
    int n = 0;
	if (T != NULL)
	{
		++n;
		n += CountNodes(T->lchild);
		n += CountNodes(T->rchild);
	}
    return n;
	
}
//叶子个数
int CountLeaves(BiTree T)
{
	int n = 0;
	if (T != NULL)
	{
		if (T->lchild == NULL && T->rchild == NULL)
			++n;
		n += CountLeaves(T->lchild);
		n += CountLeaves(T->rchild);
	}
	return n;
    
}
//单分支节点数
int CountSingleNode(BiTree T)
{
	int n = 0;
	if (T != NULL)
	{
		if ((T->lchild == NULL && T->rchild != NULL) || (T->rchild == NULL && T->lchild != NULL))
			++n;
		n += CountSingleNode(T->lchild);
		n += CountSingleNode(T->rchild);
	}
	return n;
    
}
//双分支节点数
int	CountDoubleNode(BiTree T)
{
	int n = 0;
	if (T != NULL)
	{
		if (T->lchild != NULL && T->rchild != NULL)
			++n;
		n += CountDoubleNode(T->lchild);
		n += CountDoubleNode(T->rchild);
	}
	return n;
}
//深度
int Depth(BiTree T)
{
	int m, n;
	if (T == NULL)
	{
		return 0;
	}
	else
	{
		m = Depth(T->lchild);//左子树深度
		n = Depth(T->rchild);//右子树深度
		if (m > n)
		{
			return m + 1;
		}
		else
		{
			return n + 1;
		}
	}
}

//交换二叉树左右子树
void ReChange(BiTree root)
{
    if(root==NULL) return;
    else
    {
        BiTree temp=root->lchild;
        root->lchild=root->rchild;
        root->rchild=temp;
        ReChange(root->lchild);
        ReChange(root->rchild);
    }
}





你可能感兴趣的:(算法,数据结构,c++,b树)