[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 ...
随机推荐
- javascript面向对象思想
JavaScript 使用函数来定义类.语法:function className(){ // 具体操作} function Person() { this.name=" 张三 &qu ...
- python3 django 安装
参考https://www.cnblogs.com/yuyang26/p/7411269.html 前提条件:python3.x环境 windows 步骤1 pip install Django==2 ...
- HOJ 2645 WNim sg函数 博弈论
http://blog.csdn.net/y1196645376/article/details/52165245 这道题我没有写,因为我就算翻译了我也找不到数据范围,也分不清数据变量的命名,而且ho ...
- 【BJOI2014】大融合【LCT】
闲着没事写篇题解 传送门 LCT维护子树的模板题 树链剖分中,子树可以用dfs序维护.但LCT你总不可能动态维护dfs序啊 LCT之所以不能直接维护子树,是因为LCT只能维护它的重儿子.我们把这棵子树 ...
- bzoj 1776
收获: 树上直径一定包含深度最深的点. 然后O(nlogn) 暴力. /************************************************************** P ...
- HDU 4612 Warm up tarjan 树的直径
Warm up 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4612 Description N planets are connected by ...
- hdu 4112 Break the Chocolate 贪心
Break the Chocolate Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem. ...
- MongoDB基础学习(一) MongoDB概念解析
.基础概念 SQL术语/概念 MongoDB术语/概念 说明 database database 数据库 table collection 数据表/集合 row document 数据记录行/文档 c ...
- 为什么要使用netty
选择Netty的理由在开始本节之前,我先讲一个亲身经历的故事:曾经有两个项目组同时用到了NIO编程技术,一个项目组选择自己开发NIO服务端,直接使用JDK原生的API,结果2个多月过去了,他们的NIO ...
- 原生+H5开发之:js交互【location方式】
1. 交互方式总结 1Android与JS通过WebView互相调用方法,实际上是: Android去调用JS的代码 JS去调用Android的代码 二者沟通的桥梁是WebView 对于Android ...