<!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. Oracle EBS R12多组织访问架构

    关于R12的新特性Multi-Org Access Control(MOAC).Oracle宣传的好处主要有:1.enable users to access to secured data in o ...

  2. Ubuntu 16.04.2 LTS 安装 jdk1.6 和 tomcat6 (二)

    上一篇记录和分享了jdk1.6 在Ubuntu 16.04.2 环境下的安装配置,本文开始安装和配置tomcat 6     2 安装tomcat   http://tomcat.apache.org ...

  3. (zxing.net)一维码UPC A的简介、实现与解码

    一.简介 UPC(Universal Product Code)码是最早大规模应用的条码,其特性是一种长度固定.连续性的条  码,目前主要在美国和加拿大使用,由于其应用范围广泛,故又被称万用条码. U ...

  4. JAVA—编码问题

    一.编码.(引用  百度百科) 编码是信息从一种形式或格式转换为另一种形式的过程也称为计算机编程语言的代码简称编码.用预先规定的方法将文字.数字或其它对象编成数码,或将信息.数据转换成规定的电脉冲信号 ...

  5. 函数IsValid()

    功能:检查对象变量是否已经实例化,即实例变量的值是否是个有效的对象句柄. 语法:IsValid(objectname) 参数:objectname:要检查的对象名. 返回值:Boolean.如果指定对 ...

  6. Verify the Developer App certificate for your account is trusted on your device.

    1.报错内容 Could not launch "CH5203" Verify the Developer App certificate for your account is ...

  7. H - Birthday Paradox (生日悖论)

    点击打开链接 Sometimes some mathematical results are hard to believe. One of the common problems is the bi ...

  8. jQuery中animate与scrollTop、offset().top实例

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. “全栈2019”Java第九十二章:外部类与内部类成员覆盖详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  10. java下载文件注意点

    前台: 不建议使用ajax,可以使用window.location.href 后台: 三个参数--> response path filename--filename如果要防止乱码,可以用Str ...