插入数值
//初始化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. 准备MyBatis

    MyBatis下载:https://github.com/mybatis/mybatis-3/releases MyBatis文件目录: 中文参考文档:http://www.mybatis.org/m ...

  2. linux常用命令:cp 命令

    cp命令用来复制文件或者目录,是Linux系统中最常用的命令之一.一般情况下,shell会设置一个别名,在命令行下复制文件时,如果目标文件已经存在,就会询问是否覆盖,不管你是否使用-i参数.但是如果是 ...

  3. 以太坊客户端Ethereum Wallet与Geth区别简介

    以太坊客户端Ethereum Wallet与Geth区别简介 最近有不少朋友在搭建交易平台,在咨询和技术交流的过程中发现很多朋友不太清楚Ethereum Wallet和Geth区别.甚至有朋友使用Ge ...

  4. vue editorConfig

    在文件目录下, indent_size = 2设置为4

  5. 使用WebClient下载网页,用正则匹配需要的内容

    WebClient是一个操作网页的类 webClient web=new  WebClient(): web.DownloadString(网页的路径,可以是本地路径);--采用的本机默认的编码格式  ...

  6. org.springframework.jdbc.UncategorizedSQLException

    org.springframework.jdbc.UncategorizedSQLException: StatementCallback; uncategorized SQLException fo ...

  7. Mysql 按天自动分区,合并老分区

    适用于每天一个分区...不断加分区,导致分区不够用的情况 CREATE DEFINER=hehe@XXXXXX PROCEDURE p_auto_partition_day(IN databaseNa ...

  8. 过滤特殊字符(包括过滤emoji表情)

    /** * 过滤特殊字符 * @param $text * @return mixed */ public static function filterSpecialChars($text) { // ...

  9. Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable) E: Unable to lock the administration di

    alexander@alexander-virtual-machine:~$ sudo apt-get install -y httpdE: Could not get lock /var/lib/d ...

  10. div容器中内容垂直居中

    #box{ width:200px; height:200px; line-height: 200px; vertical-align: middle; margin: 5px; background ...