插入数值
//初始化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二叉树的更多相关文章

  1. js 二叉树遍历

    二叉树定义这里不再赘述. 我这里有个二叉树: var tree = { "id": 0, "name": "root", "lef ...

  2. JS - 二叉树算法实现与遍历 (更新中...)

    一.关于二叉树: 截图来自:https://segmentfault.com/a/1190000000740261 温馨提示:学习以及使用二叉树概念,心中永远有这么一个图,对于理解和接受二叉树有很大的 ...

  3. js二叉树,前序/中序/后序(最大最小值,排序)

    function Node(data,left,right) { this.left=left this.right=right this.data=data } function Btr() { t ...

  4. js 二叉树算法

    //生成二叉树 function binarySearchTree() { let Node = function(key) { this.key = key; this.left = null; t ...

  5. js 二叉树删除最大值和最小值

    //删除最小值function delMinNode (root){ if(!root) { return false; } var current = root; if (current.left ...

  6. 实现js的二叉树

    今天算是第一次写一篇自己的博客,越是学习就越感叹学无止境,为了记录下来用js实现二叉树的方法,这算是最简单的一个算法了. 二叉树实现原理:把数组的第一个数据当作根节点,每个节点都有根节点,左孩子和右孩 ...

  7. js 实现二叉树

    二叉树是每个结点最多有两个子树的有序树.通常子树的根被称作“左子树”(left subtree)和“右子树”(right subtree),右边的总是大于左边的!二叉树的每个结点至多只有二棵子树(不存 ...

  8. 【js数据结构】可逐次添加叶子的二叉树(非最优二叉树)

    最近小菜鸟西瓜莹看到了一道面试题: 给定二叉树,按层打印.例如1的子节点是2.3, 2的子节点是3.4, 5的子节点是6,7. 需要建立如图二叉树: 但是西瓜莹找到的相关代码都是用js构建最优二叉树, ...

  9. javascript/js实现 排序二叉树数据结构 学习随笔

    二叉树是一种数据结构.其特点是: 1.由一系列节点组成,具有层级结构.每个节点的特性包含有节点值.关系指针.节点之间存在对应关系. 2.树中存在一个没有父节点的节点,叫做根节点.树的末尾存在一系列没有 ...

随机推荐

  1. nw.js package一般设置

    { "name": "app name", "main": "mainpage",                    ...

  2. 2.匿名类,匿名类对象,private/protected/public关键字、abstract抽象类,抽象方法、final关键字的使用,多线程Thread类start方法原理

    package com.bawei.multithread; //注意:模板方法我们通常使用抽象类或者抽象方法!这里我们为了方便在本类中使用就没有使用抽象类/抽象方法 public class Tem ...

  3. C#深入研究ArrayList动态数组自动扩容原理

    1 void Test1() { ArrayList arrayList = new ArrayList(); ; ; i < length; i++) { arrayList.Add(&quo ...

  4. jQuery筛选--find(expr|obj|ele)和siblings([expr])

    find(expr|obj|ele) 概述 搜索所有与指定表达式匹配的元素.这个函数是找出正在处理的元素的后代元素的好方法 参数 expr  用于查找的表达式 jQuery object   一个用于 ...

  5. sql server启动服务和还原bak文件

    sql server启动服务和还原bak文件, sql server启动要: mysql数据库备份是psc后缀文件, sql server还原数据库备份bak文件: 三张图简介明了: ok:

  6. There is no session with id XXX

    系统采用 shiro + redis + spring来做的权限控制系统.   登录时报 there is no session with XXX 跟踪断点发现,系统查询session时,查不到red ...

  7. tomcat2章1

    package ex02.pyrmont; import java.io.File; public class Constants { public static final String WEB_R ...

  8. echo 命令详解

    echo命令用于在shell中打印shell变量的值,或者直接输出指定的字符串. 选项 -e:激活转义字符. 使用-e选项时,若字符串中出现以下字符,则特别加以处理,而不会将它当成一般文字输出: \a ...

  9. [转载]Javascript:history.go()和history.back()的用法和区别

    Javascript:history.go()和history.back()的用法和区别 简单的说就是:go(-1): 返回上一页,原页面表单中的内容会丢失:back(): 返回上一页,原页表表单中的 ...

  10. [转载]window.location.href的用法(动态输出跳转)

    无论在静态页面还是动态输出页面中window.location.href都是不错的用了跳转的实现方案   javascript中的location.href有很多种用法,主要如下. self.loca ...