二叉树/树笔记

PART1:建树

   1.直接一个数组建树,子节点为当前下标*2以及当前下标*2+1,适用于完全二叉树

   2.struct 法(数组大小取决于节点个数

struct node{
    int fa,l,r,depth;
}

3.链表法(待补充)

struct node {
    bool have_v;
    int v; 
    node *l,*r;
    node():have_v(false),l(NULL),r(NULL){}//结构体初始化 
}*root;

PART2:树的遍历(这里用struct法演示)

1.对完全二叉树的三种递归遍历

#pragma GCC optimize(2)
#pragma GCC optimize(1)
#include<bits/stdc++.h> 
typedef long long ll;
typedef unsigned long long ull;
const ull base=131;
#define MAX 100009
#define PI 3.141592653589793
using namespace std;
int n;
inline void preorder(int i){
    printf("%d ",i);
    if(i+i<=n)
      preorder(2*i);
    if(i+i+1<=n)
      preorder(2*i+1);
}

inline void inorder(int i){
    if(i+i<=n)
      inorder(i+i);
    printf("%d ",i);
    if(i+i+1<=n)
      inorder(i+i+1);
}

inline void postorder(int i){
    if(i+i<=n)
      postorder(2*i);
    if(2*i+1<=n)
      postorder(i+i+1);
    printf("%d ",i);
} 
int main()
{
    scanf("%d",&n);
    preorder(1);cout<<"\n";
    inorder(1);cout<<"\n";
    postorder(1);cout<<"\n";
 } 

2.对非完全二叉树的遍历

#pragma GCC optimize(2)
#pragma GCC optimize(1)
#include<bits/stdc++.h> 
typedef long long ll;
typedef unsigned long long ull;
const ull base=131;
#define MAX 100009
#define PI 3.141592653589793
using namespace std;
int n;
struct node{
    int fa,l,r;
}tree[MAX];
inline void preorder(int i){
    printf("%d ",i);
    if(tree[i].l)
      preorder(tree[i].l);
    if(tree[i].r)
      preorder(tree[i].r);
}

inline void inorder(int i){
    if(tree[i].l)
      inorder(tree[i].l);
    printf("%d ",i);
    if(tree[i].r)
      inorder(tree[i].r);
}

inline void postorder(int i){
    if(tree[i].l)
      postorder(tree[i].l);
    if(tree[i].r)
      postorder(tree[i].r);
    printf("%d ",i);
} 
int main()
{
    cin>>n;
    for(int i =1;i<=n;i++)
    {
        int x,y;cin>>x>>y;
        if(x){
            tree[i].l=x;
            tree[x].fa=i;
        }
        if(y){
            tree[i].r=y;
            tree[y].fa=i;
        }
    }
    preorder(1);cout<<"\n";
    inorder(1);cout<<"\n";
    postorder(1);cout<<"\n";
}

3.dfs和bfs找节点公共祖先

链接二叉树的最近公共祖先 - 题目 - Daimayuan Online Judge

dfs

#pragma GCC optimize(2)
#pragma GCC optimize(1)
#include<bits/stdc++.h> 
typedef long long ll;
typedef unsigned long long ull;
const ull base=131;
#define MAX 2009
#define PI 3.141592653589793
using namespace std;
struct node{
    int fa,l,r;
}tree[MAX]; 
int n,a[MAX],b[MAX];
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++){
        int x,y;cin>>x>>y;
        if(x){
            tree[i].l=x;tree[x].fa=i;
        }
        if(y){
            tree[i].r=y;tree[y].fa=i;
        }
    }
    int u,v;cin>>u>>v;
    int l1=0;
    while(u!=1)
    {
        a[++l1]=u,u=tree[u].fa;
    }
    a[++l1]=1;
    int l2=0;
    while(v!=1)
    {
        b[++l2]=v,v=tree[v].fa;
    }
    b[++l2]=1;
    int x=0;
    for(int i=l1,j=l2;i&&j;--i,--j){
        if(a[i]==b[j])  x=a[i];
        else break;
    }
    cout<<x<<endl;
}

bfs赋值深度版本

#pragma GCC optimize(2)
#pragma GCC optimize(1)
#include<bits/stdc++.h> 
typedef long long ll;
typedef unsigned long long ull;
const ull base=131;
#define MAX 2009
#define PI 3.141592653589793
using namespace std;
struct node{
    int fa,l,r,depth;
}tree[MAX]; 
int n,a[MAX];
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++){
        int x,y;cin>>x>>y;
        if(x){
            tree[i].l=x;tree[x].fa=i;
        }
        if(y){
            tree[i].r=y;tree[y].fa=i;
        }
    }
    int front=1,rear=1;
    a[1]=1,tree[1].depth=1;
    while(front<=rear){
        int p=a[front];front++;
        if(tree[p].l)
          a[++rear]=tree[p].l,tree[tree[p].l].depth=tree[p].depth+1;
        if(tree[p].r)
          a[++rear]=tree[p].r,tree[tree[p].r].depth=tree[p].depth+1;
    }
    int u,v;cin>>u>>v;
    if(tree[u].depth<tree[v].depth)
    swap(u,v);
    int cha=tree[u].depth-tree[v].depth;
    for(int i=1;i<=cha;i++)
    u=tree[u].fa;
    while(u!=v)
      u=tree[u].fa,v=tree[v].fa;
    cout<<u<<endl;
}
 

UVA - 122 Trees on the level

Trees on the level - UVA 122 - Virtual Judge (csgrandeur.cn)

指针建树加bfs层次遍历

#pragma GCC optimize(2)
#pragma GCC optimize(1)
#include<bits/stdc++.h> 
typedef long long ll;
typedef unsigned long long ull;
const ull base=131;
#define MAX 277
#define PI 3.141592653589793
using namespace std;
struct node {
    bool have_v;
    int v; 
    node *l,*r;
    node():have_v(false),l(NULL),r(NULL){}//结构体初始化 
}*root;
bool failed;
node* newnode(){ return new node();}
void addnode(int v,char* s){
    int n=strlen(s);
    node* u=root;
    for(int i=0;i<n;i++)
    {
        if(s[i]=='L'){
        if(u->l==NULL) u->l=newnode();u=u->l;}
        else if(s[i]=='R'){
        if(u->r==NULL) u->r=newnode();u=u->r;}
    }
    if(u->have_v) failed=true;
    u->v=v;
    u->have_v=true;
}
inline void remove_tree(node* u){//释放节点 
    if(u==NULL) return;
    remove_tree(u->l);
    remove_tree(u->r);
    delete u; 
}
bool read_input()
{
    char s[400];
    failed=false;
    int v;
    root=newnode();//
    for(;;)
    {
        if(scanf("%s", s) != 1) {//读入EOF 
            return false;
        }
        if(strcmp(s,"()") == 0) {//结束读入 
            break;
        }
        sscanf(&s[1],"%d",&v);//读入第一个数字 
        addnode(v,strchr(s,',')+1);//读入从第一个逗号右边读入的数据 
    }
    return true;
}
bool bfs(vector<int>& ans){
    queue<node*>q;
    ans.clear();
    q.push(root);
    while(!q.empty()){
        node* u=q.front();q.pop();
        if(!u->have_v) return false;
        ans.push_back(u->v);
        if(u->l!=NULL) q.push(u->l);
        if(u->r!=NULL) q.push(u->r);
    }
    return true;
}
int main(){
    vector<int> ans;
    while(read_input()){
        if(!bfs(ans)){
            failed=true;
        }
        if(failed) cout<<"not complete\n";
        else{
            for(int i=0;i<ans.size()-1;i++) cout<<ans[i]<<' ';
            cout<<ans[ans.size()-1]<<"\n";
        }
    }
}

 

posted on 2022-05-11 15:16  zesure  阅读(25)  评论(0编辑  收藏  举报

导航