javascript数据结构与算法---二叉树(删除节点)
javascript数据结构与算法---二叉树(删除节点)
- function Node(data,left,right) {
- this.data = data;
- this.left = left;
- this.right = right;
- this.show = show;
- }
- function show() {
- return this.data;
- }
- function BST() {
- this.root = null;
- this.insert = insert;
- this.inOrder = inOrder;
- this.getMin = getMin;
- this.getMax = getMax;
- this.find = find;
- this.remove = remove;
- }
- function insert(data) {
- var n = new Node(data,null,null);
- if(this.root == null) {
- this.root = n;
- }else {
- var current = this.root;
- var parent;
- while(current) {
- 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());
- 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(current.data == data) {
- return current;
- }else if(data < current.data) {
- current = current.left;
- }else {
- current = current.right;
- }
- }
- return null;
- }
- function remove(data) {
- root = removeNode(this.root,data);
- }
- function getSmallest(node) {
- if (node.left == null) {
- return node;
- }
- else {
- return getSmallest(node.left);
- }
- }
- 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;
- }
- // 有2个子节点的节点
- var tempNode = getSmallest(node.right);
- node.data = tempNode.data;
- node.right = removeNode(node.right,tempNode.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;
- }
- }
- 代码初始化如下:
- var nums = new BST();
- nums.insert(23);
- nums.insert(45);
- nums.insert(16);
- nums.insert(37);
- nums.insert(3);
- nums.insert(99);
- nums.insert(22);
- var min = nums.getMin();
- console.log(min);
- var max = nums.getMax();
- console.log(max);
- var value = nums.find("45");
- console.log(value);
- nums.remove(23);
javascript数据结构与算法---二叉树(删除节点)的更多相关文章
- javascript数据结构与算法-- 二叉树
javascript数据结构与算法-- 二叉树 树是计算机科学中经常用到的一种数据结构.树是一种非线性的数据结构,以分成的方式存储数据,树被用来存储具有层级关系的数据,比如文件系统的文件,树还被用来存 ...
- javascript数据结构与算法---二叉树(查找最小值、最大值、给定值)
javascript数据结构与算法---二叉树(查找最小值.最大值.给定值) function Node(data,left,right) { this.data = data; this.left ...
- javascript数据结构与算法--二叉树遍历(后序)
javascript数据结构与算法--二叉树遍历(后序) 后序遍历先访问叶子节点,从左子树到右子树,再到根节点. /* *二叉树中,相对较小的值保存在左节点上,较大的值保存在右节点中 * * * */ ...
- javascript数据结构与算法--二叉树遍历(先序)
javascript数据结构与算法--二叉树遍历(先序) 先序遍历先访问根节点, 然后以同样方式访问左子树和右子树 代码如下: /* *二叉树中,相对较小的值保存在左节点上,较大的值保存在右节点中 * ...
- javascript数据结构与算法--二叉树遍历(中序)
javascript数据结构与算法--二叉树遍历(中序) 中序遍历按照节点上的键值,以升序访问BST上的所有节点 代码如下: /* *二叉树中,相对较小的值保存在左节点上,较大的值保存在右节点中 * ...
- javascript数据结构与算法--二叉树(插入节点、生成二叉树)
javascript数据结构与算法-- 插入节点.生成二叉树 二叉树中,相对较小的值保存在左节点上,较大的值保存在右节点中 /* *二叉树中,相对较小的值保存在左节点上,较大的值保存在右节点中 * * ...
- 为什么我要放弃javaScript数据结构与算法(第八章)—— 树
之前介绍了一些顺序数据结构,介绍的第一个非顺序数据结构是散列表.本章才会学习另一种非顺序数据结构--树,它对于存储需要快速寻找的数据非常有用. 本章内容 树的相关术语 创建树数据结构 树的遍历 添加和 ...
- JavaScript 数据结构与算法之美 - 线性表(数组、栈、队列、链表)
前言 基础知识就像是一座大楼的地基,它决定了我们的技术高度. 我们应该多掌握一些可移值的技术或者再过十几年应该都不会过时的技术,数据结构与算法就是其中之一. 栈.队列.链表.堆 是数据结构与算法中的基 ...
- JavaScript 数据结构与算法之美 - 非线性表中的树、堆是干嘛用的 ?其数据结构是怎样的 ?
1. 前言 想学好前端,先练好内功,内功不行,就算招式练的再花哨,终究成不了高手. 非线性表(树.堆),可以说是前端程序员的内功,要知其然,知其所以然. 笔者写的 JavaScript 数据结构与算法 ...
随机推荐
- Le Chapitre IV
J'avais ainsi appris une seconde chose très importante: C'est que sa planète d'origine était à peine ...
- Gibs抽样
/* * Copyright (C) 2007 by * * Xuan-Hieu Phan * hieuxuan@ecei.tohoku.ac.jp or pxhieu@gmail.com * Gra ...
- 关于内存类型 UDIMM、RDIMM、LRDIMM 的学习结论(转)
随着内存技术不断发展,服务器上内存的容量.密度和速度也越来越高.目前在市场上出现的内存条最高密度可以做到每条内存条 4 个 Rank,容量达到 32GB/条,最高速度达到 1.6GHz.高密度高频率也 ...
- VSS + Eclipse 管理源码
- 一个发送邮件的java类,包含多种发送方法
import java.util.Calendar;import java.util.Date; import java.util.Properties; import javax.mail.Addr ...
- spring之jdbcTemplate
spring的另一个功能模块data access对于数据库的支持 spring data access第一个helloword案例: 使用java程序实现访问配置 1.导包 2.测试案例 @Test ...
- Using Spring.net in console application
Download Spring.net in http://www.springframework.net/ Install Spring.NET.exe Create a console appli ...
- Codeforces816B Karen and Coffee 2017-06-27 15:18 39人阅读 评论(0) 收藏
B. Karen and Coffee time limit per test 2.5 seconds memory limit per test 512 megabytes input standa ...
- hdu 4135 [a,b]中n互质数个数+容斥
http://acm.hdu.edu.cn/showproblem.php?pid=4135 给定一个数n,求某个区间[a,b]内有多少数与这个数互质. 对于一个给定的区间,我们如果能够求出这个区间内 ...
- Vue的配置
一.build:打包的配置文件的文件夹 1.build.js 生产版本的配置文件,一般这个文件我们是不改的 'use strict' //调用检查版本的文件,check-versions的导出直接是 ...