几个学校作业

来源:互联网 发布:ios开发程序员招聘 编辑:程序博客网 时间:2024/04/28 23:25

块链的移位

#define CHUNK_SIZE 4/* 可由用户定义的块大小 */  

/* 未存储的空间用'#'字符代替 */

typedef struct Chunk

{

    char ch[CHUNK_SIZE];

    structChunk *next;

}Chunk;

Chunk* insert(Chunk* Shead,Chunk* Thead,char c)

{

    Chunk* cur=Shead;

    int flag=0;

    while(cur)

    {

        for(int i=0;i<=CHUNK_SIZE-1;i++)

        {

           if(c==cur->ch[i])

           {

               flag=i;

               break;

           }

        }

        cur=cur->next;

    }

    Chunk* newone=NULL;

    if(flag<CHUNK_SIZE-1)

    {

        newone=newChunk;

        while(flag<=CHUNK_SIZE-1)

        {

            flag++;

            newone->ch[flag]=cur->ch[flag];

            cur->ch[flag]='#';

        }

    }

    

    Chunk* tail=Thead;

    while(tail->next)

        tail=tail->next;

    

    Chunk* k=NULL;

    if(cur->next)

        k=cur->next;

    

    cur->next=Thead;

    tail->next=newone;

    

    tail=tail->next;

    if(tail)

        tail->next=k;

    else

        tail=newone;

    return Shead;

}

15.    已知在二叉链表表示的二叉树中,root 为根结点, p↑q↑为二叉树中两个结点,试编写算法求距离它们最近的共同祖先。

16.   试给出算法求二叉链表表示的二叉树的直径(高度、最大层次数)以及长度等于直径的一条路经(从根到叶子的结点序列)。


17.    试给出算法将二叉树表示的表达式按中序遍历输出并加上相应的扩号。

class Solution15{

public:

    void helper(queue<TreeNode*>&fathers,TreeNode* p,TreeNode* root,vector<TreeNode*>&cur)

    {

        

        if(root->left&&root->left->val==p->val)//root已经在爸爸队列里,不等

        {

            for(int i=0;i<=cur.size()-1;i++)

                fathers.push(cur[i]);

            return;

        }

        if(root->right&&root->right->val==p->val)

        {

            for(int i=0;i<=cur.size()-1;i++)

                fathers.push(cur[i]);

            return;

        }

        if(root->left){

        cur.push_back(root->left);

        helper(fathers, p, root->left,cur);

        cur.pop_back();

        }

        if(root->right){

        cur.push_back(root->right);

        helper(fathers, p, root->right,cur);

        cur.pop_back();

        }

    }

    TreeNode* find(TreeNode* root,TreeNode* p,TreeNode* q)

    {

        queue<TreeNode*> pfathers;

        queue<TreeNode*> qfathers;

        vector<TreeNode*> curp;

        vector<TreeNode*> curq;

        curp.push_back(root);

        curq.push_back(root);

        if(p->val!=root->val)

            helper(pfathers, p, root,curp);

        if(q->val!=root->val)

            helper(qfathers, q, root,curq);

        TreeNode* res=NULL;

        while(pfathers.front()==qfathers.front())

        {

            res=pfathers.front();

            pfathers.pop();

            qfathers.pop();

            if(pfathers.empty()||qfathers.empty())

                break;

        }

        return res;

    }

};

class Solution16{

public:

    vector<vector<TreeNode*>> res;

    int height(TreeNode* root)

    {

        if(!root)return 0;

        int l=height(root->left);

        int r=height(root->right);

        return (l>r? l:r)+1;

    }

    vector<vector<TreeNode*>> findPath(vector<TreeNode*> &cur,TreeNode* root,int curHeight,int height)

    {

        if(curHeight>=height)

        {

            res.push_back(cur);

            cout<<"res:"<<endl;

            for(int i=0;i<=cur.size()-1;i++)

                if(cur[i])

                cout<<cur[i]->val;

        }

        else{

        cur.push_back(root->left);

        findPath(cur, root->left, curHeight+1, height);

        cur.pop_back();

        cur.push_back(root->right);

        findPath(cur, root->right, curHeight+1, height);

        cur.pop_back();

        }

        returnres;

    }

};

class solution17{

public:

    string calculate(TreeNode* root)

    {

        if(!root)return "";

        if(root->val>='a'&&root->val<='z')

        {

            string res;

            res+=root->val;

            return res;

        }

        string res;

        if(root->val=='*'||root->val=='/')

        {

            if(root->left&&(root->left->val=='+'||root->left->val=='-'))

            {

                res+='(';

                res+=calculate(root->left);

                res+=')';

            }

            else

                res+=calculate(root->left);

            res+=(root->val);

            if(root->right&&(root->right->val=='+'||root->right->val=='-'))

            {

                res+='(';

                res+=calculate(root->right);

                res+=')';

            }

            else

                res+=calculate(root->right);

        }

        else

            res+= calculate(root->left)+(root->val)+calculate(root->right);

        return res;

    }

};

//测试17

int main()

{

    TreeNode* root=new TreeNode('*');

    cout<<root->val<<endl;

    root->left=new TreeNode('+');

    root->right=new TreeNode('+');

    root->left->left=new TreeNode('a');

    root->left->right=new TreeNode('/');

    root->right->left=new TreeNode('e');

    root->right->right=new TreeNode('f');

    root->left->right->left=new TreeNode('b');

    root->left->right->right=new TreeNode('-');

    root->left->right->right->left=new TreeNode('e');

    root->left->right->right->right=new TreeNode('m');

    solution17 s;

    cout<<s.calculate(root);

}