C++数据结构(第三四次实习)


本次实习主要关于树的操作,在这里简单记录一下,防止以后忘掉。

由于牛客网面试平台提交答案后无法查看,所以只能凭记忆简单描述一下题目要求

1.前,中,后序遍历输出

题目所用数据都为整型,且为完全二叉树所以选择通过数组实现
代码如下:

#include <iostream>

using namespace std;

class BinaryTree{
private:
    int *a;
    int maxSize;
public:
    BinaryTree(int sz){
        maxSize = sz;
        a = new int[maxSize+1];
    }
    ~BinaryTree(){delete a;}

    void Creat(){
        for(int i = 1;i <= maxSize;i++){
            cin >> a[i];
        }
    }

    void preOrder(int n){
        if(a[n] == 0 || n > maxSize){
            cout << "";
        }
        else{
            cout << a[n] << " " ;
            preOrder(2*n);
            preOrder(2*n+1);
        }
    }

    void inOrder(int n){
        if(a[n] == 0 || n > maxSize){
            cout << "";
        }
        else{
            inOrder(2*n);
            cout << a[n] << " ";
            inOrder(2*n+1);
        }
    }

    void postOrder(int n){
        if(a[n] == 0 || n > maxSize){
            cout << "";
        }
        else {
            postOrder(2*n);
            postOrder(2*n+1);
            cout << a[n] << " ";
        }
    }
};



int main()
{
    int m;
    cin >> m;
    BinaryTree *tree = new BinaryTree(m);
    tree->Creat();

    tree->preOrder(1);
    cout << endl;
    tree->inOrder(1);
    cout << endl;
    tree->postOrder(1);
    return 0;
}

2.由前序和中序遍历构造二叉树,并后续和层序输出

“递归狗都不用”,作为菜狗的我,毫不犹豫地选择了递归,希望等我下次看到笔记地时候,我已经用迭代把它重新解决了一遍。

这道题的思路为通过前序遍历确定根节点,然后在中序遍历中找到对应的根节点下标,下标之前则为左子树的中序遍历,之后则为右子树的中序遍历。

根据左子树和右子树的节点数,可以将前序遍历的左右子树划分出来,从而通过递归解决。

还是直接上代码,估计只有我自己能看懂,再次提醒一下自己养成写注释的习惯。

#include <iostream>
#include <queue>

using namespace std;

struct Node{
    int value;
    Node *left;
    Node *right;
    Node(int v = 0,Node *l = NULL,Node *r = NULL){
        value = v;
        left = l;
        right = r;
    }
};
class BinaryTree{
public:
    Node *root;
public:
    BinaryTree(){root = new Node();}
    ~BinaryTree(){}

    void postOrder(Node *n){
        if(n == NULL) return;
            postOrder(n->left);
            postOrder(n->right);
            cout << n->value << " ";
    }

    void levelOrder(Node *n){
        queue<Node*> q;
        q.push(n);
        while(!q.empty()){
            cout << q.front()->value << " ";
            if(q.front()->left != NULL) q.push(q.front()->left);
            if(q.front()->right != NULL) q.push(q.front()->right);
            q.pop();
        }
    }


};

 Node* Create(int *pre,int *in,int pre_left,int pre_right,int in_left,int in_right){
        if(pre_left > pre_right) return NULL;
        int pre_root = pre_left;
        int in_root;
        for(int i = in_left;i <= in_right;i++){
            if(in[i] == pre[pre_root]){
                in_root = i;
                break;
            }
        }
        int dis = in_root - in_left;
        Node *root = new Node(pre[pre_root],Create(pre,in,pre_left+1,pre_left+dis,in_left,in_root-1),Create(pre,in,pre_left+dis+1,pre_right,in_root+1,in_right));
        return root;
    }

int main()
{
    BinaryTree tree;
    int m;
    cin >> m;
    int *a = new int[m];
    int *b = new int [m];
    for(int i = 0;i < m;i++){
        cin >> a[i];
    }
    for(int i = 0;i < m;i++){
        cin >> b[i];
    }

    tree.root = Create(a,b,0,m-1,0,m-1);
    tree.levelOrder(tree.root);
    cout << endl;
    tree.postOrder(tree.root);
    return 0;
}

3.由中序和层序遍历构造二叉树,并前序和后续输出

这题在网上参考了其他大佬的代码,都是通过容器存放数据,但我的c艹是混过来的,所以打算用最原始的数组实现

结果现在看着这道题的代码自己都不想看了,只有一句我是傻逼,还是得把STL给补起来,不然以后每次写笔记都得来一句我是傻逼

主要思路是通过每次层序遍历的第一个元素都肯定为根节点,然后在中序遍历中找到对应根节点的下标,划分出左右子树,从而确定左右子树的层序遍历,返回根节点,这样就可以用递归解决了。

傻逼代码如下

#include <iostream>

using namespace std;

struct Node{
    int value;
    Node *left;
    Node *right;
    Node(int v = 0,Node *l = NULL,Node *r = NULL){
        value = v;
        left = l;
        right = r;
    }
};

class BinaryTree{
public:
    Node *root;
    BinaryTree(){root = new Node();}

    void preOrder(Node *n){
        if(n == NULL) return;
        cout << n->value << " ";
        preOrder(n->left);
        preOrder(n->right);
    }

    void postOrder(Node *n){
        if(n == NULL) return;
        postOrder(n->left);
        postOrder(n->right);
        cout << n->value << " ";
    }
//     void levelOrder(Node *n){
//        queue<Node*> q;
//        q.push(n);
//        while(!q.empty()){
//            cout << q.front()->value << " ";
//            if(q.front()->left != NULL) q.push(q.front()->left);
//            if(q.front()->right != NULL) q.push(q.front()->right);
//            q.pop();
//        }
//    }
};

Node *Create(int *level,int *in,int level_left,int level_right,int in_left,int in_right){
    if(level_left > level_right) return NULL;
    int level_root;
    int in_index;
    int k = 0;
    int next_level1,next_level2;
    level_root = level[level_left];
    for(int i = in_left;i <= in_right;i++){
        if(in[i] == level_root){
            in_index = i;
            break;
        }
    }
    int *left_level = new int[in_index-in_left];
    int *right_level = new int[in_right-in_index];
    for(int i = 1;i <= level_right;i++){
        for(int j = in_left;j < in_index;j++){
            if(level[i] == in[j]){
                left_level[k] = level[i];
                k++;
                break;
            }
        }
    }
    next_level1 = k-1;
    k = 0;
    for(int i = 1;i <= level_right;i++){
        for(int j = in_index+1;j <= in_right;j++){
            if(level[i] == in[j]){
                right_level[k] = level[i];
                k++;
                break;
            }
        }

    }
    next_level2 = k-1;
    Node *root = new Node(level_root,Create(left_level,in,0,next_level1,in_left,in_index-1),Create(right_level,in,0,next_level2,in_index+1,in_right));

    return root;

}

int main()
{
    BinaryTree tree;
//    int m = 6;
//    int a[m] = {1,2,3,5,6,7};
//    int b[m] = {2,5,1,6,3,7};
    int m;
    cin >> m;
    int *a = new int[m];
    int *b = new int[m];
    for(int i = 0;i < m;i++){
        cin >> a[i];
    }
    for(int i = 0;i < m;i++){
        cin >> b[i];
    }
    tree.root = Create(a,b,0,m-1,0,m-1);
    //cout << tree.root->value;
//    tree.levelOrder(tree.root);
//    cout << endl;
    tree.preOrder(tree.root);
    cout << endl;
    tree.postOrder(tree.root);
    return 0;
}

4.括号匹配多叉树并层序遍历输出

这道题还行,主要通过栈实现括号匹配之后,将括号内的子节点都接在此括号前的节点上,如此循环,直到将字符串读取完毕,然后返回根节点,最后通过队列实现层序遍历输出。

上代码

#include <iostream>
#include <queue>
#include <string>
#include <stack>

using namespace std;

struct Node{
    char value;
    Node *children[10];
    int chNum;
    Node(char v = '0'){
        value = v;
    }
};

class Tree{
public:
    Node *root;
    Tree(){
        root = new Node();
    }
    ~Tree(){}

    void levelSort(Node *n){
        queue<Node*> q;
        q.push(n);
        while(!q.empty()){
            Node *tem = q.front();
            q.pop();
            cout << tem->value;
            for(int i = 0;i < tem->chNum;i++){
                if(tem->children[i] != NULL) q.push(tem->children[i]);
            }
        }
    }
};

Node *Create(string str){
    stack<Node*> s1;
    stack<Node*> s2;
    for(int i = 0;i < str.length()-1;i++){
        if(str[i] == ')'){
            int num = 0;
            while(s1.top()->value != '('){
                num++;
                s2.push(s1.top());
                s1.pop();

            }
            s1.pop();
            s1.top()->chNum = num;

            for(int j = 0;j < num;j++){
                s1.top()->children[j] = s2.top();
                s2.pop();
            }
        }
        else{
            if(str[i] != ','){
                Node *in = new Node(str[i]);
                s1.push(in);
            }
        }
    }
    return s1.top();
}

int main()
{
    string str;
    Tree tree;
    cin >> str;
    tree.root = Create(str);
    tree.levelSort(tree.root);
    return 0;
}

5. 哈夫曼树编码

本题要求输入大写英文字母,并根据字母出现频率,确定权重,最后根据权重输出字母、权重、以及编码。

主要思路
getWeight()函数中通过大小为26的数组确定每个字母的频率,并将数据不为0 的丢入优先级队列priority_queue<Node*,vector<Node*>,com> q;

通过Create()函数构造二叉树,将权重最小的两个节点作为一个新节点的左右子节点,并出队,然后将新节点入队,做n-1此循环,

最后通过Print()函数输出,其中通过父节点将每个节点所存的编码放入栈,存到根节点后,再将栈中编码遍历输出。

上代码

#include <iostream>
#include <queue>
#include <stack>

using namespace std;

struct Node{
    int weight;
    char value;
    int code;
    Node *left,*right,*farther;
    Node(int weight1 = 0,char value1 = '0',Node *left1 = NULL,Node *right1 = NULL){
        weight = weight1;
        value = value1;
        left = left1;
        right = right1;
    }

};
struct com{
    bool operator() (Node *n1,Node *n2){
        return n1->weight > n2->weight;
    }
};

class Huff{
private:
    Node *root;
    priority_queue<Node*,vector<Node*>,com> q;
public:
    Huff(){root = new Node();}

    void getWeight(int n){
        Node *temp;
        int a[26] = {0};
        char s;
        for(int i = 0;i < n;i++){
            cin >> s;
            a[s-'A']++;
        }
        for(int i = 0;i < 26;i++){
            if(a[i] != 0){
                temp = new Node(a[i],char(i+'A'));
                q.push(temp);
            }
        }
    }

    void Create(){
        Node *n1;
        Node *n2;
        Node *temp;
        int Size = q.size();
        for(int i = 0;i < Size-1;i++){
            n1 = q.top();
            q.pop();
            n2 = q.top();
            q.pop();
            if(n1->weight < n2->weight){
                temp = new Node(n1->weight+n2->weight,'0',n2,n1);
                n1->farther = temp;
                n2->farther = temp;
                n1->code = 0;
                n2->code = 1;
            }
            else{
                temp = new Node(n1->weight+n2->weight,'0',n1,n2);
                n2->farther = temp;
                n1->farther = temp;
                n2->code = 0;
                n1->code = 1;
            }
            q.push(temp);
        }
        root = q.top();
    }

    void Print(){
        queue<Node*> que;
        stack<int> s;
        Node *current;
        que.push(root);
        while(!que.empty()){
            if(que.front()->left==NULL && que.front()->right==NULL){
                cout << que.front()->value << " " << que.front()->weight << " ";
                current = que.front();
                while(current != root){
                    s.push(current->code);
                    current = current->farther;
                }
                while(!s.empty()){
                    cout << s.top();
                    s.pop();
                }
                cout << endl;
            }
            if(que.front()->left != NULL) que.push(que.front()->left);
            if(que.front()->right != NULL) que.push(que.front()->right);
            que.pop();
        }
    }
};


int main()
{
    Huff tree;
    int m;
    cin >> m;
    tree.getWeight(m);
    tree.Create();
    tree.Print();
    return 0;
}

2021.11.20

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值