C/C++实现二叉树的先序,中序,后序,按层遍历以及深度,叶子结点,是否是满二叉树的计算和判断

废话不说,直接上代码:

#include<iostream>
#include<cmath>

#define MAXSIZE 1024
struct tree{
    char data;
    tree *lchild;
    tree *rchild;
};
using namespace std;

void create_tree(tree **ptr,int &sum)
{
    char ch;
    ch=cin.get();
    if(ch=='#')
        (*ptr)==NULL ;
    else
    {
        sum++;
        *ptr=new tree;
        (*ptr)->data=ch;
        create_tree(&((*ptr)->lchild),sum);
        create_tree(&((*ptr)->rchild),sum);
    }
    return ;
}
int is_empty_tree(tree *ptr)
{
    if(ptr==NULL)
        return 1;
    else
        return 0;
}

void traverse_first(tree *ptr)
{
    if(ptr!=NULL)
    {
        cout<<ptr->data;
        traverse_first(ptr->lchild);
        traverse_first(ptr->rchild);
    }
    return ;
}

void traverse_middle(tree *ptr)
{
    if(ptr!=NULL)
    {
        traverse_middle(ptr->lchild);
        cout<<ptr->data;
        traverse_middle(ptr->rchild);
    }
    return ;
}

void traverse_last(tree *ptr)
{
    if(ptr!=NULL)
    {
        traverse_last(ptr->lchild);
        traverse_last(ptr->rchild);
        cout<<ptr->data;
    }
    return ;
}


void traverse_level(tree *ptr)
{
    int front=0;
    int rear=0;
    tree *queue[MAXSIZE];
    tree *str;
    if(!is_empty_tree(ptr))
    {
        queue[rear++]=ptr;
        while(rear!=front)
        {
            str=queue[front++];
            if(!is_empty_tree(str->lchild))
                queue[rear++]=str->lchild;
            if(!is_empty_tree(str->rchild))
                queue[rear++]=str->rchild;
            cout<<str->data;
        }
    }
    return ;
}

int deep_of_tree(tree *ptr)
{
    int deep=0;
    int l_deep=0;
    int r_deep=0;
    if(ptr!=NULL)
    {
        l_deep=deep_of_tree(ptr->lchild);
        r_deep=deep_of_tree(ptr->rchild);
        deep=l_deep > r_deep ? l_deep+1:r_deep+1;
    }
    return deep;
}

void leave_of_tree(tree *ptr,int &num)
{
    if(ptr!=NULL)
    {
        if((ptr->lchild)==NULL && (ptr->rchild)==NULL)
        {
            num++;
            cout<<ptr->data;
        }
        leave_of_tree(ptr->lchild,num);
        leave_of_tree(ptr->rchild,num);
    }
}

void is_full_tree(int deep,int number)
{
    if((pow(2,deep)-1)==number)
        cout<<"This is a full tree"<<endl;
    else
        cout<<"This is not a full tree"<<endl;
    return ;
}



int main()
{
    int number=0;
    int sum=0;
    int deep=0;
    int num_leave=0;
    tree *ptr;
    cout<<"Please build a tree:"<<endl;
    create_tree(&ptr,number);
    cout<<"Traverse by first:"<<endl;
    traverse_first(ptr);
    cout<<endl<<"Traverse by middle:"<<endl;
    traverse_middle(ptr);
    cout<<endl<<"Traverse by last:"<<endl;
    traverse_last(ptr);
    cout<<endl<<"Traverse by level:"<<endl;
    traverse_level(ptr);
    cout<<endl<<"The deep of the tree:"<<endl;
    deep=deep_of_tree(ptr);
    cout<<deep<<endl;
    cout<<"The number of the tree's leave and show them:"<<endl<<"Leave:";
    leave_of_tree(ptr,num_leave);
    cout<<endl<<"leave number:"<<num_leave<<endl<<"Is this a full tree?"<<endl;;
    is_full_tree(deep,number);
    return 0;
}





运行结果:

zhanghang@Ubuntu-14:~$ g++ tree.cpp 
zhanghang@Ubuntu-14:~$ ./a.out 
Please build a tree:
AB#D##CE###
Traverse by first:
ABDCE
Traverse by middle:
BDAEC
Traverse by last:
DBECA
Traverse by level:
ABCDE
The deep of the tree:
3
The number of the tree's leave and show them:
Leave:DE
leave number:2
Is this a full tree?
This is not a full tree

猜你喜欢

转载自blog.csdn.net/qq_43260665/article/details/85203986