我觉得这社会上,也不差钱好多人,可能好多人也不差权力,但是我觉得能得到这种满足的也不多。 --郭小平<临汾红丝带学校校长>
树是计算机科学中经常用到的一种数据结构。树是一种非线性的数据结构,以分层的方式存储数据。是被用来存储具有层级关系或有序的数据,比如文件系统中的文件。
二叉树,每个节点最多有两个子树的树结构。二叉树是一种特殊的树,也是一个连通的无环图。
二叉查找树是一种特殊的二叉树,其相对较小的值保存在左节点中,较大的值保存在右节点中。这一特性使其查找效率很高。
如果待插入节点小于(大于)当前节点,且当前节点的左(右)节点为null,则将待插入节点插入到当前节点的左(右)节点位置上,结束循环;否则,将当前节点的左(右)节作为当前节点继续下次循环。
/** * 节点定义 * @param data 数据 * @param left 左子树 * @param right 右子树 * @constructor */ function Node(data, left, right) { this.data = data; this.left = left; this.right = right; } Node.prototype.show = function() { return this.data; }; /** * 二叉查找树定义 * @constructor */ function BST() { this.root = null; } /** * 插入节点 * @constructor */ BST.prototype.insert = function(data) { // 待插入节点 var node = new Node(data, null, null); if(this.root === null) { this.root = node; } else { var currentNode = this.root; var parent; while(true) { parent = currentNode; // 待插入节点小于当前节点 if(data < currentNode.data) { // 将其左子树作为当前节点 currentNode = currentNode.left; if(currentNode === null) { parent.left = node; break; } }else { // 将其右子树作为当前节点 currentNode = currentNode.right; if(currentNode === null) { parent.right = node; break; } } } } return this; // 支持链式调用 }; 中序:先访问左子树,再访问根节点,最后访问右字数;以升序访问BST上所有节点;(左==>根==>右)先序:先访问根节点,然后以同样方式访问左子树和右子树;(根==>左==>右)后序:先访问叶子节点,从左子树到右子树,再到根节点。(左==>右==>根) BST.prototype.order = function(node, type) { switch (type) { case "inorder": // 中序 if(node != null) { /*左==>根==>右*/ this.order(node.left, type); console.log(node.show()); this.order(node.right, type); } break; case "preorder": // 先序 if(node != null) { /*根==>左==>右*/ console.log(node.show()); this.order(node.left, type); this.order(node.right, type); } break; case "postorder": // 后序 if(node != null) { /*左==>右==>根*/ this.order(node.left, type); this.order(node.right, type); console.log(node.show()); } break; } };测试
var bst = new BST(); bst.insert(32).insert(11).insert(2) .insert(13).insert(75) .insert(66).insert(88); bst.order(bst.root, "inorder"); // 中序 bst.order(bst.root, "preorder"); // 先序 bst.order(bst.root, "postorder"); // 后序 树,在计算机科学中体现的特别多 。如,我们熟悉的DOM树,数据库底层经常用到的B树等等。树能很好的保证字典序,存储词典的空间压缩率高, 能做前缀搜索。抽象地说,基本上有序列的地方就可以应用树,因为树结构即是一种序列索引结构。
奋飛 认证博客专家 技术管理 前端工程化 乐观、勇气、专注、果断、好奇、公正、慎思、真诚、追求极致追求完美、诚信!独立撰写了多个前端专题模块,访问量达百万级。多次负责组织大数据可视化前端架构平台开发工作。对前端新技术、新潮流具有很强的敏锐力和洞察力!