美文网首页JavaScript与数据结构
JavaScript数据结构23—排序二叉树

JavaScript数据结构23—排序二叉树

作者: RichardW | 来源:发表于2017-04-08 16:14 被阅读0次

本示例表现了排序二叉树的三种操作
查找,删除,插入

//二叉排序树
function BtreeNode(data) {
  this.data = data;
}
BtreeNode.prototype.setL = function(node){
  this.lchild = node;
}
BtreeNode.prototype.setR = function(node){
  this.rchild = node;
}
BtreeNode.prototype.search = function(key){
  if(key == this.data){
    return {find:true,node:this};
  }else if(key < this.data){
    if(!this.lchild){
      return {find:false,node:this};
    }
    return this.lchild.search(key);
  }else{
    if(!this.rchild){
      return {find:false,node:this};
    }
    return this.rchild.search(key);
  }
}
BtreeNode.prototype.insert = function(key){
  var searchResult = this.search(key);
  if(!searchResult.find){
    var s = new BtreeNode(key);
    if(key<searchResult.node.data){
      searchResult.node.lchild = new BtreeNode(key);
    }else{
      searchResult.node.rchild = new BtreeNode(key);
    }
    return true;
  }
  return false;
}
BtreeNode.prototype.delete = function(){
  if(!this.rchild){
    var p = this;
    p = this.lchild;
  }else if(!this.lchild){
    var p = this
    p = this.rchild;
  }else{
    var q = this;
    var s = this.rchild;
    while(s.rchild){
      q = s;
      s = s.rchild;
    }
    this.data = s.data;
    if(q!=this){
      q.rchild = s.lchild;
    }else{
      q.lchild = s.lchild;
    }
  }
}
BtreeNode.prototype.deleteKey = function(key){
  if(this.data == key){
    this.delete();
  }else if(this.data>key){
    this.lchild.deleteKey(key);
  }else{
    this.rchild.deleteKey(key);
  }
}
var array1 = [];
for (var i = 0; i < 5000; i++) {
  array1[i] = parseInt(Math.random()*100000)+1;
}
var b = new BtreeNode(50000);
for (var i = 0; i < array1.length; i++) {
  b.insert(array1[i]);
}
console.info(array1[2]);
console.info(b.search(array1[2]));
b.deleteKey(array1[2]);
console.info(b.search(array1[2]));

OUTPUT

99209
{ find: true,
node:
BtreeNode {
data: 99209,
lchild: BtreeNode { data: 95537, rchild: [Object], lchild: [Object] },
rchild: BtreeNode { data: 99387, rchild: [Object], lchild: [Object] } } }
{ find: false, node: BtreeNode { data: 99191 } }
[Finished in 0.1s]

相关文章

  • 数据结构

    数据结构 数据结构概念 顺序表 链表 队列 栈 二叉树 常用排序算法

  • JavaScript数据结构23—排序二叉树

    本示例表现了排序二叉树的三种操作查找,删除,插入 OUTPUT 99209{ find: true,node:Bt...

  • 关于函数递归和迭代的转化, 及尾递归相关知识的接触和思考

    javascript实现数据结构: 树和二叉树,二叉树的遍历和基本操作 js 二叉树 【数据结构与算法】深入浅出递...

  • java堆排序

    什么是堆排序:图解堆排序堆排序:利用了堆这种数据结构堆数据结构:特殊的完全二叉树,因为具有以下的特点:1)每个结点...

  • JavaScript 使用 堆排序

    堆 排序 JavaScript 使用 堆 进行对数组的排序 基本的概念 必须是完全二叉树 ((n - 1) 层必须...

  • 前端常见数据结构小结

    常见数据结构的 JavaScript 实现 栈 队列 链表 集合 字典 哈希表 二叉树 图 前端与数据结构 数据结...

  • 数据结构

    数组 数组是数据结构的基础,描述了物理排序地址 栈 队列 链表 二叉树

  • iOS中级开发面试的重点

    Runloopruntime锁多线程优化block 算法: 排序, 查找数据结构: 链表, 二叉树矩阵哈希怎么解决...

  • 排序算法

    之前一篇练习数据结构中的二叉树-BinaryTree,本篇来点——排序算法,调调味,都是基本的排序算法中。 1. ...

  • 堆排序

    堆排序 堆排序(Heapsort) 是指利用堆这种数据结构所设计的一种排序算法. 堆是一个近似完全二叉树的结构, ...

网友评论

    本文标题:JavaScript数据结构23—排序二叉树

    本文链接:https://www.haomeiwen.com/subject/nhglattx.html