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的更多相关文章

  1. 将百分制转换为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 ...

  2. [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 ...

  3. 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 ...

  4. Binary Tree和Binary Search Tree

    Binary TreeDefinition: at most two children node. Binary Tree Example: 10 ==root /      \ 13        ...

  5. [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 ...

  6. 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 ...

  7. [Algorithm] Construct String from Binary Tree

    You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...

  8. 九章算法系列(#3 Binary Tree & Divide Conquer)-课堂笔记

    前言 第一天的算法都还没有缓过来,直接就进入了第二天的算法学习.前一天一直在整理Binary Search的笔记,也没有提前预习一下,好在Binary Tree算是自己最熟的地方了吧(LeetCode ...

  9. Binary Tree Longest Consecutive Sequence

    Given a binary tree, find the length of the longest consecutive sequence path (连续的路径,不是从小到大). The pa ...

随机推荐

  1. salt 常用命令整理

    salt 常用命令整理 ***********模块*********** 查看模块列表module salt 'minion' sys.list_modules 查看指定module的function ...

  2. BASE64Decoder BASE64Encoder jar包问题

    操作 对项目右击--->build path--->configure build path---> 选中默认jre OK,操作完毕, import sun.misc.BASE64D ...

  3. 51nod1423 最大二"货" 单调栈

    枚举每个点作为次大值,用单调栈处理出左边 / 右边第一个比它大的数,直接回答 复杂度$O(n)$ #include <cstdio> #include <cstring> #i ...

  4. 51nod1376 最长上升子序列的数量

    机房的人问我树状数组怎么做这题...... 树状数组维护$len, num$表示$LIS$的长度和数量即可 复杂度$O(n \log n)$ 注:$O(n \log n)$二分+单调栈才是真神仙 具体 ...

  5. Android Studio 2.3更换默认的ConstraintLayout布局

    首先打开你的Android Sudio安装目录,我的为D:\Program Files\Android\Android Studio,进入到以下文件夹\plugins\android\lib\temp ...

  6. jquery 常用获取值得方法汇总

    jquery取radio单选按钮的值$("input[name='items']:checked").val();jquery radio取值,checkbox取值,select取 ...

  7. 解决数据库 Table 'content_tags' is marked as crashed and should be repaired 表损坏问题

    今天突然网站TAG页面打不开了,打开debug,发现提示 Table 'content_tags' is marked as crashed and should be repaired 这样的错误 ...

  8. SQL Server 2000 ——DBCC命令

    http://blog.163.com/ruifeng_00/blog/static/6904584200971291923462/   一.定义 微软当初从SYBASE将DBCC是作为数据库一致性检 ...

  9. Windows下Linux 环境 Cygwin安装及配置 基本工具使用

    用久了Ubuntu开发环境,切换到windows多少有些不爽,想在windows下享用linux便捷的工具可以安装强大的Cygwin,本人也是初次使用中学习.本文主要目的让你可以通过cygwin使用基 ...

  10. 全面的framebuffer详解一

    转:http://blog.chinaunix.net/uid-20628575-id-72534.html 一.FrameBuffer的原理 FrameBuffer 是出现在 2.2.xx 内核当中 ...