二叉树--模板

一、 基础操作

#include<bits/stdc++.h>
using namespace std;

const int N = 100;
const int MAXN = 0x3f3f3f3f;
typedef long long ll;
char s[N];
int n;
int tot = 0;

struct node{
    char data;
    node *l, *r;
};
// 建树
node *creat(node *root){
    if(s[tot] == ','){
        root = NULL;
        tot++;
    }
    else {
        root = new node;
        root->data = s[tot++];
        root->l = creat(root->l);
        root->r = creat(root->r);
    }
    return root;
}
// 前序遍历
void preorder(node *root){
    if(root){
        cout << root->data;
        preorder(root->l);
        preorder(root->r);
    }
}
// 中序遍历
void midorder(node *root){
    if(root){
        midorder(root->l);
        cout << root->data;
        midorder(root->r);
    }
}
// 后序排列
void postorder(node *root){
    if(root){
        postorder(root->l);
        postorder(root->r);
        cout << root->data;
    }
}
// 层序遍历一:
void levelorder(node *root){
    if(!root) return;// 如果结点空的话
    node *tree[111], *p;
    tree[1] = root;
    int l = 1, r = 1;
    while(l <= r){
        p = tree[l++];
        if(p)  cout << p->data;
        if(p->l)  tree[++r] = p->l;
        if(p->r)  tree[++r] = p->r;
    }
}
// 层序遍历二:广搜思想(bfs)
void levelorder(node *root){
    if(root == NULL) return;
    queue<node*>q;
    q.push(root);
    int f = 0;
    while(q.size()){
        node *p = q.front(); q.pop();
        if(f) cout << " ";
        f = 1;
        cout << p->data;
        if(p->l){
            q.push(p->l);
        }
        if(p->r){
            q.push(p->r);
        }
    }
}
// 求树的高度
int height(node *root){
    int ans = 0;
    int lh, rh;
    if(!root) ans = 0;// 若结点为空
    else{
        lh = height(root->l);
        rh = height(root->r);
        ans = max(lh, rh) + 1;// 加上一个根结点
    }
    return ans;
}
// 求叶子结点的数量
int leaves(node *root){
    int ans = 0;
    if(root){
        if(root->l == NULL && root->r == NULL)
            ans++;// 叶子结点的求法
        else{// 不停地向左、向右递归
            if(root->l != NULL)
                ans += leaves(root->l);
            if(root->r != NULL)
                ans += leaves(root->r);
        }
    }
    return ans;
}
int main()
{
    while(cin >> s){
        tot = 0;
        node *root = new node;
        root = creat(root);
        midorder(root);
        cout << endl;
        postorder(root);
        cout << endl;
    }
    return 0;
}

二、各种创建二叉树的方法

**前序定树根,中序分左右

1、根据前序遍历和中序遍历

// 利用前、中序遍历建树
node *creat(int len, char pre[], char mid[]){
    if(len <= 0) return NULL;
    node *p;
    p = new node;
    p->data = pre[0];
    int i;
    for(i = 0; i < len; i++){
        if(mid[i] == pre[0]){
            break;
        }
    }
    p->l = creat(i, pre+1, mid);
    p->r = creat(len-i-1, pre+i+1, mid+i+1);
    return p;
}

// 弄清楚传入的参数与i的关系!! 

// 建议写下来两个字符串,标注上i在哪个位置会清楚一些

2、根据中序遍历和后序遍历

node *creat(int len, char mid[], char post[]){
    if(len <= 0) return NULL;
    node *root = new node;
    int i;
    for(i = 0; i < len; i++){
        if(mid[i] == post[len-1]){
            break;
        }
    }
    root->data = post[len-1];
    root->l = creat(i, mid, post);
    root->r = creat(len-i-1, mid+i+1, post+i);
    return root;
}

**注意事项同上!!

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值