js二叉树
插入数值
//初始化node对象
function Node ( data) {
this.data = data;
this.left = null;
this.right = null;
}
// 定义插入对象
function BST () {
this.root = null;
this.size = size;
this.count = 0;
this.insert = insert;
this.show = show;
}
//查询节点个数
function size () {
return this.count;
}
// 展示节点树
function show () {
return this.root;
}
function insert (data) {
var n = new Node(data, null, null);
if (this.root == null) {
//如果不存在节点,则此节点是根节点
this.root = n;
this.count++;
} else {
var current = this.root;
var parent;
while(current) {
parent = current;
if(data < current.data){
current = current.left;
if(current == null) {
parent.left = n;
this.count++;
break;
}
} else if (data > current.data){
current = current.right;
if(current == null) {
parent.right = n;
this.count++;
break;
}
} else {
current.data == data;
break;
}
}
}
}
插入键值对
//初始化node对象
function Node (key ,value) {
this.key = key;
this.value = value;
this.left = null;
this.right = null;
}
// 定义插入对象
function BST () {
this.root = null;
this.size = size;
this.count = 0;
this.insert = insert;
this.show = show;
this.contain = contain;
this.search = search;
}
//查询节点个数
function size () {
return this.count;
}
// 展示节点树
function show () {
return this.root;
}
//插入
function insert (key, value) {
var n = new Node(key, value);
if (this.root == null) {
//如果不存在节点,则此节点是根节点
this.count++;
return this.root = n;
} else {
var current = this.root;
var parent;
while(current) {
parent = current;
if(key < current.key){
current = current.left;
if(current == null) {
parent.left = n;
this.count++;
break;
}
} else if (key > current.key){
current = current.right;
if(current == null) {
parent.right = n;
this.count++;
break;
}
} else {
current = n;
break;
}
}
}
}
//查找是否含有这个值
function contain (key) {
if(!this.root){
return false;
}
var current = this.root;
while (current){
if (key == current.key) {
return true;
} else if (key > current.key){
current = current.right;
} else {
current = current.left;
}
}
}
//查找
function search (key) {
if(!this.root){
return null;
}
var current = this.root;
while (current){
if (key == current.key) {
return current;
} else if (key > current.key){
current = current.right;
} else {
current = current.left;
}
}
}
js二叉树的更多相关文章
- js 二叉树遍历
二叉树定义这里不再赘述. 我这里有个二叉树: var tree = { "id": 0, "name": "root", "lef ...
- JS - 二叉树算法实现与遍历 (更新中...)
一.关于二叉树: 截图来自:https://segmentfault.com/a/1190000000740261 温馨提示:学习以及使用二叉树概念,心中永远有这么一个图,对于理解和接受二叉树有很大的 ...
- js二叉树,前序/中序/后序(最大最小值,排序)
function Node(data,left,right) { this.left=left this.right=right this.data=data } function Btr() { t ...
- js 二叉树算法
//生成二叉树 function binarySearchTree() { let Node = function(key) { this.key = key; this.left = null; t ...
- js 二叉树删除最大值和最小值
//删除最小值function delMinNode (root){ if(!root) { return false; } var current = root; if (current.left ...
- 实现js的二叉树
今天算是第一次写一篇自己的博客,越是学习就越感叹学无止境,为了记录下来用js实现二叉树的方法,这算是最简单的一个算法了. 二叉树实现原理:把数组的第一个数据当作根节点,每个节点都有根节点,左孩子和右孩 ...
- js 实现二叉树
二叉树是每个结点最多有两个子树的有序树.通常子树的根被称作“左子树”(left subtree)和“右子树”(right subtree),右边的总是大于左边的!二叉树的每个结点至多只有二棵子树(不存 ...
- 【js数据结构】可逐次添加叶子的二叉树(非最优二叉树)
最近小菜鸟西瓜莹看到了一道面试题: 给定二叉树,按层打印.例如1的子节点是2.3, 2的子节点是3.4, 5的子节点是6,7. 需要建立如图二叉树: 但是西瓜莹找到的相关代码都是用js构建最优二叉树, ...
- javascript/js实现 排序二叉树数据结构 学习随笔
二叉树是一种数据结构.其特点是: 1.由一系列节点组成,具有层级结构.每个节点的特性包含有节点值.关系指针.节点之间存在对应关系. 2.树中存在一个没有父节点的节点,叫做根节点.树的末尾存在一系列没有 ...
随机推荐
- Sitecore CMS中创建模板
如何在Sitecore CMS中创建模板. 在/sitecore/templates选择应创建模板的文件夹中. 注意:在多站点项目中,通常会在模板所属的网站名称的/sitecore/templates ...
- SourceTree 使用
删除分支 新建一个分支 A 后,要想把 A 分支删除掉,只需跳到另一个分支上去,选中 A 分支然后右击,在弹出的菜单栏中选中 [删除 A 分支]即可: 在分支下建一个文件夹 左上的然后弹出选框,在[新 ...
- memcache、redis、mongoDB 如何选择?
不同的 Nosql,其实应用的场景各有不同,所以我们应该先了解不同Nosql 之间的差别,然后分析什么才是最适合我使用的 Nosql. Nosql 介绍 Nosql 的全称是 Not Only Sql ...
- python自定义方法处理日志文件
从命令行界面拷贝的内容包含过个">>>",函数的作用是用正则把每两个">>>"之间的字符取出来,然后把包含“Tracebac ...
- inux 驱动程序开发中输入子系统总共能产生哪些事件类型(EV_KEY,EV_ABS,EV_REL)
inux 驱动程序开发中, 输入子系统总共能产生哪些事件类型?,以及分别是什么意思?详见如下: Linux中输入设备的事件类型有EV_SYN 0x00 同步事件EV_KEY 0x01 按键事件,如KE ...
- tft屏图像显示也成功完成
2010-04-30 14:18:00 tft屏图像显示也成功完成. 其实有了刷屏的经验,图像显示就很简单. void address_set(uint x1,uint y1,uint x2,uint ...
- Android开发常见错误汇总
[错误信息] [2011-01-19 16:39:10 - ApiDemos] WARNING: Application does not specify an API level requireme ...
- Linux账号管理
Linux系统是一个多用户多任务的分时操作系统,任何一个要使用系统资源的用户,都必须首先向系统管理员申请一个账号,然后以这个账号的身份进入系统. 用户的账号一方面可以帮助系统管理员对使用系统的用户进行 ...
- Python+OpenCV图像处理(三)—— Numpy数组操作图片
一.改变图片每个像素点每个通道的灰度值 (一) 代码如下: #遍历访问图片每个像素点,并修改相应的RGB import cv2 as cv def access_pixels(image): prin ...
- Java常用API、Math类介绍
一.API的概述 API——Application Programing Interface:应用程序编程接口,是java提供的一些预定义的函数: 目的:基于API实现程序的快速编写,只需了解其作用, ...