[Algorithm] Construct a Binary Tree and Binary Search
function createNode(value) {
return {
value,
left: null,
right: null
};
}
function BinaryTree(val) {
return {
root: null,
nodes: [],
add(val) {
const node = createNode(val);
if (!this.root) {
this.root = node;
} else {
this.downShift(node);
}
this.nodes.push(node);
},
downShift(node) {
let value = node.value;
let current = this.root;
while (current) {
if (value > current.value) {
if (!current.right) {
current.right = node;
break;
} else {
current = current.right;
}
} else {
if (!current.left) {
current.left = node;
break;
} else {
current = current.left;
}
}
}
},
size() {
return this.nodes.length;
},
search(target) {
let found = false;
let current = this.root;
while (current) {
if (target > current.value) {
if (!current.right) {
return "Not Found";
}
current = current.right;
} else if (target < current.value) {
if (!current.left) {
return "Not Found";
}
current = current.left;
} else {
found = true;
break;
}
}
return found;
}
};
}
const t = new BinaryTree();
t.add();
t.add();
t.add();
t.add();
t.add();
t.add();
t.add();
console.log(t.search());
About how to traverse binary tree, can refer this post.
[Algorithm] Construct a Binary Tree and Binary Search的更多相关文章
- 将百分制转换为5分制的算法 Binary Search Tree ordered binary tree sorted binary tree Huffman Tree
1.二叉搜索树:去一个陌生的城市问路到目的地: for each node, all elements in its left subtree are less-or-equal to the nod ...
- [Algorithm] Check if a binary tree is binary search tree or not
What is Binary Search Tree (BST) A binary tree in which for each node, value of all the nodes in lef ...
- What is the difference between a binary tree, a binary search tree, a B tree and a B+ tree?
Binary Tree : It is a tree data structure in which each node has at most two children. As such there ...
- Binary Tree和Binary Search Tree
Binary TreeDefinition: at most two children node. Binary Tree Example: 10 ==root / \ 13 ...
- [Leetcode][JAVA] Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
- 33. Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree
Minimum Depth of Binary Tree OJ: https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ Give ...
- [Algorithm] Construct String from Binary Tree
You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...
- 九章算法系列(#3 Binary Tree & Divide Conquer)-课堂笔记
前言 第一天的算法都还没有缓过来,直接就进入了第二天的算法学习.前一天一直在整理Binary Search的笔记,也没有提前预习一下,好在Binary Tree算是自己最熟的地方了吧(LeetCode ...
- Binary Tree Longest Consecutive Sequence
Given a binary tree, find the length of the longest consecutive sequence path (连续的路径,不是从小到大). The pa ...
随机推荐
- PHP的数据类型与常量使用
数据类型之间的相互转换 1.强制转换:setType(变量,类型):这个函数将原变量的类型改变//integer,int,float,double,bool,boolen,string,arry,ob ...
- HDU 6119 小小粉丝度度熊(Two pointers)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6119 [题目大意] 给出一些签到区间和一些补签卡,问可以创造的最长连续签到区间 [题解] 如果我们 ...
- <form> 标签 // HTML 表单 // from 表单转换成json 格式
<form> 标签 // HTML 表单 // from 表单转换成json 格式 form 表单,对开发人员来说是在熟悉不过的了,它是页面与web服务器交互时的重要信息来源 表 ...
- 一文了解JVM全部垃圾回收器,从Serial到ZGC
<对象搜索算法与回收算法>介绍了垃圾回收的基础算法,相当于垃圾回收的方法论.接下来就详细看看垃圾回收的具体实现. 上文提到过现代的商用虚拟机的都是采用分代收集的,不同的区域用不同的收集器. ...
- bzoj 1017 tree dp
这道题几经波折啊. 最开始和vfleaking一样,把题意理解错了,认为一个装备可能被多个装备依赖,然后想不出来,去看题解. 发现自己理解错了题意,自己想想,其实也不难想到dp[i][j][k]表示“ ...
- Unity 播放音频文件
Unity 播放音频文件参考代码: public void Play(string strSoundName, float autoDestroyTime = 0f, bool bLoop = fal ...
- 02-MariaDB主从安装SpringBoot整合MyBatis配置
关于MariaDB的介绍 MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可 MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能轻松成为My ...
- JDK源码(1.7) -- java.util.Map<K,V>
java.util.Map<K,V> 源码分析 --------------------------------------------------------------------- ...
- [转]android系统各种音量的获取与设置
获取系统音量 通过程序获取android系统手机的铃声和音量.同样,设置铃声和音量的方法也很简单! 设置音量的方法也很简单,AudioManager提供了方法:publicvoidsetStreamV ...
- CentOS 6.9开启iptables的日志实现调试
系统日志配置在CentOS 5上叫syslog,而在CentOS 6上叫rsyslog(增强版的syslog),CentOS 5上的配置文件在/etc/syslog.conf下,而CentOS 6在/ ...