资讯专栏INFORMATION COLUMN

二叉树简单两三下

lwx12525 / 830人阅读

摘要:今天温习的是树中比较简单常用的二叉树。因为一个简单固定的结构更有利于查询,所以有了二叉查找树的概念。简单介绍下研究依然以点线模型的分析理解,不过树是从一个方向顺势的分支结构。

前言

树和图一样,是常用的数据结构模型,但是我的理解树是图的一个用于更具体的数据结构。今天温习的是树中比较简单、常用的二叉树。因为一个简单固定的结构更有利于查询,所以有了二叉查找树的概念。内容来源来自《JavaScript 数据结构和算法》。

简单介绍下?

研究依然以点-线模型的分析理解,不过树是从一个方向顺势的分支结构。这里可以是自上向下,也可以自下向上。

树的常见术语有几个:

根节点

子节点

叶子节点

子树

路径

键(这里是抽象主要的数据)

二叉树:

子节点不超过两个

左节点

右节点

如果具备查找特性:

</>复制代码

  1. 较小值在左,较大值在右

这里准备一组值来构建树:

</>复制代码

  1. [ 23, 45, 16, 37, 3, 99, 22 ]

然后我需要构建的树的成品是:

具体实现

首先需要构造一个节点对象,来表示节点这一个描述元素

</>复制代码

  1. class Node {
  2. constructor (data, left, right) {
  3. this.data = data;
  4. this.left = left;
  5. this.right = right;
  6. }
  7. getData () {
  8. return this.data;
  9. }
  10. output() {
  11. console.log(this.data);
  12. }
  13. }

主要包含:

data: 节点的键

left: 左节点

right: 右节点

getData: 获取键

output: 辅助输出函数

树的对象以及树的操作

</>复制代码

  1. class Tree {
  2. constructor () {
  3. // 根节点默认是 null
  4. this.root = null;
  5. }
  6. // 插入节点
  7. insert (data) {
  8. const node = new Node(data, null, null);
  9. if(this.root === null) {
  10. this.root = node;
  11. } else {
  12. let current = this.root;
  13. let parent = null;
  14. while(1) {
  15. parent = current;
  16. if(data < current.data) {
  17. current = current.left;
  18. if(current === null) {
  19. parent.left = node;
  20. break;
  21. }
  22. } else {
  23. current = current.right;
  24. if(current === null) {
  25. parent.right = node;
  26. break;
  27. }
  28. }
  29. }
  30. }
  31. return this;
  32. }
  33. // 中序遍历
  34. ascOrder (node) {
  35. if(node !== null) {
  36. this.ascOrder(node.left);
  37. node.output();
  38. this.ascOrder(node.right);
  39. }
  40. }
  41. // 先序遍历
  42. rootOrder (node) {
  43. if(node !== null) {
  44. node.output();
  45. this.rootOrder(node.left);
  46. this.rootOrder(node.right);
  47. }
  48. }
  49. // 后序遍历
  50. leafOrder (node) {
  51. if(node !== null) {
  52. this.leafOrder(node.left);
  53. this.leafOrder(node.right);
  54. node.output();
  55. }
  56. }
  57. // 执行路径的 action: left or right
  58. action (path) {
  59. let node = this.root;
  60. while(node[path] !== null) {
  61. node = node[path];
  62. }
  63. return node;
  64. }
  65. // 最小值
  66. min () {
  67. return this.action("left");
  68. }
  69. // 最大值
  70. max () {
  71. return this.action("right");
  72. }
  73. // 查找固定值
  74. find (data) {
  75. let node = this.root;
  76. while(node !== null) {
  77. if(data === node.data) {
  78. return node;
  79. } else if(data < node.data) {
  80. node = node.left;
  81. } else {
  82. node = node.right;
  83. }
  84. }
  85. return node;
  86. }
  87. }

最后我在 Node 环境下测试,所以导出一下 Tree 类:

</>复制代码

  1. module.exports = Tree;

对于每一种排序后的结果是不一样的,可以用图形来表示一下:

中序遍历的过程:

先序遍历的过程:

后序遍历的过程:

其实看图是最直观的,其实算法这玩意最好的就是能够体会思想,然后根据实际的场景进行映射建立数据结构模型,以最优或更平衡的去解决问题。

测试代码如下:

</>复制代码

  1. const Tree = require("./binTree");
  2. const log = s => console.log(s);
  3. const tree = new Tree();
  4. [23, 45, 16, 37, 3, 99, 22].forEach(n => tree.insert(n));
  5. log("中-排序:");
  6. tree.ascOrder(tree.root);
  7. log("先-排序:");
  8. tree.rootOrder(tree.root);
  9. log("后-排序:");
  10. tree.leafOrder(tree.root);
  11. console.log("=====================");
  12. log("最小值:");
  13. log( tree.min() );
  14. log("最大值:");
  15. log( tree.max() );
  16. log("查找 3:");
  17. log( tree.find(3) );
  18. log("查找 11:");
  19. log( tree.find(11) );

终端跑的结果如下:

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/91258.html

相关文章

  • 叉树就是这么简单

    前言 只有光头才能变强。 文本已收录至我的GitHub仓库,欢迎Star:https://github.com/ZhongFuCheng3y/3y 一、二叉树就是这么简单 本文撇开一些非常苦涩、难以理解的概念来讲讲二叉树,仅入门观看(或复习).... 首先,我们来讲讲什么是树: 树是一种非线性的数据结构,相对于线性的数据结构(链表、数组)而言,树的平均运行时间更短(往往与树相关的排序时间复杂度都...

    Cruise_Chan 评论0 收藏0
  • 叉树那些事儿

    摘要:大家在聊到二叉树的时候,总会离不开链表。受限线性表主要包括栈和队列,受限表示对结点的操作受限制。存储结构线性表主要由顺序表示或链式表示。链式表示指的是用一组任意的存储单元存储线性表中的数据元素,称为线性表的链式存储结构。 大家在聊到二叉树的时候,总会离不开链表。这里先带大家一起了解一些基本概念。 线性表 概念 线性表是最基本、最简单、也是最常用的一种数据结构。 线性表中数据元素之间的关...

    Little_XM 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<