王道机试第三章总结

    xiaoxiao2021-04-13  46

    一、STL中栈的使用。

    包含的库:#include<stack>

    定义:stack<int> s;

    入栈:s.push(i);

    读栈顶:s.top();

    出栈: s.pop();

    判断是否非空:s.empty();

    运用场景:括号匹配、计算表达式求值。

    二、哈夫曼树

    存储表示方法:

    利用STL中的优先权队列

    priority_queue<int> Q; //递减排序队列

    priority_queue<int , vector<int> , greater<int>> Q; //递增排序队列

    包含的头文件:#include<queue>

    入队: Q.push(x);

    读出队顶元素: int x=Q.top();

    出队,弹出队顶元素:Q.pop(); //弹出后自动调整保持大小顺序

    队列元素个数:Q.size();

    三、二叉树存储表示方法:

    struct Node{

    Node * lchild;

    Node * rchild;

    int c; //结点信息

    }

    静态内存分配构造二叉树:

    struct Node{

    Node * lchild;

    Node * rchild;

    int c; //结点信息

    }

    Tree[50]; //静态申请50个结点空间

    int loc=0; //记录已经分配的结点数

    //结点分配函数

    Node* create(){

    Tree[loc].lchild=Tree[loc].rchild=null;

    return &Tree[loc++];

    }

    典型例子:

    二叉排序树的创建:

    Node *Insert(Node *T,int x){

    if(T == NULL){

    T=create();

    T->c=x;

    }else if(x<T->c){

    T->lchild=Insert(T->lchild,x);

    }else{

    T->rchild=Insert(T->rchild,x);

    }

    return T; //最后返回根结点

    }

    转载请注明原文地址: https://ju.6miu.com/read-669397.html

    最新回复(0)