René's URL Explorer Experiment


Title: 前端进阶算法11:二叉查找树(BST树) · Issue #87 · sisterAn/JavaScript-Algorithms · GitHub

Open Graph Title: 前端进阶算法11:二叉查找树(BST树) · Issue #87 · sisterAn/JavaScript-Algorithms

X Title: 前端进阶算法11:二叉查找树(BST树) · Issue #87 · sisterAn/JavaScript-Algorithms

Description: 一、二叉查找树(BST树) 有的笔者也称它为二叉搜索树,都是一个意思。 二叉树本身没有多大的意义,直到有位大佬提出一个 trick。 如果我们规定一颗二叉树上的元素拥有顺序,所有比它小的元素在它的左子树,比它大的元素在它的右子树,那么我们不就可以很快地查找某个元素了吗? 不得不说这是一个非常天才的想法,于是,二叉查找树诞生了。 所以,二叉查找树与二叉树不同的是,它在二叉树的基础上,增加了对二叉树上节点存储位置的限制:二叉搜索树上的每个节点都需要满足: 左子节点值小于该节...

Open Graph Description: 一、二叉查找树(BST树) 有的笔者也称它为二叉搜索树,都是一个意思。 二叉树本身没有多大的意义,直到有位大佬提出一个 trick。 如果我们规定一颗二叉树上的元素拥有顺序,所有比它小的元素在它的左子树,比它大的元素在它的右子树,那么我们不就可以很快地查找某个元素了吗? 不得不说这是一个非常天才的想法,于是,二叉查找树诞生了。 所以,二叉查找树与二叉树不同的是,它在二叉树的基础上,增加了对二...

X Description: 一、二叉查找树(BST树) 有的笔者也称它为二叉搜索树,都是一个意思。 二叉树本身没有多大的意义,直到有位大佬提出一个 trick。 如果我们规定一颗二叉树上的元素拥有顺序,所有比它小的元素在它的左子树,比它大的元素在它的右子树,那么我们不就可以很快地查找某个元素了吗? 不得不说这是一个非常天才的想法,于是,二叉查找树诞生了。 所以,二叉查找树与二叉树不同的是,它在二叉树的基础上,增加了对二...

Opengraph URL: https://github.com/sisterAn/JavaScript-Algorithms/issues/87

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"前端进阶算法11:二叉查找树(BST树)","articleBody":"### 一、二叉查找树(BST树)\r\n\r\n有的笔者也称它为二叉搜索树,都是一个意思。\r\n\r\n二叉树本身没有多大的意义,直到有位大佬提出一个 trick。\r\n\r\n如果我们规定一颗二叉树上的元素拥有顺序,所有比它小的元素在它的左子树,比它大的元素在它的右子树,那么我们不就可以很快地查找某个元素了吗?\r\n\r\n不得不说这是一个非常天才的想法,于是,二叉查找树诞生了。\r\n\r\n所以,二叉查找树与二叉树不同的是,它在二叉树的基础上,增加了对二叉树上节点存储位置的限制:二叉搜索树上的每个节点都需要满足:\r\n\r\n- 左子节点值小于该节点值\r\n- 右子节点值大于等于该节点值\r\n\r\n![img](https://cdn.nlark.com/yuque/0/2020/png/273506/1584237137899-5cb8a9d4-c2f7-4741-98c8-b29d24951300.png?x-oss-process=image/resize,w_1500)\r\n\r\n在二叉树中,所有子节点值都是没有固定规律的,所以使用二叉树存储结构存储数据时,查找数据的时间复杂度为 O(n),因为它要查找每一个节点。\r\n\r\n而使用二叉查找树就不同了,例如上图,我们如果要查找 6 ,先从根节点 10 比较,6 比 10 小,则查找左子树,再与 8 比较,6 比 8 小,继续查找 8 的左子树,也就是 6,我们找到了元素,结束。\r\n\r\n### 二、代码实现\r\n\r\n```js\r\nfunction BinarySearchTree() {\r\n  let Node = function (key) {\r\n    this.key = key\r\n    this.left = null\r\n    this.right = null\r\n  }\r\n  let root = null\r\n  \r\n  // 插入\r\n  this.insert = function(key){}\r\n  \r\n  // 查找\r\n  this.search = function(key){}\r\n  \r\n  // 删除\r\n  this.remove = function(key){}\r\n  \r\n  // 最大值\r\n  this.max = function(){}\r\n  \r\n  // 最小值\r\n  this.min = function(){}\r\n  \r\n  // 中序遍历\r\n  this.inOrderTraverse = function(){}\r\n  \r\n  // 先序遍历\r\n  this.preOrderTraverse = function(){}\r\n  \r\n  // 后序遍历\r\n  this.postOrderTraverse = function(){}\r\n}\r\n```\r\n\r\n#### 插入:\r\n\r\n- 首先创建一个新节点\r\n- 判断树是否为空,为空则设置插入的节点为根节点,插入成功,返回\r\n- 如果不为空,则比较该节点与根结点,比根小,插入左子树,否则插入右子树\r\n\r\n```js\r\nfunction insert(key) {\r\n  // 创建新节点\r\n  let newNode = new Node(key)\r\n  // 判断是否为空树\r\n  if(root === null) {\r\n    root = newNode\r\n  } else {\r\n    insertNode(root, newNode)\r\n  }\r\n}\r\n\r\n// 将 insertNode 插入到 node 子树上\r\nfunction insertNode(node, newNode) {\r\n  if(newNode.key \u003c node.key) {\r\n    // 插入 node 左子树\r\n    if(node.left === null) {\r\n      node.left = newNode\r\n    } else {\r\n      insertNode(node.left, newNode)\r\n    }\r\n  } else {\r\n    // 插入 node 右子树\r\n    if(node.right === null) {\r\n      node.right = newNode\r\n    } else {\r\n      insertNode(node.right, newNode)\r\n    }\r\n  }\r\n}\r\n```\r\n\r\n#### 最值:\r\n\r\n最小值:树最左端的节点\r\n\r\n最大值:树最右端的节点\r\n\r\n```js\r\n// 最小值\r\nfunction min() {\r\n  let node = root\r\n  if(node) {\r\n    while(node \u0026\u0026 node.left !== null) {\r\n      node = node.left\r\n    }\r\n    return node.key\r\n  }\r\n  return null\r\n}\r\n\r\n// 最大值\r\nfunction max() {\r\n  let node = root\r\n  if(node) {\r\n    while(node \u0026\u0026 node.right !== null) {\r\n      node = node.right\r\n    }\r\n    return node.key\r\n  }\r\n  return null\r\n}\r\n```\r\n\r\n#### 查找:\r\n\r\n```js\r\nfunction search(key) {\r\n  return searchNode(root, key)\r\n}\r\n\r\nfunction searchNode(node, key) {\r\n  if(node === null) \r\n    return false\r\n  if(key \u003c node.key) {\r\n    return searchNode(node.left, key)\r\n  } else if(key \u003e node.key) {\r\n    return searchNode(node.right, key)\r\n  } else {\r\n    return true\r\n  }\r\n}\r\n```\r\n\r\n#### 删除:\r\n\r\n```js\r\nfunction remove(key) {\r\n  root = removeNode(root, key)\r\n}\r\n\r\nfunction removeNode(node, key) {\r\n  if(node === null) \r\n    return null\r\n  if(key \u003c node.key) {\r\n    return removeNode(node.left, key)\r\n  } else if(key \u003e node.key) {\r\n    return removeNode(node.right, key)\r\n  } else {\r\n    // key = node.key 删除\r\n    //叶子节点\r\n    if(node.left === null \u0026\u0026 node.right === null) {\r\n      node = null\r\n      return node\r\n    }\r\n    // 只有一个子节点\r\n    if(node.left === null) {\r\n      node = node.right\r\n      return node\r\n    }\r\n    if(node.right === null) {\r\n      node = node.left\r\n      return node\r\n    }\r\n    // 有两个子节点\r\n    // 获取右子树的最小值替换当前节点\r\n    let minRight = findMinNode(node.right)\r\n    node.key = minRight.key\r\n    node.right = removeNode(node.right, minRight.key)\r\n    return node\r\n  }\r\n}\r\n\r\n// 获取 node 树最小节点\r\nfunction findMinNode(node) {\r\n  if(node) {\r\n    while(node \u0026\u0026 node.right !== null) {\r\n      node = node.right\r\n    }\r\n    return node\r\n  }\r\n  return null\r\n}\r\n```\r\n\r\n#### 中序遍历:\r\n\r\n顾名思义,中序遍历就是把根放在中间的遍历,即按先左节点、然后根节点、最后右节点(左根右)的遍历方式。\r\n\r\n由于BST树任意节点都大于左子节点值小于等于右子节点值的特性,所以 **中序遍历其实是对🌲进行排序操作** ,并且是按从小到大的顺序排序。\r\n\r\n```js\r\nfunction inOrderTraverse(callback) {\r\n  inOrderTraverseNode(root, callback)\r\n}\r\n\r\nfunction inOrderTraverseNode(node, callback) {\r\n  if(node !== null) {\r\n    // 先遍历左子树\r\n    inOrderTraverseNode(node.left, callback)\r\n    // 然后根节点\r\n    callback(node.key)\r\n    // 再遍历右子树\r\n    inOrderTraverseNode(node.right, callback)\r\n  }\r\n}\r\n\r\n// callback 每次将遍历到的结果打印到控制台\r\nfunction callback(key) {\r\n  console.log(key)\r\n}\r\n```\r\n\r\n#### 先序遍历:\r\n\r\n已经实现的中序遍历,先序遍历就很简单了,它是按根左右的顺序遍历\r\n\r\n```js\r\nfunction preOrderTraverse() {\r\n  preOrderTraverseNode(root, callback)\r\n}\r\n\r\nfunction preOrderTraverseNode(node, callback) {\r\n  if(node !== null) {\r\n    // 先根节点\r\n    callback(node.key)\r\n    // 然后遍历左子树\r\n    preOrderTraverseNode(node.left, callback)\r\n    // 再遍历右子树\r\n    preOrderTraverseNode(node.right, callback)\r\n  }\r\n}\r\n\r\n// callback 每次将遍历到的结果打印到控制台\r\nfunction callback(key) {\r\n  console.log(key)\r\n}\r\n```\r\n\r\n#### 后序遍历:\r\n\r\n后序遍历按照左右根的顺序遍历,实现也很简单。\r\n\r\n```js\r\nfunction postOrderTraverse() {\r\n  postOrderTraverseNode(root, callback)\r\n}\r\n\r\nfunction postOrderTraverseNode(node, callback) {\r\n  if(node !== null) {\r\n    // 先遍历左子树\r\n    postOrderTraverseNode(node.left, callback)\r\n    // 然后遍历右子树\r\n    postOrderTraverseNode(node.right, callback)\r\n    // 最后根节点\r\n    callback(node.key)\r\n  }\r\n}\r\n\r\n// callback 每次将遍历到的结果打印到控制台\r\nfunction callback(key) {\r\n  console.log(key)\r\n}\r\n```\r\n\r\n### 三、BST树的局限\r\n\r\n在理想情况下,二叉树每多一层,可以存储的元素都增加一倍。也就是说 n 个元素的二叉搜索树,对应的树高为 O(logn)。所以我们查找元素、插入元素的时间也为 O(logn)。\r\n\r\n当然这是理想情况下,但在实际应用中,并不是那么理想,例如一直递增或递减的给一个二叉查找树插入数据,那么所有插入的元素就会一直出现在一个树的左节点上,数型结构就会退化为链表结构,时间复杂度就会趋于 O(n),这是不好的。\r\n\r\n而我们上面的平衡树就可以很好的解决这个问题,所以平衡二叉查找树(AVL树)由此诞生。","author":{"url":"https://github.com/sisterAn","@type":"Person","name":"sisterAn"},"datePublished":"2020-07-21T12:34:39.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/87/JavaScript-Algorithms/issues/87"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:a325c13c-cea3-75d1-22d4-6dc6d0fcf5c5
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idC6B8:3A9D93:EC2511:14CEB3B:696A686E
html-safe-nonce6a6dd95f6b67a0703ab2ca4f43c791080448357103421b7a3f52e0326cfa19bb
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNkI4OjNBOUQ5MzpFQzI1MTE6MTRDRUIzQjo2OTZBNjg2RSIsInZpc2l0b3JfaWQiOiI1Njg3MDQwMjczNzAzMTM1MzQyIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmacbb368b9fb7bd477ce457e934d19d4c2b496b91906287dd91b7391b02d318f74b
hovercard-subject-tagissue:662968901
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/sisterAn/JavaScript-Algorithms/87/issue_layout
twitter:imagehttps://opengraph.githubassets.com/0b6500f667a7e19f9abfedb77d866c5bc9aa94e490fd0a9cd3310c91b9a930ff/sisterAn/JavaScript-Algorithms/issues/87
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/0b6500f667a7e19f9abfedb77d866c5bc9aa94e490fd0a9cd3310c91b9a930ff/sisterAn/JavaScript-Algorithms/issues/87
og:image:alt一、二叉查找树(BST树) 有的笔者也称它为二叉搜索树,都是一个意思。 二叉树本身没有多大的意义,直到有位大佬提出一个 trick。 如果我们规定一颗二叉树上的元素拥有顺序,所有比它小的元素在它的左子树,比它大的元素在它的右子树,那么我们不就可以很快地查找某个元素了吗? 不得不说这是一个非常天才的想法,于是,二叉查找树诞生了。 所以,二叉查找树与二叉树不同的是,它在二叉树的基础上,增加了对二...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamesisterAn
hostnamegithub.com
expected-hostnamegithub.com
None6fea32d5b7276b841b7a803796d9715bc6cfb31ed549fdf9de2948ac25d12ba6
turbo-cache-controlno-preview
go-importgithub.com/sisterAn/JavaScript-Algorithms git https://github.com/sisterAn/JavaScript-Algorithms.git
octolytics-dimension-user_id19721451
octolytics-dimension-user_loginsisterAn
octolytics-dimension-repository_id252061924
octolytics-dimension-repository_nwosisterAn/JavaScript-Algorithms
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id252061924
octolytics-dimension-repository_network_root_nwosisterAn/JavaScript-Algorithms
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
releasef2d9f6432a5a115ec709295ae70623f33bb80aee
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/sisterAn/JavaScript-Algorithms/issues/87#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2FsisterAn%2FJavaScript-Algorithms%2Fissues%2F87
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub SparkBuild and deploy intelligent appshttps://github.com/features/spark
GitHub ModelsManage and compare promptshttps://github.com/features/models
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
GitHub SponsorsFund open source developershttps://github.com/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/accelerator
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2FsisterAn%2FJavaScript-Algorithms%2Fissues%2F87
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=sisterAn%2FJavaScript-Algorithms
Reloadhttps://github.com/sisterAn/JavaScript-Algorithms/issues/87
Reloadhttps://github.com/sisterAn/JavaScript-Algorithms/issues/87
Reloadhttps://github.com/sisterAn/JavaScript-Algorithms/issues/87
sisterAn https://github.com/sisterAn
JavaScript-Algorithmshttps://github.com/sisterAn/JavaScript-Algorithms
Notifications https://github.com/login?return_to=%2FsisterAn%2FJavaScript-Algorithms
Fork 649 https://github.com/login?return_to=%2FsisterAn%2FJavaScript-Algorithms
Star 5.7k https://github.com/login?return_to=%2FsisterAn%2FJavaScript-Algorithms
Code https://github.com/sisterAn/JavaScript-Algorithms
Issues 158 https://github.com/sisterAn/JavaScript-Algorithms/issues
Pull requests 0 https://github.com/sisterAn/JavaScript-Algorithms/pulls
Actions https://github.com/sisterAn/JavaScript-Algorithms/actions
Projects 0 https://github.com/sisterAn/JavaScript-Algorithms/projects
Security Uh oh! There was an error while loading. Please reload this page. https://github.com/sisterAn/JavaScript-Algorithms/security
Please reload this pagehttps://github.com/sisterAn/JavaScript-Algorithms/issues/87
Insights https://github.com/sisterAn/JavaScript-Algorithms/pulse
Code https://github.com/sisterAn/JavaScript-Algorithms
Issues https://github.com/sisterAn/JavaScript-Algorithms/issues
Pull requests https://github.com/sisterAn/JavaScript-Algorithms/pulls
Actions https://github.com/sisterAn/JavaScript-Algorithms/actions
Projects https://github.com/sisterAn/JavaScript-Algorithms/projects
Security https://github.com/sisterAn/JavaScript-Algorithms/security
Insights https://github.com/sisterAn/JavaScript-Algorithms/pulse
New issuehttps://github.com/login?return_to=https://github.com/sisterAn/JavaScript-Algorithms/issues/87
New issuehttps://github.com/login?return_to=https://github.com/sisterAn/JavaScript-Algorithms/issues/87
前端进阶算法11:二叉查找树(BST树)https://github.com/sisterAn/JavaScript-Algorithms/issues/87#top
文章https://github.com/sisterAn/JavaScript-Algorithms/issues?q=state%3Aopen%20label%3A%22%E6%96%87%E7%AB%A0%22
https://github.com/sisterAn
https://github.com/sisterAn
sisterAnhttps://github.com/sisterAn
on Jul 21, 2020https://github.com/sisterAn/JavaScript-Algorithms/issues/87#issue-662968901
https://camo.githubusercontent.com/7ae06bc200cad9cb7c55523a1263e7ec143acf9c6903633f4504b54e18bc633d/68747470733a2f2f63646e2e6e6c61726b2e636f6d2f79757175652f302f323032302f706e672f3237333530362f313538343233373133373839392d35636238613964342d633266372d343734312d393863382d6232396432343935313330302e706e673f782d6f73732d70726f636573733d696d6167652f726573697a652c775f31353030
文章https://github.com/sisterAn/JavaScript-Algorithms/issues?q=state%3Aopen%20label%3A%22%E6%96%87%E7%AB%A0%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.