自己尝试用js实现了数据结构的二叉查找树。

// node
function Node(data) {
this.data = data;
this.lc = null;
this.rc = null;
} // BST
function BST() {
this.root = null;
}
//======================create root node======================
BST.prototype.create = function(num) {
this.root = new Node(num);
}
// ======================add tree node=========================
BST.prototype.insert = function(root, num) {
var node = new Node(num); if(root.data < node.data) {
if(!root.rc)
root.rc = node;
else
this.insert(root.rc, num);
} else {
if(!root.lc)
root.lc = node;
else
this.insert(root.lc, num);
}
} var bst = new BST();
var arr = [5,3,6,7,4,1,8]; // create root node
bst.create(arr[0]);
// create tree
for(var i = 1; i < arr.length; i++) {
bst.insert(bst.root, arr[i]);
}

console.log(bst.root);

第二种

// node
function Node(data) {
this.data = data;
this.lc = null;
this.rc = null;
} // BST
function BST() {
this.root = null;
} // add tree node
BST.prototype.insert = function(num, bst) {
var node = new Node(num);
var isRootTree = (bst && bst.hasOwnProperty('root')); // 判断传入的是一棵树还是一个节点
var root = isRootTree ? bst.root : bst; if(isRootTree && !root) { // 如果传入的参数为树且该树的root为null
bst.root = node; // 初始化root,不能用root = node,
// 因为这样不会改变bst.root,而是另root变量重新指向了一个新node节点
} else {
var target = root.data < num ? 'rc' : 'lc'; // 避免bst为null带来的error
root[target] == null ? root[target] = node : this.insert(num, root[target]);
}
}; var bst = new BST();
var arr = [5,9,6,7,4,1,8]; for(var i = 0; i < arr.length; i++) {
bst.insert(arr[i], bst);
}
console.log(bst.root);

第三种  通过迭代

// node
function Node(data) {
this.data = data;
this.left = null;
this.rifht = null;
} // BST
function BST() {
this.root = null;
}
BST.prototype.insert = function(data){
var node = new Node(data, null, null);
if(this.root == null){
this.root = node;
}
else{
var currNode = this.root;
var parent; while(true){
parent = currNode;
if(data < currNode.data){
currNode = currNode.left;
if(currNode == null){
parent.left = node;
break;
}
}
else{
currNode = currNode.right;
if(currNode == null){
parent.right = node;
break;
}
}
}
}
}; BST.prototype.orderVisit = function(root) {
if(root) {
this.orderVisit(root.left);
console.log(root.data);
this.orderVisit(root.right);
}
}; var arr = [5,4,6,3,7,2,8];
var bst = new BST(); for(var i = 0; i < arr.length; i++)
bst.insert(arr[i]); bst.orderVisit(bst.root);

JavaScript之BST的更多相关文章

  1. 数据结构之二叉搜索树(BST)--JavaScript实现

    原理: 叉排序树的查找过程和次优二叉树类似,通常采取二叉链表作为二叉排序树的存储结构.中序遍历二叉排序树可得到一个关键字的有序序列,一个无序序列可以通过构造一棵二叉排序树变成一个有序序列,构造树的过程 ...

  2. javascript数据结构与算法-- 二叉树

    javascript数据结构与算法-- 二叉树 树是计算机科学中经常用到的一种数据结构.树是一种非线性的数据结构,以分成的方式存储数据,树被用来存储具有层级关系的数据,比如文件系统的文件,树还被用来存 ...

  3. 第一章:javascript: 数据结构与算法

    在前端工程师中,常常有一种声音,我们为什么要学数据结构与算法,没有数据结构与算法,我们一样很好的完成工作.实际上,算法是一个宽泛的概念,我们写的任何程序都可以称为算法,甚至往冰箱里放大象,也要通过开门 ...

  4. JavaScript数据结构——树

    树:非顺序数据结构,对于存储需要快速查找的数据非常有用. 二叉树:二叉树中的节点最多只能有两个子节点(左侧子节点和右侧子节点).这些定义有助于我们写出更高效的向/从树中插入.查找和删除节点的算法. 二 ...

  5. JavaScript数据结构 (手打代码)

    array: 数组创建: ); //创建一个长度为6的数组 ,,,,,); 数组方法: var str="I love javascript"; var single=str.sp ...

  6. JavaScript的数据结构和算法

    所有JavaScript对象都有hasOwnProperty(value)的方法,用来返回一个表明对象是不是具有这个value Key值属性的布尔值. javaScript的方法 具有delete的方 ...

  7. JavaScript笔记 #07# 用js写算法

    算法盒子初代(为了提高学习算法的热情...) 效果图: 所有代码放在单个html中: <!DOCTYPE html> <html> <head> <meta ...

  8. javascript数据结构与算法---二叉树(删除节点)

    javascript数据结构与算法---二叉树(删除节点) function Node(data,left,right) { this.data = data; this.left = left; t ...

  9. javascript数据结构与算法---二叉树(查找最小值、最大值、给定值)

    javascript数据结构与算法---二叉树(查找最小值.最大值.给定值) function Node(data,left,right) { this.data = data; this.left ...

随机推荐

  1. display:inline-block引发的间隙思考

    一.导火线 没错,总有一类属性在助你轻松寻得捷径的同时,也可为你增添烦劳,比如本文的主谋display:inline-block.众前端们所诸知,其作用是将对象呈递为内联对象,但是对象的内容作为块对象 ...

  2. Django学习日记07_Admin

    django.contrib django.contrib是django中附带的一个工具集,由很多的附加组件组成.这些附加组件包括管理工具(django.contrib.admin).用户鉴别系统(d ...

  3. Ascall 码特殊字符——去除从windows上传文件的^M

    在windows上编辑过的文件如果传到unix上,在每个文件的末尾都会有一个换行控制符^M,这个字符一般处于隐藏状态,除非cat -A才能看到,如果不去掉这个符号,很多脚本不能正常运行,很多文件不能正 ...

  4. primer漏配问题解决

    在对之前的ITS数据(454数据)做split时,发现有一些reads没有被匹配上,但是barcode能够完全匹配,虽然之后的primer在中间漏了一个碱基,导致后面的碱基全部误匹配,从而导致这条re ...

  5. 腾讯WeTest发布《2017中国移动游戏质量白皮书》,专注手游品质提升

    1月8日,腾讯质量开放平台WeTest正式发布<2017中国移动游戏质量白皮书>. 刚刚过去的这一年,市场逐渐成熟,中国移动互联网由增量市场转向存量市场.中国移动游戏市场急剧变化,真正的精 ...

  6. window.btoa 和 window.atob

    前一段时间被安全部门查出,明文传递密码,被要求整改. 然后就进行了引入了第三方的base64编码的js库,进行了编码然后传递. 其实在前端的加密都是寻求一个心理安慰,作用是微乎其微的,确实也更加好那么 ...

  7. adb 获取Android手机信息命令(2)

    #Android命令 #获取手机名称 GET_PHONE_NAME = 'adb shell getprop ro.product.model' #获取手机版本 GET_PHONE_VERSION = ...

  8. cassandra的gc调优

    我们用的是cassandra3.7官方的docker镜像,在生产环境发现有一个小时一次停顿的现象.我猜测是java gc的原因,于是看了cassandra的gc日志,果然发现有每小时长达300ms-2 ...

  9. 6.while loop

    while 循环   有时候我们不确定需要循环几次.就像一个司机不知道自己需要什么时候加油一样.程序可以这样写:   while petrol_filling: increase price show ...

  10. 使用socket实现的ftp文件传输服务器

    服务端: # encoding:utf-8 # Author:"richie" # Date:8/23/2017 from socket import * import pickl ...