JavaScript之BST
自己尝试用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的更多相关文章
- 数据结构之二叉搜索树(BST)--JavaScript实现
原理: 叉排序树的查找过程和次优二叉树类似,通常采取二叉链表作为二叉排序树的存储结构.中序遍历二叉排序树可得到一个关键字的有序序列,一个无序序列可以通过构造一棵二叉排序树变成一个有序序列,构造树的过程 ...
- javascript数据结构与算法-- 二叉树
javascript数据结构与算法-- 二叉树 树是计算机科学中经常用到的一种数据结构.树是一种非线性的数据结构,以分成的方式存储数据,树被用来存储具有层级关系的数据,比如文件系统的文件,树还被用来存 ...
- 第一章:javascript: 数据结构与算法
在前端工程师中,常常有一种声音,我们为什么要学数据结构与算法,没有数据结构与算法,我们一样很好的完成工作.实际上,算法是一个宽泛的概念,我们写的任何程序都可以称为算法,甚至往冰箱里放大象,也要通过开门 ...
- JavaScript数据结构——树
树:非顺序数据结构,对于存储需要快速查找的数据非常有用. 二叉树:二叉树中的节点最多只能有两个子节点(左侧子节点和右侧子节点).这些定义有助于我们写出更高效的向/从树中插入.查找和删除节点的算法. 二 ...
- JavaScript数据结构 (手打代码)
array: 数组创建: ); //创建一个长度为6的数组 ,,,,,); 数组方法: var str="I love javascript"; var single=str.sp ...
- JavaScript的数据结构和算法
所有JavaScript对象都有hasOwnProperty(value)的方法,用来返回一个表明对象是不是具有这个value Key值属性的布尔值. javaScript的方法 具有delete的方 ...
- JavaScript笔记 #07# 用js写算法
算法盒子初代(为了提高学习算法的热情...) 效果图: 所有代码放在单个html中: <!DOCTYPE html> <html> <head> <meta ...
- javascript数据结构与算法---二叉树(删除节点)
javascript数据结构与算法---二叉树(删除节点) function Node(data,left,right) { this.data = data; this.left = left; t ...
- javascript数据结构与算法---二叉树(查找最小值、最大值、给定值)
javascript数据结构与算法---二叉树(查找最小值.最大值.给定值) function Node(data,left,right) { this.data = data; this.left ...
随机推荐
- HBase资料
http://blog.csdn.net/ymh198816/article/details/51244911 https://www.cnblogs.com/JingJ/p/4521245.html ...
- 在IIS使用localDB
项目使用localdb来作为本机测试数据库,发布到本机IIS后项目却链接不到数据库,查看windows日志为如下错误 "无法获取本地应用程序数据路径.很可能是因为未加载用户配置文件.如果在 ...
- [js高手之路]html5 canvas教程 - 1px问题以及绘制坐标系网格
在canvas中,要画出1px的线条,默认情况下是不行的 context.beginPath(); context.moveTo( 100, 100 ); context.lineTo( 400, 1 ...
- XSS Challenges闯关笔记
前言 做xss做疯了再来一个. 地址:https://xss-quiz.int21h.jp/ ,这个貌似是日本的一个安全研究员yamagata21做的. 做到第九关就跪了,而总共有二十关.一半都还没有 ...
- [ERROR] Terminal initialization failed; falling back to unsupported java.lang.IncompatibleClassChangeError: Found class jline.Terminal, but interface was expected
1:出现此种错误应该是jar版本包冲突了,启动hive的时候,由于hive依赖hadoop,启动hive,会将hadoop的配置以及jar包等等导入到hive中,导致jar包版本冲突,下面贴一下错误, ...
- Redis 部署主从哨兵 C#使用,实现自动获取redis缓存 实例2
资料查找https://www.cnblogs.com/tdws/p/5836122.html https://www.cnblogs.com/lori/p/5794454.html private ...
- HTTP2.0和QUIC
最近看到腾讯云支持QUIC的文章,突然意识到还没有好好认识HTTP2.QUIC,而要认识HTTP2,就需要从HTTP1.0开始讲起,才能清楚HTTP的发展历程. HTTP1.x HTTP(HyperT ...
- [转] Web 开发模式演变历史和趋势
文章转自梦想天空--前端文摘:Web 开发模式演变历史和趋势 一.简单明快的早期时代 可称之为 Web 1.0 时代,非常适合创业型小项目,不分前后端,经常 3-5 人搞定所有开发.页面由 JSP.P ...
- contain_of宏定义
Container_of在Linux内核中是一个常用的宏,用于从包含在某个结构中的指针获得结构本身的指针,通俗地讲就是通过结构体变量中某个成员的首地址进而获得整个结构体变量的首地址. 实现方式: co ...
- #pta循环作业
7-7 计算阶乘和 1.题目 . 2.设计思路 此题目比一道类似的经典题目的区别就是不是直接的数字累加而是每次的数字先累乘之后再累加 只需要在累加之前处理一下所要加的数字就可以实现了 3.流程 ...