树相关的全部代码,可直接运行

#include <iostream>
#include<cstdio>
#include<cstdlib>
#include <stack>
#include <queue>

using namespace std;
typedef int TelemType;
typedef struct BinaryTreeTree
{
    TelemType data;
    struct BinaryTreeTree *left;
    struct BinaryTreeTree *right;
}Tree;

//输入格式为前序  1 2 4 0 0 0 3 0 0
//或者  1 2 4 7 0 0 0 5 0 0 3 6 0 0 0
//创建二叉树,顺序依次为中间节点->左子树->右子树
Tree* createBinaryTree()
{
    Tree *p;
    TelemType ch;
    cin>>ch;
    if(ch == 0)     //如果到了叶子节点,接下来的左、右子树分别赋值为0
    {
        p = nullptr;
    }
    else
    {
        p = (Tree*)malloc(sizeof(Tree));
        p->data = ch;
        p->left  = createBinaryTree();  //递归创建左子树
        p->right = createBinaryTree();  //递归创建右子树
    }
    return p;
}
/**递归方式遍历***/
//先序遍历
void PreOrder(Tree* root)
{
    if( root == nullptr )
        return;

    cout<<root->data<<' ';
    PreOrder(root->left);
    PreOrder(root->right);

}

//中序遍历
void InOrder(Tree* root)
{
    if( root == nullptr )
        return;

    InOrder(root->left);
    cout<<root->data<<' ';
    InOrder(root->right);
}

//后序遍历
void PostOrder(Tree* root)
{
    if( root == nullptr )
       return;
    PostOrder(root->left);
    PostOrder(root->right);
    cout<<root->data<<' ';
}

//二叉树的层次遍历
void LevelOrder(Tree *root)
{
    if( root == nullptr )
        return;
    queue<Tree *> Q;
    Q.push(root);
    while( !Q.empty())
    {
        Tree *tmp = Q.front();
        if(tmp->left != nullptr)
            Q.push(tmp->left);
        if(tmp->right != nullptr)
            Q.push(tmp->right);
        cout << tmp->data << ' ';
        Q.pop();
    }
}

/***非递归方式遍历***/
//先序遍历
void PreOrderT(Tree * root)
{
    if(root == nullptr)
        return ;
    stack<Tree *>s;
    s.push((root));
    Tree *p = nullptr;
    while(!s.empty())
    {
        p = s.top();
        s.pop();
        cout << p->data << " ";
        if( p->right)
            s.push(p->right);
        if(p->left)
            s.push(p->left);
    }
}


//中序遍历
void InOrderT(Tree *root){
    if(root == nullptr)
        return ;
    stack<Tree *>s;
    Tree *p = root;
    while(p != nullptr || !s.empty())
    {
        if(p != nullptr)
        {
            s.push(p);
            p = p->left;
        }
        else{
            p = s.top();
            s.pop();
            cout << p->data << " ";
            p = p->right;
        }
    }
}



//后序遍历
void PostOrderT(Tree *root){
    if(root == nullptr)
        return ;
    stack<Tree *> s;
    vector<int> result;
    s.push(root);
    Tree *p =nullptr;
    while(!s.empty())
    {
        p = s.top();
        s.pop();;
        result.insert(result.begin(),p->data);
        if(p->left)
            s.push(p->left);
        if(p->right)
            s.push(p->right);
    }
    for(int i = 0; i < result.size(); i++)
        cout << result[i] << " ";

}
//二叉树节点总数目
int Treenum(Tree* root)
{
    if(root == nullptr)
    {
        return 0;
    }
    else
    {
        return 1+Treenum(root->left)+Treenum(root->right);
    }
}

//二叉树的深度
int DepthOfTree(Tree* root)
{
    if(root)
    {
        return DepthOfTree(root->left)>DepthOfTree(root->right)?DepthOfTree(root->left)+1:DepthOfTree(root->right)+1;
    }
    if( root == nullptr)
    {
        return 0;
    }
}


//二叉树叶子节点数
int Leafnum(Tree* root)
{
    if(root == nullptr)
    {
        return 0;
    }
    else if(  (root->left == nullptr) && (root->right == nullptr) )
    {
        return 1;
    }
    else
    {
        return  (Leafnum(root->left) + Leafnum(root->right)) ;
    }
}


// 遍历二叉树最右边节点
vector<int> levelright(Tree * root){
    vector<int>result;
    if(root==nullptr)
        return result;
    queue<Tree *>bfs;
    bfs.push(root);

    while(!bfs.empty())
    {
        int n = bfs.size();
        int t_Data = 0;
        for(int i=0; i<n; ++i)
        {
            Tree *tmp = bfs.front();
            bfs.pop();
            t_Data = tmp->data;
            if(tmp->left)
                bfs.push(tmp->left);
            if(tmp->right)
                bfs.push(tmp->right);
        }
        result.push_back(t_Data);
    }
     return result;
}





//或者  1 2 4 7 0 0 0 5 0 0 3 6 0 0 0
int main()
{
    Tree *root = nullptr;
    root = createBinaryTree();
    printf("二叉树建立成功");
    cout<<endl;

    cout<<"二叉树总节点数为:"<<Treenum(root)<<endl;

    cout<<"二叉树深度为:"<<DepthOfTree(root)<<endl;

    cout<<"二叉树叶子节点数为:"<<Leafnum(root)<<endl;

    cout << endl;
    cout << "以下是递归遍历的结果"<<endl<<endl;
    cout<<"前序遍历结果:"<<endl;
    preOrderTraverse(root);
    cout<<endl;

    cout<<"中序遍历结果:"<<endl;
    inOrderTraverse(root);
    cout<<endl;

    cout<<"后序遍历结果:"<<endl;
    lastOrderTraverse(root);
    cout<<endl;

    cout<<endl;
    cout << "以下是非递归遍历的结果"<<endl<<endl;

    cout<<"前序遍历结果:"<<endl;
    preOrderF(root);
    cout<<endl;

    cout<<"中序遍历结果:"<<endl;
    InOrderT(root);
    cout<<endl;

    cout<<"后序遍历结果:"<<endl;
    PostOrderT(root);
    cout<<endl;


    cout<<"层次遍历结果:"<<endl;
    LevelOrder(root);
    cout<<endl<<endl;

    cout << "最右边节点"<<endl;
    vector<int>aa;
    aa = levelright(root);
    for(int i = 0; i < aa.size();i++)
      cout << aa[i] << " ";
    cout << endl;

    return 0;
}

猜你喜欢

转载自blog.csdn.net/feixi7358/article/details/82911616
今日推荐