一、树的相关概念

1.基本概念

子树

一个子树由一个节点和它的后代构成。

节点的度

节点所拥有的子树的个数。

树的度

树中各节点度的最大值

节点的深度

节点的深度等于祖先节点的数量

树的高度

树的高度等于所有节点深度的最大值

森林

若干课互不相交的树的集合。任何一棵树,删去根节点就变成了森林。

2. 二叉树

二叉树的定义

(1)二叉树中每个节点的度不大于2

(2)二叉树是有序的,其子树有左右之分,其次序不能随意颠倒

二叉树的性质

  • 第k层上最多有2^(k-1)个节点
  • 深度为k的二叉树最多有 2^k-1 个节点

3. 几种特殊的二叉树

满二叉树

深度为k且有2^k-1个节点的二叉树

完全二叉树

有n个节点的二叉树,当且仅当其每个节点按从上到下、从左至右的顺序进行编号,其编号与满二叉树中1至n的节点编号一一对应

  • 有n个节点的完全二叉树中最后一个有子节点的节点的序号:Math.floor(n/2-1)

二叉搜索树

二叉树的一种,但是它只允许左子节点存储比父节点小的值,右子节点存储比父节点大的值。这是本文要研究的数据结构

2.二叉搜索树及相关方法的js实现代码

function BinarySearchTree() {

  //Node类表示树中的节点
const Node = function(key) {
this.key = key;
this.left = null;
this.right = null;
}; // 变量root表示根节点
let root = null; //向树中插入一个节点
this.insert = function(key) {
const newNode = new Node(key);
const insertNode = function(node, newNode) {
if (newNode.key < node.key) {
if (node.left === null) {
node.left = newNode;
} else {
insertNode(node.left, newNode);
}
} else {
if (node.right === null) {
node.right = newNode;
} else {
insertNode(node.right, newNode);
}
}
}
if (root === null) {
root = newNode;
} else {
insertNode(root, newNode);
}
}; //先序遍历:根左右
this.preOrderTraverse = function(callback) {
const preOrderTraverseNode = function(node, callback) {
if (node !== null) {
callback(node.key);
preOrderTraverseNode(node.left, callback);
preOrderTraverseNode(node.right, callback);
}
}
preOrderTraverseNode(root, callback) }; //中序遍历: 左根右
this.inOrderTraverse = function(callback) {
const inOrderTraverseNode = function (node, callback) {
if (node !== null) {
inOrderTraverseNode(node.left, callback);
callback(node.key);
inOrderTraverseNode(node.right, callback);
}
}
inOrderTraverseNode(root, callback);
}; //后序遍历:左右根
this.postOrderTraverse = function(callback) {
const postOrderTraverseNode = function(node, callback) {
if (node !== null) {
postOrderTraverseNode(node.left, callback);
postOrderTraverseNode(node.right, callback);
callback(node.key);
}
};
postOrderTraverseNode(root, callback);
}; //寻找树中的最小值,并返回这个最小值
this.min = function () {
const minNode = function(node) {
if (node) {
while (node.left !== null) {
node = node.left;
}
return node.key
}
return null;
};
return minNode(root);
}; //寻找树中的最大值,病返回这个最大值
this.max = function() {
const maxNode = function(node) {
if (node) {
while (node.right !== null) {
node = node.right;
}
return node.key;
}
return null;
};
return maxNode(root);
} //寻找树中是否存在一个特定值,存在返回true,不存在返回false
this.search = function(key) {
const searchNode = function(node, key) {
if (node === null) {
return false;
} if (key < node.key) {
return searchNode(node.left, key);
} else if (key > node.key) {
return searchNode(node.right, key);
} else {
return true;
}
} return searchNode(root, key);
}; //从树中移除一个节点
this.remove = function(key) {
const findMinNode = function(node) { //与方法min中的minNode不同的是,minNode返回node.key,它返回node本身
while (node.left !== null) {
node = node.left;
}
return node;
};
const removeNode = function(node, key) {
if (node === null) {
return null;
} if (key < node.key) { //这种情况需要更新node.left,然后返回更新了node.left的新的node
node.left = removeNode(node.left, key);
return node;
} else if (key > node.key) { //这种情况需要更新node.right,然后返回更新了node.right的新的node
node.right = removeNode(node.right, key);
return node;
} else { //这种情况需要更新node.key或者其他更新手段(包括直接将node变为null, 或更新node.right),返回的也是更新后的node //情况1,被移除的是叶子节点
if (node.left === null && node.right === null) {
node = null;
return node;
} //情况2,被移除的是只有一个子节点的节点
if (node.left === null) { //只有右子节点
node = node.right;
return node;
} else if (node.right === null) {//只有左子节点
node = node.left;
return node;
} //情况3, 被移除的是有两个子节点的节点
const aux = findMinNode(node.right);//找到子树中的最小节点,它肯定是一个叶子节点
node.key = aux.key;//更新node的key //node.left不变
//node.right要删除上述aux节点
node.right = removeNode(node.right, aux.key);//更新node.right,这里其实是移除了一个以node.right为root的树的叶子节点 return node;
}
}; root = removeNode(root, key);
};
}

参考资料

-《学习JavaScript数据结构和算法》Chapter8

-《数据结构(C语言版)》Chapter6

数据结构-二叉搜索树的js实现的更多相关文章

  1. 数据结构-二叉搜索树(BST binary search tree)

    本文由@呆代待殆原创,转载请注明出处:http://www.cnblogs.com/coffeeSS/ 二叉搜索树简介 顾名思义,二叉搜索树是以一棵二叉树来组织的,这样的一棵树可以用一个链表数据结构来 ...

  2. 数据结构☞二叉搜索树BST

    二叉查找树(Binary Search Tree),(又:二叉搜索树,二叉排序树)它可以是一棵空树,也可以是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值: 若它 ...

  3. 基本数据结构 —— 二叉搜索树(C++实现)

    目录 什么是二叉搜索树 二叉搜索树如何储存数值 二叉搜索树的操作 插入一个数值 查询是否包含某个数值 删除某个数值 测试代码 参考资料 什么是二叉搜索树 二叉搜索树(英语:Binary Search ...

  4. 数据结构---二叉搜索树BST实现

    1. 二叉查找树 二叉查找树(Binary Search Tree),也称为二叉搜索树.有序二叉树(ordered binary tree)或排序二叉树(sorted binary tree),是指一 ...

  5. 数据结构-二叉搜索树和二叉树排序算法(python实现)

    今天我们要介绍的是一种特殊的二叉树--二叉搜索树,同时我们也会讲到一种排序算法--二叉树排序算法.这两者之间有什么联系呢,我们一起来看一下吧. 开始之前呢,我们先来介绍一下如何创建一颗二叉搜索树. 假 ...

  6. 数据结构 - 二叉搜索树封装 C++

    二叉搜索树封装代码 #pragma once #include <iostream> using namespace std; template<class T>class T ...

  7. Java数据结构——二叉搜索树

    定义二叉查找树(Binary Search Tree),(又:二叉搜索树,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值: 若 ...

  8. 数据结构——二叉搜索树(Binary Search Tree)

    二叉树(Binary Tree)的基础下 每个父节点下 左节点小,右节点大. 节点的插入: 若root==NULL则root=newnode 否则不断与节点值比较,较小则向左比较,较大则向右比较. 完 ...

  9. 数据结构-二叉搜索树Java实现

    1,Node.java 生成基础二叉树的结构 package com.cnblogs.mufasa.searchTree; /** * 节点配置父+左+右 */ public class Node{ ...

随机推荐

  1. TFS中工作项的定制- 字段功能定义

    参考,翻译此页面All FIELD XML Elements Reference(http://msdn.microsoft.com/en-us/library/ms194953.aspx) 对于每一 ...

  2. cocos2dx使用cocostudio导出的animation

    local uilocal function createLayerUI() if not ui then ui=cc.Layer:create(); createLayerUI=nil; end r ...

  3. spring AOP操作

    在spring进行AOP操作,使用aspectj实现 一.aspectj准备 aspectj不是spring的一部分,和spring一起使用进行AOP的操作 1.除了spring基本的jar包还需要导 ...

  4. R语言图形base系统(二)

    x<-c(1:10) y<-x z<-10/x opar<-par(no.readonly = T) par(mar=c(5,4,4,8)+0.1) plot(x,y,type ...

  5. Python:笔记(3)——面向对象编程

    Python:笔记(3)——面向对象编程 类和面向对象编程 1.类的创建 说明:和Java不同的是,我们不需要显示的说明类的字段属性,并且可以在后面动态的添加. 2.构造函数 构造函数的功能毋庸置疑, ...

  6. pandas.resample()

    http://www.cnblogs.com/hhh5460/p/5596340.html resample与groupby的区别:resample:在给定的时间单位内重取样groupby:对给定的数 ...

  7. eclipse新建Maven项目

    1.在eclipse中安装maven插件 2.点击File->new->maven project,出现弹窗后点击next. 接着在弹窗Select an Archetype中,filte ...

  8. js 数组的所有操作

    js的数组操作有很多,这里记录了常用的和不常用的数组操作方法. 一.数组的创建 数组的创建有两种方法,一种是通过字面量,另一种是通过Array构造函数. 1.字面量 var num1 = [1,2,3 ...

  9. neutron VPC

    The goal of this document is to provide an umbrella blueprint defining how to add support for VPC in ...

  10. java:解决eclipse配置Tomcat时找不到server选项

    http://blog.csdn.net/wugangsunny/article/details/25246565 集成Eclipse和Tomcat时找不到server选项: 按照网上的步骤如下: 在 ...