这是第六周的练习题,最近加班比较多,上周主要完成一篇 GraphQL入门教程 ,有兴趣的小伙伴可以看下哈。

下面是之前分享的链接:

欢迎关注我的 个人主页 && 个人博客 && 个人知识库 && 微信公众号“前端自习课”

本周练习内容:数据结构与算法 —— Tree

这些都是数据结构与算法,一部分方法是团队其他成员实现的,一部分我自己做的,有什么其他实现方法或错误,欢迎各位大佬指点,感谢。

一、什么是树?

1.树有什么特点,什么是二叉树和二叉搜索树(BST: Binary Search Tree)?

2.生活中常见的例子有哪些?


解析:

  1. 树有什么特点,什么是二叉树和二叉搜索树:
  • 是一种非线性的数据结构,以分层方式存储数据,用来表示有层级关系的数据

  • 每棵树至多只有一个根结点根结点会有很多子节点,每个子节点只有一个父结点

  • 父结点子节点是相对的。

  1. 生活中的例子:

    如:家谱、公司组织架构图。

二、请实现二叉搜索树(BST),并实现以下方法:

  • insert(key):向树中插入一个新的键;
  • search(key):树中查找一个键,如果节点存在返回true,不存在返回false;
  • min():返回树中最小的值/键;
  • max():返回树中最大的值/键;
  • remove(key):移除某个键;

提示:所谓的键对应于之前章节所学的节点(Node)

class Node {
constructor(key){
this.key = key
this.left = null
this.right = null
}
}
class BST {
constructor(){
this.root = null
}
/**
* 插入一个节点
* @param {*} node 插入的位置节点
* @param {*} newNode 插入的节点
*/
insertNode (node, newNode){
if(newNode.key < node.key){
if(node.left === null && node.right === null){
node.left = newNode
}else if(node.left !== null && node.right === null){
node.right = newNode
}else{
this.insertNode(node.left, newNode)
}
}else{
if(node.left === null && node.right === null){
node.left = newNode
}else if(node.left !== null && node.right === null){
node.right = newNode
}else{
this.insertNode(node.right, newNode)
}
}
}
/**
* 插入操作
* @param {*} key
*/
insert (key){
let newNode = new Node(key)
if(this.root === null){
this.root = newNode
}else{
this.insertNode(this.root, newNode)
}
}
searchNode (node, key){
if(node === null) return false
if(key < node.key){
return this.searchNode(node.left, key)
}else if(key > node.key){
return this.searchNode(node.right, key)
}else{
return true
}
}
/**
* 搜索操作
* @param {*} key
*/
search (key){
return this.searchNode(this.root, key)
}
/**
* 最小值的节点
*/
min (){
let node = this.root
if(node === null) return null
while(node && node.left !== null){
node = node.left
}
return node.key
}
/**
* 最大值的节点
*/
max (){
let node = this.root
if(node === null) return null
while(node && node.right !== null){
node = node.right
}
return node.key
}
/**
* 找到最小节点
* @param {*} node
*/
findMinNode (node){
if(node === null) return null
while(node && node.left !== null){
node = node.left
}
return node
}
/**
* 删除一个节点
* @param {*} node
* @param {*} key
*/
removeNode (node, key){
if(node === null) return null
if(key < node.key){
node.left = this.removeNode(node.left, key)
return node
}else if(key > node.key){
node.right = this.removeNode(node.right, key)
return node
}else{
// 1.叶节点
if(node.left === null && node.right === null){
node = null
return node
}
// 2.只有一个子节点
if(node.left === null){
node = node.right
return node
}else if(node.right === null){
node = node.left
}
// 3.有两个子节点
let curNode = this.findMinNode(node.right)
node.key = curNode.key
node.right = this.removeNode(node.right, curNode.key)
return node
}
}
/**
* 删除一个节点
* @param {*} key
*/
remove (key){
if(this.root === null) return null
this.root = this.removeNode(this.root, key)
}
}

三、基于题二实现二叉搜索树扩展以下方法:

  • preOrderTraverse(): 通过先序遍历方式遍历所有节点;
  • inOrderTraverse(): 通过中序遍历方式遍历所有节点;
  • postOrderTraverse(): 通过后序遍历方式遍历所有节点;

提示:

  • 先序:先访问根节点,然后以同样方式访问左子树和右子树;(根==>左==>右)

输出 =》 11 7 5 3 6 9 8 10 15 13 12 14 20 18 25

  • 中序:先访问左子树,再访问根节点,最后访问右字数;以升序访问所有节点;(左==>根==>右)

输出 =》 3 5 6 7 8 9 10 11 12 13 14 15 18 20 25

  • 后序:先访问叶子节点,从左子树到右子树,再到根节点。(左==>右==>根)

输出 =》 3 6 5 8 10 9 7 12 14 13 18 25 20 15 11


解析:

// 1. 先序
BST.prototype.preOrderTraverseNode = function(node, callback){
if(node !== null){
callback(node.key)
this.preOrderTraverseNode(node.left, callback)
this.preOrderTraverseNode(node.right, callback)
}
}
BST.prototype.preOrderTraverse = function(callback){
this.preOrderTraverseNode(this.root, callback)
} // 2. 中序
BST.prototype.inOrderTraverseNode = function(node, callback){
if(node !== null){
this.inOrderTraverseNode(node.left, callback)
callback(node.key)
this.inOrderTraverseNode(node.right, callback)
}
}
BST.prototype.inOrderTraverse = function(callback){
this.inOrderTraverseNode(this.root, callback)
} // 3. 后序
BST.prototype.postOrderTraverseNode = function(node, callback){
if(node !== null){
this.postOrderTraverseNode(node.left, callback)
this.postOrderTraverseNode(node.right, callback)
callback(node.key)
}
}
BST.prototype.postOrderTraverse = function(callback){
this.postOrderTraverseNode(this.root, callback)
}

四、请实现从上往下打印二叉树

给定的二叉树为:[3, 9 , 20, null, null, 15, 7]

    3
/ \
9 20
/ \
15 7

请实现一个 printLevelOrder 方法,输出以下结果:

[
[3],
[9, 20],
[15, 7]
]

来源:102.二叉树的层次遍历

解析:

  • 方法一:
BST.prototype.printLevelOrder = function (root, arr = [], i = 0){
if (root && (root.key || root.key === 0)) {
!arr[i] && (arr[i] = [])
arr[i].push(root.key)
i++
root.left && this.printLevelOrder(root.left, arr, i)
root.right && this.printLevelOrder(root.right, arr, i)
}
return arr
}
  • 方法二:
BST.prototype.printLevelOrder = function (){
if(this.root === null) return []
let result = [], queue = [this.root]
while(true){
let len = queue.length, arr = []
while(len > 0){
console.log(queue)
let node = queue.shift()
len -= 1
arr.push(node.key)
if(node.left !== null) queue.push(node.left)
if(node.right !== null) queue.push(node.right)
}
if(arr.length === 0) return result
result.push([...arr])
}
}

五、给定一个二叉树,判断其是否是一个有效的二叉搜索树。

假设一个二叉搜索树具有如下特征:

  • 节点的左子树只包含小于当前节点的数。
  • 节点的右子树只包含大于当前节点的数。
  • 所有左子树和右子树自身必须也是二叉搜索树。

示例 1:

输入:
2
/ \
1 3
输出: true

示例 2:

输入:
5
/ \
1 4
/ \
3 6
输出: false
解释: 输入为: [5,1,4,null,null,3,6]。
根节点的值为 5 ,但是其右子节点值为 4 。

代码实现:

/**
* 二叉树节点定义
*/
function TreeNode(val) {
this.val = val;
this.left = this.right = null;
} /**
- @param {TreeNode} root
- @return {boolean}
*/
function isValidBST(root) {};

来源:99.验证二叉搜索树

解析:

function isValidBST(root) {
let arr = []
function inOrderTraverse(node){
if(node === null) return;
node.left && inOrderTraverse(node.left);
arr.push(node.val);
node.right && inOrderTraverse(node.right);
}
inOrderTraverse(root)
for(let i = 0; i < arr.length - 1; i++){
if(arr[i] >= arr[i+1]) return false
}
return true
};

每周一练 之 数据结构与算法(Tree)的更多相关文章

  1. 每周一练 之 数据结构与算法(Dictionary 和 HashTable)

    这是第五周的练习题,上周忘记发啦,这周是复习 Dictionary 和 HashTable. 下面是之前分享的链接: 1.每周一练 之 数据结构与算法(Stack) 2.每周一练 之 数据结构与算法( ...

  2. 每周一练 之 数据结构与算法(LinkedList)

    这是第三周的练习题,原本应该先发第二周的,因为周末的时候,我的母亲大人来看望她的宝贝儿子,哈哈,我得带她看看厦门这座美丽的城市呀. 这两天我抓紧整理下第二周的题目和答案,下面我把之前的也列出来: 1. ...

  3. 每周一练 之 数据结构与算法(Set)

    这是第四周的练习题,五一放假结束,该收拾好状态啦. 下面是之前分享的链接: 1.每周一练 之 数据结构与算法(Stack) 2.每周一练 之 数据结构与算法(LinkedList) 2.每周一练 之 ...

  4. 每周一练 之 数据结构与算法(Queue)

    这是第二周的练习题,这里补充下咯,五一节马上就要到了,自己的计划先安排上了,开发一个有趣的玩意儿. 下面是之前分享的链接: 1.每周一练 之 数据结构与算法(Stack) 2.每周一练 之 数据结构与 ...

  5. 每周一练 之 数据结构与算法(Stack)

    最近公司内部在开始做前端技术的技术分享,每周一个主题的 每周一练,以基础知识为主,感觉挺棒的,跟着团队的大佬们学习和复习一些知识,新人也可以多学习一些知识,也把团队内部学习氛围营造起来. 我接下来会开 ...

  6. 【算法】273-每周一练 之 数据结构与算法(Tree)

    这是第六周的练习题,最近加班比较多. 下面是之前分享的链接: [算法]200-每周一练 之 数据结构与算法(Stack) [算法]213-每周一练 之 数据结构与算法(LinkedList) [算法] ...

  7. 【算法】272-每周一练 之 数据结构与算法(Dictionary 和 HashTable)

    这是第五周的练习题,上周忘记发啦,这周是复习 Dictionary 和 HashTable. 下面是之前分享的链接: [算法]200-每周一练 之 数据结构与算法(Stack) [算法]213-每周一 ...

  8. JavaScript 数据结构与算法之美 - 非线性表中的树、堆是干嘛用的 ?其数据结构是怎样的 ?

    1. 前言 想学好前端,先练好内功,内功不行,就算招式练的再花哨,终究成不了高手. 非线性表(树.堆),可以说是前端程序员的内功,要知其然,知其所以然. 笔者写的 JavaScript 数据结构与算法 ...

  9. 【转】MySQL索引背后的数据结构及算法原理

    摘要 本文以MySQL数据库为研究对象,讨论与数据库索引相关的一些话题.特别需要说明的是,MySQL支持诸多存储引擎,而各种存储引擎对索引的支持也各不相同,因此MySQL数据库支持多种索引类型,如BT ...

随机推荐

  1. 在VMware15.5中安装CentOS7_7_64bit

    一.创建虚拟机 在我的另一个随笔里有. 地址为:https://www.cnblogs.com/qi-yuan/p/11692092.html 只是在虚拟机安装操作系统时候选择 Linux 而不是 W ...

  2. 【前端知识体系-JS相关】JS基础知识总结

    1 变量类型和计算 1.1 值类型和引用类型的区别? 值类型:每个变量都会存储各自的值.不会相互影响 引用类型:不同变量的指针执行了同一个对象(数组,对象,函数) 1.2 typeof可以及检测的数据 ...

  3. Java描述设计模式(21):状态模式

    本文源码:GitHub·点这里 || GitEE·点这里 一.生活场景 1.场景描述 变色龙是爬行动物,是非常奇特的动物,它有适于树栖生活的种种特征和行为,身体也会随着环境的变化而变化出适应环境的颜色 ...

  4. Win32窗口消息机制 x Android消息机制 x 异步执行

    如果你开发过Win32窗口程序,那么当你看到android代码到处都有的mHandler.sendEmptyMessage和 private final Handler mHandler = new ...

  5. HashSet源码学习,基于HashMap实现

    HashSet源码学习 一).Set集合的主要使用类 1). HashSet 基于对HashMap的封装 2). LinkedHashSet 基于对LinkedHashSet的封装 3). TreeS ...

  6. 使用boost data_time模块来获取毫秒级时间并转换为string字符串

    本文首发于个人博客https://kezunlin.me/post/107cbcbb/,欢迎阅读! using boost.date_time to get time in millisecond m ...

  7. Alibaba Nacos 学习(一):Nacos介绍与安装

    Alibaba Nacos 学习(一):Nacos介绍与安装 Alibaba Nacos 学习(二):Spring Cloud Nacos Config Alibaba Nacos 学习(三):Spr ...

  8. 详解 PHP 中的三大经典模式

    单例模式 单例模式的含义: 作为对象的创建模式,单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统全局地提供这个实例.它不会创建实例副本,而是会向单例类内部存储的实例返回一个引用. 单例模式 ...

  9. 【论文阅读】The Contextual Loss for Image Transformationwith Non-Aligned Data(ECCV2018 oral)

    目录: 相关链接 方法亮点 相关工作 方法细节 实验结果 总结与收获 相关链接 论文:https://arxiv.org/abs/1803.02077 代码:https://github.com/ro ...

  10. 快速入门函数式编程——以Javascript为例

    函数式编程是在不改变状态和数据的情况下使用表达式和函数来编写程序的一种编程范式.通过遵守这种范式,我们能够编写更清晰易懂.更能抵御bug的代码.这是通过避免使用流控制语句(for.while.brea ...