二叉树的基本操作(未注释)

文章详细介绍了C++中二叉树的构造函数如CreateBinaryTree,以及各种遍历方法如PreOrderTraversal、InOrderTraversal和PostOrderTraversal,还有辅助函数如CountNode、CountLeafNode和Depth。
摘要由CSDN通过智能技术生成

代码如下

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define N 100000
using namespace std;
typedef long long ll;
typedef double db;

typedef struct Node{
    int data;
    struct Node *lchild,*rchild;
}*BinaryTree,BinaryNode;

deque<BinaryTree>SequenceQueue;
vector<int>Pre;
vector<int>In;
vector<int>Post;

static inline void CreateBinaryTree(BinaryTree &Tree);
static inline void InPreCreateBinaryTree(int root,int start,int end,BinaryTree &Tree);
static inline void InPostCreateBinaryTree(int root,int start,int end,BinaryTree &Tree);
static inline void PreOrderTraversal(BinaryTree Tree);
static inline void InOrderTraversal(BinaryTree Tree);
static inline void PostOrderTraversal(BinaryTree Tree);
static inline void SequenceTraversal(BinaryTree Tree);
static inline void Traversal(BinaryTree Tree);
static inline void Copy(BinaryTree &Tree,BinaryTree &NewTree);
static inline bool BinaryTreeEmpty(BinaryTree Tree);
static inline int CountNode(BinaryTree Tree);
static inline int CountLeafNode(BinaryTree Tree);
static inline int Depth(BinaryTree Tree);
// static inline 

int main(){
    BinaryTree Tree_1;
    BinaryTree Tree_2;
    BinaryTree Tree_3;
    BinaryTree Tree_4;
    CreateBinaryTree(Tree_1);
    Traversal(Tree_1);
    printf("%d %d %d\n",CountNode(Tree_1),CountLeafNode(Tree_1),Depth(Tree_1));
    int n;
    cin >> n;
    Pre.resize(n);
    In.resize(n);
    Post.resize(n);
    for(int i=0;i<n;i++)cin >> Pre[i];
    for(int i=0;i<n;i++)cin >> In[i];
    for(int i=0;i<n;i++)cin >> Post[i];
    InPreCreateBinaryTree(0,0,n-1,Tree_2);
    Traversal(Tree_2);
    printf("%d %d %d\n",CountNode(Tree_2),CountLeafNode(Tree_2),Depth(Tree_2));
    InPostCreateBinaryTree(n-1,0,n-1,Tree_3);
    Traversal(Tree_3);
    printf("%d %d %d\n",CountNode(Tree_3),CountLeafNode(Tree_3),Depth(Tree_3));
    Copy(Tree_1,Tree_4);
    Traversal(Tree_4);
    printf("%d %d %d\n",CountNode(Tree_4),CountLeafNode(Tree_4),Depth(Tree_4));
    return 0;
}

static inline void CreateBinaryTree(BinaryTree &Tree){
    int data;
    cin >> data;
    if(data==-1){
        Tree = NULL;
    }
    else{
        Tree = new BinaryNode;
        Tree->data = data;
        CreateBinaryTree(Tree->lchild);
        CreateBinaryTree(Tree->rchild);
    }
}

static inline void InPreCreateBinaryTree(int root,int start,int end,BinaryTree &Tree){
    if(start>end){
        Tree = NULL;
        return;
    }
    int i=start;
    while(i<end && In[i]!=Pre[root])i++;
    Tree = new BinaryNode;
    InPreCreateBinaryTree(root+1,start,i-1,Tree->lchild);
    InPreCreateBinaryTree(root+(i-start+1),i+1,end,Tree->rchild);
    Tree->data = In[i];
}

static inline void InPostCreateBinaryTree(int root,int start,int end,BinaryTree &Tree){
    if(start>end){
        Tree = NULL;
        return;
    }
    int i=start;
    while(i<end && In[i]!=Post[root])i++;
    Tree = new BinaryNode;
    Tree->data = In[i];
    InPostCreateBinaryTree(root-(end-i+1),start,i-1,Tree->lchild);
    InPostCreateBinaryTree(root-1,i+1,end,Tree->rchild);
}

static inline void PreOrderTraversal(BinaryTree Tree){
    if(Tree){
        cout << Tree->data << "->";
        PreOrderTraversal(Tree->lchild);
        PreOrderTraversal(Tree->rchild);
    }
}

static inline void InOrderTraversal(BinaryTree Tree){
    if(Tree){
        InOrderTraversal(Tree->lchild);
        cout << Tree->data << "->";
        InOrderTraversal(Tree->rchild);
    }
}

static inline void PostOrderTraversal(BinaryTree Tree){
    if(Tree){
        PostOrderTraversal(Tree->lchild);
        PostOrderTraversal(Tree->rchild);
        cout << Tree->data << "->";
    }
}

static inline void SequenceTraversal(BinaryTree Tree){
    SequenceQueue.clear();
    SequenceQueue.push_back(Tree);
    while(SequenceQueue.size()){
        Tree = SequenceQueue[0];
        cout << Tree->data << "->";
        SequenceQueue.pop_front();
        if(Tree->lchild)SequenceQueue.push_back(Tree->lchild);
        if(Tree->rchild)SequenceQueue.push_back(Tree->rchild);
    }
}

static inline void Traversal(BinaryTree Tree){
    PreOrderTraversal(Tree);
    cout << endl;
    InOrderTraversal(Tree);
    cout << endl;
    PostOrderTraversal(Tree);
    cout << endl;
    SequenceTraversal(Tree);
    cout << endl;
}

static inline int CountNode(BinaryTree Tree){
    if(Tree){
        return CountNode(Tree->lchild) + CountNode(Tree->rchild) + 1;
    }
    else return 0;
}

static inline int CountLeafNode(BinaryTree Tree){
    if(!Tree){
        return 0;
    }
    else if(!Tree->lchild && !Tree->rchild){
        return 1;
    }
    else return CountLeafNode(Tree->lchild) + CountLeafNode(Tree->rchild);
}

static inline int Depth(BinaryTree Tree){
    if(Tree){
        int l = Depth(Tree->lchild);
        int r = Depth(Tree->rchild);
        return l > r ? l+1 : r+1;
    }
    else return 0;
}

static inline void Copy(BinaryTree &Tree,BinaryTree &NewTree){
    if(Tree){
        NewTree = new BinaryNode;
        NewTree->data = Tree->data;
        Copy(Tree->lchild,NewTree->lchild);
        Copy(Tree->rchild,NewTree->rchild);
    }
    else{
        NewTree = NULL;
        return;
    }
}

static inline bool BinaryTreeEmpty(BinaryTree Tree){
    if(Tree){
        return true;
    }
    else return false;
}

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值