码迷,mamicode.com
首页 > 其他好文 > 详细

数据结构-二叉树

时间:2014-12-01 00:39:17      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   sp   for   

二叉树先序后序中序的重建与遍历:

ZOJ1944 已知前序和中序求后序

不建树版

bubuko.com,布布扣
#include<iostream>  
#include<cstdio>  
#include<cstring>  
using namespace std;  
char pre[30],in[30];  
int num;  
void Postorder(int l,int r)  
{  
  
    if(l>r)return ;  
    if(l==r){  
        num++;  
        printf("%c",in[l]);  
        return;  
    }  
    int re=l;  
    for(int i=l;i<=r;i++)  
    {  
        if(in[i]==pre[num]){  
            re=i;  
            break;  
        }  
    }  
    num++;  
    Postorder(l,re-1);  
    Postorder(re+1,r);  
    printf("%c",in[re]);  
  
}  
  
int main()  
{  
    int n;  
    while(~scanf("%s%s",pre,in))  
    {  
       num=0;  
       Postorder(0,strlen(pre)-1);  
       printf("\n");  
    }  
    return 0;  
}  
View Code

建树版

bubuko.com,布布扣
#include<iostream>  
#include<cstdio>  
#include<cstring>  
using namespace std;  
char pre[30],in[30];  
struct node  
{  
    char value;  
    node *left,*right;  
};  
int num;  
node* Build(int l,int r)  
{  
   // printf("%d %d %d\n",num,l,r);  
    node* root=new node;  
    root->value=pre[num];  
    if(l>r)return NULL;  
    if(l==r){  
        num++;  
        root->left=NULL;  
        root->right=NULL;  
        return root;  
    }  
  
    int i;  
    int re=l;  
    for(i=l;i<=r;i++)  
    {  
        if(in[i]==pre[num]){  
            re=i;  
            break;  
        }  
    }  
    num++;  
    root->left=Build(l,re-1);  
    root->right=Build(re+1,r);  
    return root;  
}  
void Put_data(node *root)  
{  
    if(root->left!=NULL)Put_data(root->left);  
    if(root->right!=NULL)Put_data(root->right);  
    printf("%c",root->value);  
}  
int main()  
{  
    int n;  
    while(~scanf("%s%s",pre,in))  
    {  
        num=0;  
        node* root=Build(0,strlen(in)-1);  
  
        Put_data(root);  
        printf("\n");  
    }  
    return 0;  
}
View Code

Pat1086先序建树,后序输出

bubuko.com,布布扣
#include<iostream>  
#include<cstdio>  
#include<algorithm>  
using namespace std;  
struct Node{  
    int value;  
    Node *leftChild;  
    Node *rightChild;  
    Node(int v):value(v),leftChild(NULL),rightChild(NULL){}  
};  
char ch[10];  
int n,num;  
int flag;  
Node* Build(Node* root)  
{  
    if(num>=2*n)return NULL;  
    scanf("%s",ch);  
    if(ch[1]==u){  
        root=new Node(0);  
        scanf("%d",&root->value);  
        num++;  
        root->leftChild=Build(root->leftChild);  
        root->rightChild=Build(root->rightChild);  
    }  
    else {  
            root=NULL;  
            num++;  
    }  
    return root;  
}  
void PutTree(Node *root)  
{  
    if(root!=NULL){  
        PutTree(root->leftChild);  
        PutTree(root->rightChild);  
        if(!flag){  
            printf("%d",root->value);  
            flag=1;  
        }  
        else printf(" %d",root->value);  
    }  
}  
int main()  
{  
    while(~scanf("%d",&n))  
    {  
        Node *root=NULL;  
        num=0;  
        flag=0;  
        root=Build(root);  
        PutTree(root);  
        puts("");  
    }  
    return 0;  
}  
View Code

 

Pat1043二叉搜索树

bubuko.com,布布扣
#include <iostream>  
using namespace std;  
  
int flag;   //是否符合条件标志位,0符合,1不符合  
  
struct node //树中的点  
{  
    int value;  //
    node *left,*right; //左右子树  
};  
  
node* Build1(int *data,int p,int q) //数据,起始、终止位置[p,q];  
{  
    if(p>q)  
        return NULL;  
    if(flag==1) //不符合条件,直接跳出(应该放到最前)  
        return NULL;  
  
    node* root=new node;  
    root->value=data[p];  
  
    if(p==q)    //只有一个数  
    {  
        root->left=NULL;  
        root->right=NULL;  
        return root;  
    }  
    int i;  
    int r=p;    //第一个比data[p]大的数  
    for(i=p+1;i<=q;i++)  
    {  
        if(data[i]>=data[p])  
        {  
            r=i;  
            break;  
        }  
    }  
    if(r>p)  //找得到比data[p]大的data[r]  
    {  
        for(i=r+1;i<=q;i++)  
        {  
            if(data[i]<data[p])  
            {  
                flag=1;  
                return NULL;  
            }  
        }  
        root->left=Build1(data,p+1,r-1);  
        root->right=Build1(data,r,q);  
    }  
    else    //找不到  
    {  
        root->left=Build1(data,p+1,q);  
        root->right=NULL;  
    }  
    return root;  
}  
  
node* Build2(int *data,int p,int q) //镜像  
{  
    if(p>q)  
        return NULL;  
    if(flag==1)  
        return NULL;  
  
    node* root=new node;  
    root->value=data[p];  
  
    if(p==q)  
    {  
        root->left=NULL;  
        root->right=NULL;  
        return root;  
    }  
    int i;  
    int r=p;  
    for(i=p+1;i<=q;i++)  
    {  
        if(data[i]<data[p])  //注意镜像的定义  
        {  
            r=i;  
            break;  
        }  
    }  
    if(r>p)  
    {  
        for(i=r+1;i<=q;i++)  
        {  
            if(data[i]>=data[p])  
            {  
                flag=1;  
                return NULL;  
            }  
        }  
        root->left=Build2(data,p+1,r-1);  
        root->right=Build2(data,r,q);  
    }  
    else  
    {  
        root->left=Build2(data,p+1,q);  
        root->right=NULL;  
    }  
    return root;  
}  
  
int put_flag=0; //是否输出过信息标志位,用于跳过第一个空格  
  
void Put_data(node *root)  
{  
    if(root->left!=NULL)  
        Put_data(root->left);  
    if(root->right!=NULL)  
        Put_data(root->right);  
    if(put_flag==1)  
        cout<<" ";  
    else  
        put_flag=1;  
    cout<<root->value;  
}  
  
int main()  
{  
    int n,data[1001];   //数据  
    int i;  
    cin>>n;  
    for(i=0;i<n;i++)  
        cin>>data[i];  
    if(n==1)    //只有一个值,符合条件  
    {  
        cout<<"YES\n";  
        cout<<data[0];  
        return 0;  
    }  
    flag=0; //考虑正向  
    node *root=Build1(data,0,n-1);  
    if(flag==0)  
    {  
        cout<<"YES\n";  
        Put_data(root);  
        return 0;  
    }  
    flag=0; //考虑镜像  
    root=Build2(data,0,n-1);  
    if(flag==0)  
    {  
        cout<<"YES\n";  
        Put_data(root);  
    }  
    else    //都不符合  
        cout<<"NO";  
    return 0;  
}  
View Code

Pat 1066 平衡二叉树的旋转插入

http://blog.csdn.net/tiantangrenjian/article/details/12891091

bubuko.com,布布扣
#include<iostream>  
#include<cstdio>  
#include<algorithm>  
using namespace std;  
struct Node  
{  
    int value;  
    int height;  
    Node *leftChild;  
    Node *rightChild;  
    Node(int v):value(v),leftChild(NULL),rightChild(NULL),height(0){}  
};  
int getHeight(Node *node)  
{  
    if(node == NULL)return -1;  
    return node->height;  
}  
bool isBalance(Node* parent)  
{  
    return abs(getHeight(parent->leftChild)-getHeight(parent->rightChild))<2;  
}  
Node* rotate_LL(Node* parent)  
{  
    Node* child=parent->leftChild;  
    parent->leftChild=child->rightChild;  
    child->rightChild=parent;  
  
    parent->height = max(getHeight(parent->leftChild),getHeight(parent->rightChild))+1;  
    child->height = max(getHeight(child->leftChild),getHeight(child->rightChild))+1;  
    return child;  
}  
Node* rotate_RR(Node* parent)  
{  
    Node* child=parent->rightChild;  
    parent->rightChild=child->leftChild;  
    child->leftChild=parent;  
  
    parent->height = max(getHeight(parent->leftChild),getHeight(parent->rightChild))+1;  
    child->height = max(getHeight(child->leftChild),getHeight(child->rightChild))+1;  
    return child;  
}  
Node* rotate_LR(Node* parent)  
{  
    Node* child=parent->leftChild;  
    parent->leftChild=rotate_RR(child);  
    return rotate_LL(parent);  
}  
Node* rotate_RL(Node *parent)  
{  
    Node* child=parent->rightChild;  
    parent->rightChild=rotate_LL(child);  
    return rotate_RR(parent);  
}  
Node* InsertNode(Node* root,int newValue)  
{  
    if(root!=NULL)  
    {  
        if(newValue>root->value)  
        {  
            root->rightChild=InsertNode(root->rightChild,newValue);  
            if(!isBalance(root))  
            {  
                if(newValue>root->rightChild->value)  
                {  
                    root=rotate_RR(root);  
                }  
                else root=rotate_RL(root);  
            }  
        }  
        else  
        {  
            root->leftChild=InsertNode(root->leftChild,newValue);  
            if(!isBalance(root))  
            {  
                if(newValue>root->leftChild->value)  
                    root=rotate_LR(root);  
                else root=rotate_LL(root);  
            }  
        }  
  
    }  
    else{  
        root=new Node(newValue);  
    }  
    root->height=max(getHeight(root->leftChild),getHeight(root->rightChild))+1;  
    return root;  
}  
int main()  
{  
    int n;  
    while(~scanf("%d",&n))  
    {  
        int val;  
        Node *root=NULL;  
        for(int i=0;i<n;i++)  
        {  
            scanf("%d",&val);  
            root=InsertNode(root,val);  
        }  
        printf("%d\n",root->value);  
    }  
    return 0;  
}  
View Code

 

数据结构-二叉树

标签:style   blog   http   io   ar   color   os   sp   for   

原文地址:http://www.cnblogs.com/kylehz/p/binary_tree.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!