数据结构学习记录

数据结构

数组 & 链表
相连性 | 指向性
数组可以迅速定位到数组中某一个节点的位置
链表则需要通过前一个元素指向下一个元素,需要前后依赖顺序查找,效率较低

实现链表

//    head => node1 => node2 => ... => nullclass Node {constructor(element) {this.element = element;this.next = null;}
}class LinkList {constructor() {this.length = 0;//    1.空链表特征 => 判断链表的长度this.head = null;}//    返回索引对应的元素getElementAt(position) {//    边缘检测if (position < 0 || position >= this.length) {return null;}let _current = this.head;for (let i = 0; i < position; i++) {_current = _current.next;}return _current;}//    查找元素所在位置indexOf(element) {let _current = this.head;for (let i = 0; i < this.length; i++) {if (_current === element) return i;_current = _current.next;}return -1;}//    添加节点 在尾部加入append(element) {_node = new Node(element);if (this.head === null) {this.head = _node;} else {this.getElementAt(this.length - 1);_current.next = _node;}this.length++;   }//    插入节点    在中间插入insert(position, element) {if (position < 0 || position > this.length) return false;let _node = new Node(element);if (position === 0) {_node.next = this.head;this.head = _node;} else {let _previos = this.getElementAt(position - 1);_node.next = _previos;_previos.next = _node;}this.length++;return true;}//    删除指定位置元素removeAt(position) {if (position < 0 || position > this.length) return false;if (position === 0) {this.head = this.head.next;} else {_previos = this.getElementAt(position - 1);let _current = _previos.next;_previos.next = current.next;            }this.length--;return true;}//    删除指定元素remove(element) {}
}

特点:先入后出(薯片桶)

队列

特点:先入先出(排队)

哈希

特点:快速匹配定位

遍历 前序遍历(中左右) 中序遍历(左中右) 后序遍历(左右中)
1.二叉树 or 多子树
2.结构定义
a.树是非线性结构
b.每个节点都有0个或多个后代

面试真题:

判断括号是否有效自闭合

算法流程:
1.确定需要什么样的数据结构,满足模型的数据 - 构造变量 & 常量
2.运行方式 简单条件执行 | 遍历 | 递归 - 算法主体结构
3.确定输入输出 - 确定流程

    //    '{}[]' 和 '[{}]' true,   //    '{{}[]' false
const stack = [];const brocketMap = {'{': '}','[': ']','(': ')'
};let str = '{}';function isClose(str) {for (let i = 0; i < str.length; i++) {const it = str[i];if (!stack.length) {stack.push(it);} else {const last = stack.at(-1);if (it === brocketMap[last]) {stack.pop();} else {stack.push(it);}}}if (!stack.length) return true;return false;
}isClose(str);

遍历二叉树

//    前序遍历
class Node {constructor(node) {this.left = node.left;this.right = node.right;this.value = node.value;}
}
//    前序遍历
const PreOrder = function(node) {if (node !== null) {console.log(node.value);PreOrder(node.left);PreOrder(node.right);}
}
//    中序遍历
const InOrder = function(node) {if (node !== null) {InOrder(node.left);console.log(node.value);InOrder(node.right);}
}
//    后序遍历
const PostOrder = function(node) {if (node !== null) {PostOrder(node.left);PostOrder(node.right);console.log(node.value);}
}

树结构深度遍历

const tree = {value: 1,children: [{value: 2,children: [{ value: 3 },{value: 4, children: [{ value: 5 }]}]},{ value: 5 },{value: 6, children: [{ value: 7 }]}]
}
//    优先遍历节点的子节点 => 兄弟节点
//    递归方式
function dfs(node) {console.log(node.value);if (node.children) {node.children.forEach(child => {dfs(child);})   }
}//    遍历 - 栈
function dfs() {const stack = [node];while (stack.length > 0) {const current = stack.pop();console.log(current.value);if (current.children) {const list = current.children.reverse();stack.push(...list);}}}

树结构广度优先遍历

const tree = {value: 1,children: [{value: 2,children: [{ value: 3 },{value: 4, children: [{ value: 5 }]}]},{ value: 5 },{value: 6, children: [{ value: 7 }]}]
}//    1. 递归
function bfs(nodes) {const list = []nodes.forEach(child => {console.log(child.value);if (child.children && child.children.length) {list.push(...child.children);}})if (list.length) {bfs(list);    }
}
bfs([tree]);//    1.递归 - 方法2
function bfs(node, queue = [node]) {if (!queue.length) return;const current = queue.shift();if (current.children) {queue.push(...current.children);}bfs(null, queue);
}
bfs(tree);//    2. 递归
function bfs(node) {const queue = [node];while(queue.length > 0) {const current = queue.shift();if (current.children) {queue.push(...current.children);}}
}

实现快速构造一个二叉树

1.若他的左子树非空,那么他的所有左子节点的值应该小于根节点的值
2.若他的右子树非空,那么他的所有右子节点的值应该大于根节点的值
3.他的左 / 右子树 各自又是一颗满足下面两个条件的二叉树

class Node {constructor(key) {this.key = keythis.left = null;this.right = null;}
}class BinaryTree {constructor() {//    根节点this.root = null;}//    新增节点insert(key) {const newNode = new Node(key);const insertNode = (node, newNode) => {if (newNode.key < node.key) {//    没有左节点的场景 => 成为左节点node.left ||= newNode;//    已有左节点的场景 => 递归改变参照物,与左节点进行对比判断插入位置inertNode(node.left, newNode);} else {//    没有左节点的场景 => 成为左节点node.right ||= newNode;//    已有左节点的场景 => 递归改变参照物,与左节点进行对比判断插入位置inertNode(node.right, newNode);}}this.root ||= newNode;//    有参照物后进去插入节点insertNode(this.root, newNode);}//    查找节点contains(node) {        const searchNode = (referNode, currentNode) => {if (currentNode.key === referNode.key) {return true;}if (currentNode.key > referNode.key) {return searchNode(referNode.right, currentNode);}if (currentNode.key < referNode.key) {return searchNode(referNode.left, currentNode);}return false;}return searchNode(this.root, node);}//    查找最大值findMax() {let max = null;//    深度优先遍历const dfs = node => {if (node === null) return;if (max === null || node.key > max) {max = node.key;}dfs(node.left);dfs(node.right);}dfs(this.root);return max;}//    查找最小值findMin() {let min = null;//    深度优先遍历const dfs = node => {if (node === null) return;if (min === null || node.key < min) {min = node.key;}dfs(node.left);dfs(node.right);}dfs(this.root);return min;}//    删除节点remove(node) {const removeNode = (referNode, current) => {if (referNode.key === current.key) {if (!node.left && !node.right) return null;if (!node.left) return node.right;if (!node.right) return node.left;}}return removeNode(this.root, node);}
}

平衡二叉树

在这里插入图片描述

class Node {constructor(key) {this.key = key;this.left = null;this.right = null;this.height = 1;}
}class AVL {constructor() {this.root = null;}// 插入节点insert(key, node = this.root) {if (node === null) {this.root = new Node(key);return;}if (key < node.key) {node.left = this.insert(key, node.left);} else if (key > node.key) {node.right = this.insert(key, node.right);} else {return;}// 平衡调整const balanceFactor = this.getBalanceFactor(node);if (balanceFactor > 1) {if (key < node.left.key) {node = this.rotateRight(node);} else {node.left = this.rotateLeft(node.left);node = this.rotateRight(node);}} else if (balanceFactor < -1) {if (key > node.right.key) {node = this.rotateLeft(node);} else {node.right = this.rotateRight(node.right);node = this.rotateLeft(node);}}this.updateHeight(node);return node;}// 获取节点平衡因子getBalanceFactor(node) {return this.getHeight(node.left) - this.getHeight(node.right);}rotateLeft(node) {      const newRoot = node.right;        node.right = newRoot.left;        newRoot.left = node;//    这里是为了校准更新的这两个节点的高度。只需要把他的左右节点的高度拿来 加1即可node.height = Math.max(this.getHeight(node.left),this.getHeight(node.right)) + 1;newRoot.height = Math.max(this.getHeight(newRoot.left),this.getHeight(newRoot.right)) + 1;return newRoot;}getHeight(node) {if (!node) return 0;return node.height;}
}

红黑树

参考写法

let RedBlackTree = (function() {let Colors = {RED: 0,BLACK: 1};class Node {constructor(key, color) {this.key = key;this.left = null;this.right = null;this.color = color;this.flipColor = function() {if(this.color === Colors.RED) {this.color = Colors.BLACK;} else {this.color = Colors.RED;}};}}class RedBlackTree {constructor() {this.root = null;}getRoot() {return this.root;}isRed(node) {if(!node) {return false;}return node.color === Colors.RED;}flipColors(node) {node.left.flipColor();node.right.flipColor();}rotateLeft(node) {var temp = node.right;if(temp !== null) {node.right = temp.left;temp.left = node;temp.color = node.color;node.color = Colors.RED;}return temp;}rotateRight(node) {var temp = node.left;if(temp !== null) {node.left = temp.right;temp.right = node;temp.color = node.color;node.color = Colors.RED;}return temp;}insertNode(node, element) {if(node === null) {return new Node(element, Colors.RED);}var newRoot = node;if(element < node.key) {node.left = this.insertNode(node.left, element);} else if(element > node.key) {node.right =this.insertNode(node.right, element);} else {node.key = element;}if(this.isRed(node.right) && !this.isRed(node.left)) {newRoot = this.rotateLeft(node);}if(this.isRed(node.left) && this.isRed(node.left.left)) {newRoot = this.rotateRight(node);}if(this.isRed(node.left) && this.isRed(node.right)) {this.flipColors(node);}return newRoot;}insert(element) {this.root = insertNode(this.root, element);this.root.color = Colors.BLACK;}}return RedBlackTree;
})()

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/825192.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

基于springboot+vue+Mysql的社区维修平台

开发语言&#xff1a;Java框架&#xff1a;springbootJDK版本&#xff1a;JDK1.8服务器&#xff1a;tomcat7数据库&#xff1a;mysql 5.7&#xff08;一定要5.7版本&#xff09;数据库工具&#xff1a;Navicat11开发软件&#xff1a;eclipse/myeclipse/ideaMaven包&#xff1a;…

软件产品许可证书 Licence 全流程研发(使用非对称加密技术,既安全又简单)

本篇博客对应的代码地址&#xff1a; Gitee 仓库地址&#xff1a;https://gitee.com/biandanLoveyou/licence 源代码百度网盘链接: https://pan.baidu.com/s/1_ZhdcENcrk2ZuL11hWDLTQ?pwdbmxi 提取码: bmxi 1、背景介绍 公司是做软件 SAAS 服务的&#xff0c;一般来说软件部…

分析SQL中的ON后面AND条件与WHERE后AND的区别及其应用场景

引言ON条件 - 连接条件 示例1 - ON条件用于连接表示例2 - ON中多个连接条件WHERE条件 - 数据过滤 示例3 - WHERE条件应用于连接结果区别与结合使用总结 引言 在SQL查询中&#xff0c;JOIN子句的ON条件和WHERE子句的AND条件都用于筛选数据&#xff0c;但它们的应用上下文和作用方…

RabbitMQ项目实战(一)

文章目录 RabbitMQ项目实战选择客户端基础实战 前情提要&#xff1a;我们了解了消息队列&#xff0c;RabbitMQ的入门&#xff0c;交换机&#xff0c;以及核心特性等知识&#xff0c;现在终于来到了激动人心的项目实战环节&#xff01;本小节主要介绍通过Spring Boot RabbitMQ S…

AppsFlyer 接入更新

SDK文档 最新的 SDK 已经支持了 UPM 的接入方式&#xff0c;直接输入链接就可以安装好&#xff0c; 只需要 resolve 通过然后就可以使用了。

盒子模型和圆角边框的属性

一、标准盒子模型 content padding border margin 定义 width 和 height 时&#xff0c;参数值不包括 border 和 padding 二、怪异盒子模型 能不能实现怪异盒子模型全看属性 box-sizing&#xff0c;box-sizing 属性的取值可以为 content-box 或 border-box。 content-b…

CentOS 7 中时间快了 8 小时

1.查看系统时间 1.1 timeZone显示时区 [adminlocalhost ~]$ timedatectlLocal time: Mon 2024-04-15 18:09:19 PDTUniversal time: Tue 2024-04-16 01:09:19 UTCRTC time: Tue 2024-04-16 01:09:19Time zone: America/Los_Angeles (PDT, -0700)NTP enabled: yes NTP synchro…

DRF分页接口

DRF分页接口 目录 DRF分页接口PageNumberPagination&#xff08;基本分页类&#xff09;LimitOffsetPagination&#xff08;偏移分类)CursorPagination&#xff08;游标分页&#xff09; 实战 PageNumberPagination&#xff08;基本分页类&#xff09; 特点 基于页码的分页方式…

CS信息系统建设和服务能力评估申报知识点

代科技快速发展&#xff0c;云计算、大数据、物联网、移动互联网等新一代信息技术发展迅速&#xff0c;信息化服务模式正经历着重大的变化。而CS信息系统建设和服务能力评估是反映信息系统建设和服务提供者的能力水平的一个证书&#xff0c;现在许多企业都在办理这个资质。那么…

2021年全国大学生电子设计竞赛D题——基于互联网的摄像测量系统(三)

13 测试方案和测量结果 测量一个边长为1米的正方形&#xff0c;取三个顶点分别作为O、A、B点。 在O点上方&#xff0c;用细线悬挂激光笔&#xff0c;激光笔常亮向下指示&#xff0c;静止时激光笔的光点和O点重合。 将两个D8M摄像头子卡插到DE10-Nano开发板上&#xff0c;放…

对象生命周期:Transient(瞬态)、Scoped(范围)、Singleton(单例)

在对象生命周期和依赖注入&#xff08;DI&#xff09;的上下文中&#xff0c;特别是在使用如Microsoft.Extensions.DependencyInjection&#xff08;.NET Core的DI容器&#xff09;等框架时&#xff0c;对象的生命周期通常被划分为几个不同的类型&#xff1a;Transient&#xf…

MySQL Linux环境安装部署

目录 1、mysql安装包下载 2、安装mysql服务 3、启动mysql服务 4、登录mysql服务 1、mysql安装包下载 1、查看centos的版本 cat /etc/redhat-release 2、进入官网地址下载对应系统版本的安装包 地址&#xff1a;MySQL :: Download MySQL Yum Repository 2、安装mysql服务 …

MySQL运维实战之ProxySQL(9.3)使用ProxySQL实现读写分离

作者&#xff1a;俊达 proxysql读写分离主要通过mysql_query_rules表中的规则来实现。 下面是具体的配置步骤&#xff1a; 1 hostgroup配置 insert into mysql_servers (hostgroup_id, hostname, port, max_replication_lag) values ( 100, 172.16.121.236, 3380, 3);inser…

恒峰智慧科技-森林消防便捷泵:轻松应对火灾危机!

在广袤无垠的森林中&#xff0c;绿色是生命的象征&#xff0c;是自然的馈赠。然而&#xff0c;当火魔无情地吞噬这片生命的绿洲时&#xff0c;我们需要一种快速、高效、可靠的消防工具来守护这片绿色。此时&#xff0c;森林消防便捷泵应运而生&#xff0c;成为了守护森林安全的…

Oracle数据库 :查询表结构脚本

查询脚本 &#xff1a; SELECT CASE WHEN a.column_id1 THEN a.TABLE_NAME ELSE END AS 表名, a.column_id AS 序号, a.column_name as 列名, REPLACE(comments, CHR(10), ) as 列说明, a.data_type || ( || a.data_length || ) as 数据类型, a.DATA_LENGTH AS 长度, a.DATA_…

idea中停止运行Vue

在里面敲入Ctrlc 输入y确定即可。

代码随想录算法训练营第44天| 518.零钱兑换II*、 377. 组合总和 Ⅳ*

518.零钱兑换II* 力扣题目链接 代码 #### 示例代码 class Solution { public:int change(int amount, vector<int>& coins) {vector<int> dp(amount 1, 0);dp[0] 1;for (int i 0; i < coins.size(); i) { // 遍历物品for (int j coins[i]; j < a…

Git最佳实践指南:从配置到高效开发的全面教程20240418

引言 在现代软件开发中&#xff0c;版本控制系统是不可或缺的工具&#xff0c;而Git是其中最受欢迎的一种。无论是个人项目还是团队合作&#xff0c;合理的Git使用策略可以显著提高开发效率和代码质量。本文详细介绍了Git的实践流程&#xff0c;包括项目设置、日常开发操作和高…

【微服务-Ribbon】什么是负载均衡?微服务中负载均衡有哪些策略呢?

前面几篇文章&#xff0c;我们了解了一下Nacos的单机部署、集群部署以及微服务接入Nacos的步骤。从本篇开始&#xff0c;我们来看一下微服务第二个通用组件-负载均衡&#xff08;Ribbon&#xff09;。 1、Ribbon负载均衡器 负载均衡顾名思义&#xff0c;是指通过软件或者硬件…

电能质量分析仪是什么

TH-6500电能质量分析仪是一种用于记录和分析现场电能质量参数的设备。它能够检测并记录电力系统的电压波动、频率偏差、谐波、三相不平衡等参数&#xff0c;帮助用户了解电力系统的运行状态&#xff0c;及时发现并解决潜在的电能质量问题。 该设备具备多种测量功能&#xff0c…