插入数值
//初始化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. Unity 异步加载进度条

    public class View_LoadingScene : MonoBehaviour { //场景加载进度条对象 public GameObject loadingProgressBar; / ...

  2. KVM_webvirtmgr

    一.webvirtmgr安装前说明: 1:操作做系统:centos7.2_x86_64 2:安装参考出处1:https://github.com/retspen/webvirtmgr/wiki/Ins ...

  3. python基础之可变数据类型与不可变数据类型

    一.什么可变数据类型和不可变数据类型 可变数据类型:value值改变,id值不变:不可变数据类型:value值改变,id值也随之改变. 二.如何确定一种数据类型是可变的还是不可变的 根据可变数据类型与 ...

  4. c# 规范用户输入控件

    MaskedTextBox控件是一种特殊的文本框,它可以通过Mask属性设置格式标记符.在应用程序运行后,用户只能输入Mask属性允许的内容,列入日期.电话等 在“输入掩码”对话框的右下角有一个“使用 ...

  5. Node.js中环境变量process.env详解

    Node.js中环境变量process.env详解process | Node.js API 文档http://nodejs.cn/api/process.html官方解释:process 对象是一个 ...

  6. 找不到命令 ifconfig

    centos 7中自带的查看网络的命令是: ip addr 如果还是想要 ifconfig 安装net-tools yum install net-tools

  7. 监控网卡流量脚本(Python)

    #!/usr/bin/env python# coding: utf-8# author: Xiao Guaishou try:    import psutilexcept ImportError: ...

  8. MediaCodec在Android视频硬解码组件的应用

    https://yq.aliyun.com/articles/632892 云栖社区> 博客列表> 正文 MediaCodec在Android视频硬解码组件的应用   cheenc 201 ...

  9. xtrabackup 对pxc节点进行备份恢复

    xtrabackup 对pxc节点进行备份恢复 全量备份一个节点的数据,当节点挂掉时,使用备份恢复到最近状态,再启动节点加入集群. 备份 xtrabackup 命令小解释: --defaults-fi ...

  10. FileReader 获取图片base64数据流 并 生成图片

    <?php if(isset($_GET['upload']) && $_GET['upload'] == 'img'){ if(isset($_GET['stream_type ...