<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>二叉查找树计数</title>
</head>
<body>
<script>
function Node(data,left,right){
this.data = data;
this.count = 1;
this.left = left;
this.right = right;
this.show = show;
} function show(){
return this.data;
} function BST(){
this.update = update;
this.root = null;
this.insert = insert;
this.inOrder = inOrder;
this.preOrder = preOrder;
this.postOrder = postOrder;
this.getMin = getMin;
this.getMax = getMax;
this.find = find;
this.remove = remove;
} //不存在存进去 存在计数加1
function insert(data){
if(this.find(data)){
this.find(data).count++;
return 2;
}
var n = new Node(data,null,null);
if(this.root == null ){
this.root = n;
console.log(this.root,"insert");
}else{
var current = this.root;
var parent;
while(true){
parent = current;
if(data < current.data){
current = current.left; if(current == null ){
parent.left = n;
break;
}
}else{
current = current.right;
if(current == null ){
parent.right = n;
break;
}
}
}
}
}
//中序遍历
function inOrder(node){
if(node !=null){
inOrder(node.left);
console.log(node.show(),"zhongxu")
inOrder(node.right);
}
}
//先序遍历
function preOrder(node){
if(node !=null){
console.log(node.show());
preOrder(node.left);
preOrder(node.right);
}
} //后序遍历
function postOrder(node){
if(node != null){
postOrder(node.left);
postOrder(node.right);
console.log(node.show());
}
}
// 查找最小值
function getMin(){
var current = this.root;
while (current.left != null){
current = current.left;
}
return current.data;
}
//查找最大值
function getMax(){
var current = this.root;
while(current.right != null){
current = current.right;
}
return current.data;
} //查找给定值
function find(data){
var current = this.root;
while(current != null){
if(data == current.data){
return current;
}else if(data>current.data){
current = current.right;
}else{
current = current.left;
}
}
return null;
} //删除节点 function remove(data){
root = removeNode(this.root,data);
}
function removeNode(node,data){
if(node == null){
return null;
}
if(data == node.data){
//没有子节点的节点
if(node.left == null && node.right == null){
return null;
}
//没有左子节点
if(node.left == null){
return node.right;
}
//没有右子节点
if(node.right == null){
return node.left;
}
//有两个子节点的节点
var temp = getSmall(node.right);
node.data = temp.data;
node.right = removeNode(node.right,temp.data);
return node;
}else if(data < node.data){
node.left = removeNode(node.left,data);
return node;
}else{
node.right = removeNode(node.right,data);
return node;
}
}
//获取最小值
function getSmall(node){
console.log("123456")
if(node.left ==null && node.right == null ){
return node;
}
while(node.left !=null ){
node = node.left;
}
return node;
} //更新计数
function update(data){
var grade = this.find(data);
grade.count++;
return grade;
}
var obj = new BST();
obj.insert(15);
obj.insert(22);
console.log(obj.insert(22),"计数");
obj.insert(9);
obj.insert(30);
obj.inOrder(obj.root);
obj.preOrder(obj.root);
obj.postOrder(obj.root);
console.log(obj.getMin(),"最小值");
console.log(obj.getMax(),"最大值"); console.log(obj.find(22)); obj.remove(15);
obj.inOrder(obj.root);
</script>
</body>
</html>

JavaScript数据结构-16.二叉树计数的更多相关文章

  1. JavaScript数据结构-15.二叉树

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

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

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

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

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

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

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

  5. javascript数据结构与算法--二叉树遍历(后序)

    javascript数据结构与算法--二叉树遍历(后序) 后序遍历先访问叶子节点,从左子树到右子树,再到根节点. /* *二叉树中,相对较小的值保存在左节点上,较大的值保存在右节点中 * * * */ ...

  6. javascript数据结构与算法--二叉树遍历(先序)

    javascript数据结构与算法--二叉树遍历(先序) 先序遍历先访问根节点, 然后以同样方式访问左子树和右子树 代码如下: /* *二叉树中,相对较小的值保存在左节点上,较大的值保存在右节点中 * ...

  7. javascript数据结构与算法--二叉树遍历(中序)

    javascript数据结构与算法--二叉树遍历(中序) 中序遍历按照节点上的键值,以升序访问BST上的所有节点 代码如下: /* *二叉树中,相对较小的值保存在左节点上,较大的值保存在右节点中 * ...

  8. javascript数据结构与算法--二叉树(插入节点、生成二叉树)

    javascript数据结构与算法-- 插入节点.生成二叉树 二叉树中,相对较小的值保存在左节点上,较大的值保存在右节点中 /* *二叉树中,相对较小的值保存在左节点上,较大的值保存在右节点中 * * ...

  9. JavaScript 数据结构与算法之美 - 桶排序、计数排序、基数排序

    1. 前言 算法为王. 想学好前端,先练好内功,只有内功深厚者,前端之路才会走得更远. 笔者写的 JavaScript 数据结构与算法之美 系列用的语言是 JavaScript ,旨在入门数据结构与算 ...

随机推荐

  1. B-spline Curves 学习之B样条曲线的系数计算与B样条曲线特例(6)

    B-spline Curves: Computing the Coefficients 本博客转自前人的博客的翻译版本,前几章节是原来博主的翻译内容,但是后续章节博主不在提供翻译,后续章节我在完成相关 ...

  2. handsontable-cell features

    数据验证:在columns中设置validator,进行同步或异步验证,还可添加beforeValidate, afterValidate函数,allowInvalid:true可以不用验证 var ...

  3. Delphi获取文件名、文件名不带扩展名、文件名的方法;delphi 获取文件所在路径

    取文件名 ExtractFileName(FileName); 取文件扩展名: ExtractFileExt(filename); 取文件名,不带扩展名: 方法一:   Function Extrac ...

  4. FMX.Platform.TApplicationEvent

    FMX.Platform.TApplicationEvent http://docwiki.embarcadero.com/Libraries/Seattle/en/FMX.Platform.TApp ...

  5. Intellij IDEA如何在一个窗口同时打开多个Maven项目

    建立父目录,比如fatherProject,并将多个项目放入该父目录fatherProject下 File-Open...打开父目录fatherProject 引入pom.xml,打开Maven Pr ...

  6. Math类的三个方法比较: floor() ceil() round()

    public class Test { public static void main(String[] args) { double d1 = 3.4, d2 = 3.6; //正数 double ...

  7. TSQL--NULL值和三值逻辑

    在SQL SERVER 中逻辑表达式存在三种值:TRUE+FALSE+UNKNOWN.UNKNOW可以理解为不确定,既不是TRUE又不是FALSE的表达式,主要由与NULL相关的逻辑判断引起,值为NU ...

  8. 如何让Gogland不过期,一直使用?

    Gogland是jetBrains公司出品的GO语言开发IDE,是目前最好的GO语言开发工具!!但是目前Gogland提供的试用版,有一定的使用期限,如何到期还能使用?经过我的测试,如果Gogland ...

  9. clickonce联机模式

    发布时选择该程序只能联机使用,这样本地就不会进行安装. 参考地址:https://blog.csdn.net/dqs78833488/article/details/52513948

  10. TPshop商城 Getshell后门文件分析与复现

    本文作者:i春秋签约作家——Laimooc 官网地址:http://www.tp-shop.cn/ 影响版本:TPSHOP v2.0.0 后门文件:eval-stdin.php 内容: <?ph ...