ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

第十章 二叉树、优先队列、散列表

2022-03-21 21:02:52  阅读:190  来源: 互联网

标签:preorder treeNode 队列 inorder 第十章 int 二叉树 root postorder


1.二叉树

机试中很少直接考察二叉树的结构定义,而是考察二叉树的前中后三种遍历方法比较多

(1)二叉树的结构定义

(2)以下四种组合能够唯一确定一棵树

(3)模板(根据先序和中序建立一颗二叉树)

刚开始写没有注意到substr是在string.h库里面的

#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;

typedef struct treeNode{    //定义树的结点结构
    char c;
    treeNode *leftchild,*rightchild;
    treeNode(){};
    treeNode(char a): c(a){};   //带参构造函数
};

treeNode* buildTree(string preorder, string inorder){   //根据先序遍历preorder和中序遍历inorder,递归的建立一颗二叉树
    if(preorder == "" && inorder == ""){
        return nullptr;
    }
    treeNode *root = new treeNode(preorder[0]);
    int pos = inorder.find(preorder[0]);
    int len1 = preorder.size();
    int len2 = inorder.size();
    root->leftchild = buildTree(preorder.substr(1, pos), inorder.substr(0, pos));
    root->rightchild = buildTree(preorder.substr(pos + 1,len1), inorder.substr(pos + 1,len2));
    return root;
}

void postorder(treeNode *root){ //postorder为后序遍历
    if(root == nullptr){
        return;
    }
    postorder(root->leftchild);
    postorder(root->rightchild);
    printf("%c",root->c);
}

int main(){
    string pre,in;
    while(cin >> pre >> in){
        treeNode * root = buildTree(pre, in);
        postorder(root);
        printf("\n"); //每次输出完还要进行换行,输出下一棵二叉树的后序遍历
    }

    return 0;
}

2.二叉排序树

要注意二叉排序树的特性,任意一颗子树的左子树都小于根结点的值,右子树都大于根结点的值。

模板(根据二叉树的模板做出调整):

#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;

typedef struct treeNode{    //定义树的结点结构
    int data;
    treeNode *leftchild,*rightchild;
    treeNode(){};
    treeNode(int a): data(a){};   //带参构造函数
};

treeNode* buildorderTree(int x, treeNode *root){   //递归的建立一颗二叉排序树
    if(root == nullptr){
        root = new treeNode(x);
    }else if(x > root->data){
        root->rightchild = buildorderTree(x, root->rightchild);
    }else if(x < root->data){
        root->leftchild = buildorderTree(x, root->leftchild);
    }
    return root;
}

void preorder(treeNode *root){ //preorder为先序遍历
    if(root == nullptr){
        return;
    }
    printf("%d ",root->data);
    preorder(root->leftchild);
    preorder(root->rightchild);
}

void inorder(treeNode *root){ //inorder为中序遍历
    if(root == nullptr){
        return;
    }
    inorder(root->leftchild);
    printf("%d ",root->data);
    inorder(root->rightchild);
}

void postorder(treeNode *root){ //postorder为后序遍历
    if(root == nullptr){
        return;
    }
    postorder(root->leftchild);
    postorder(root->rightchild);
    printf("%d ",root->data);
}

int main(){
    int n,t;//n为待排序数的个数,t为临时保存每个数
    while(scanf("%d", &n) != EOF){
        treeNode *root = nullptr;
        for(int i = 0; i < n; i++){
            scanf("%d", &t);
            root  = buildorderTree(t, root);
        }
        preorder(root);
        printf("\n");
        inorder(root);
        printf("\n");
        postorder(root);
        printf("\n");
    }

    return 0;
}

3.优先队列

顾名思义,就是优先级最高的,最先出队,那么栈和队列其实都是特殊的优先队列,栈的栈顶的优先级最高,队列的队首优先级最高。

优先队列其实就是大根堆(或小根堆) 

(1)基本操作

 (2)优先队列的应用 

(3)大根堆还是小根堆

        STL priority_queue 是默认大根堆,比较符号默认是<,如priority_queue<int>

        而定义小根堆则是priority_queue<int,vector<int>,greater<int>>

(4)例题

        (1)复数集合

        (2)哈夫曼树

4.散列表

例题:查找学生信息

 

标签:preorder,treeNode,队列,inorder,第十章,int,二叉树,root,postorder
来源: https://blog.csdn.net/weixin_44223786/article/details/123621592

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有