1. 程式人生 > >樹相關的全部程式碼,可直接執行

樹相關的全部程式碼,可直接執行

#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;
}