:以分层的方式存储数据;节点:根节点,子节点,父节点,叶子节点(没有任何子节点的节点);:根节点开始0层;

二叉树:每个节点子节点不超过两个;查找快(比链表),添加,删除快(比数组);

BST:二叉树查找:

  • 设置根节点为当前节点;
  • 如果要插入的节点小于当前节点,则设置其左节点为新的当前节点;大于的话选右节点;
  • 如果如果选择的节点为null,则将要插入的节点放在这个位置,退出;否则继续向下查找;

实现的基本代码:

 function Node (data,left,right) {
this.data = data;
this.show = show;
this.left = left;
this.right = right;
}
function show() {
console.log(this.data);
}
function BST() {
this.root = root;
this.insert = insert;
}
function insert(data) {
var n = new Node(data,null,null);
if(this.root === null) {
this.root = n;
} else {
var currNode = this.root,parent;
while(true) {
parent = currNode;
if(data == currNode.data) {
break;
} else if(data < currNode.data) {
currNode = currNode.left;
if(currNode === null) {
parent.left = n;
break;
}
} else {
currNode = currNode.right;
if(currNode === null) {
parent.right = n;
break;
}
}
}
}
}

 遍历查找:(名字即为要查找的节点的输出顺序)

  • 中序遍历:升序遍历要查找的节点及其子孙节点;

//10,22,30,56,77,81,92

 function inOrder(node) {
if(!(node == null)) {
inOrder(node.left);
node.show();
inOrder(node.right);
}
}

  操作:demo:

  • 先序遍历:先输出要查找的节点,然后从左边的子节点开始依照先左后右输出;

//50,10,5,15,70,60,80

 function preOrder(node) {
if(!(node == null)) {
node.show();
preOrder(node.left);
preOrder(node.right);
}
}

  操作:demo:

  • 后序遍历:从要查找的节点的左节点最左边的子孙节点开始,按照左右各一次的顺序输出直到其左子节点;然后从其右节点的最左边的子孙节点开始;最后输出要查找的节点;

//3,22,16,37,99,45,23

 function postOrder(node) {
if(!(node == null)) {
postOrder(node.left);
postOrder(node.right);
node.show();
}
}

在二叉树上查找:

  • 最大值:即遍历右子树
 function getMax() {
var currNode = this.root;
while(!(currNode.right == null)) {
currNode = currNode.right;
}
console.log(currNode.data);
return currNode;
}
  • 最小值:即遍历左子树
function getMin() {
var currNode = this.root;
while(!(currNode.left == null)) {
currNode = currNode.left;
}
console.log(currNode.data);
return currNode;
}
  • 查找给定值:
 function find(data) {
var currNode = this.root;
while(currNode != null) {
if(currNode.data === data) {
return currNode;
} else if(data < currNode.data) {
currNode = currNode.left;
} else {
currNode = currNode.right;
}
}
return null;
}

操作:demo:

  • 删除节点:

    • 先判断,如果当前节点包括则删除节点;如果不包括,则比较大小向下一层查找;
    • 删除节点的时候,如果是叶子节点,则将其父节点指向它的引用指向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) {
//not child node;
if(node.left == null && node.right == null) {
return null;
}
//not left child node;
if(node.left == null) {
return node.right;
}
//not right child node;
if(node.right == null) {
return node.left;
}
//all have
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);//set parent.left null to delete this node;
return node;
} else {
node.right = removeNode(node.right,data);
return node;
}
}

  操作:demo:

  • 计数:可以给Node添加this.count = 1;在添加数值的时候,虽然相同的数值不会重复加入,但可以记录其被添加的次数;

js:数据结构笔记9--二叉树的更多相关文章

  1. JS数据结构与算法 - 二叉树(一)基本算法

    仅供JavaScript刷题参考用. 二叉查找树和平衡二叉树 其它树:满二叉树.完全二叉树.完美二叉树.哈弗曼树.二叉查找树BST.平衡二叉树AVL 了解:红黑树,是一种特殊的二叉树.这种树可以进行高 ...

  2. js:数据结构笔记12--排序算法(2)

    高级排序算法:(处理大数据:百万以上) 希尔排序:是插入排序的优化版: 首先设置间隔数组,然后按照每个间隔,分别进行排序: 如第一个间隔为5,首先a[5]与a[0]进行插入排序;然后a[6]和a[0] ...

  3. js:数据结构笔记5--链表

    数组: 其他语言的数组缺陷:添加/删除数组麻烦: js数组的缺点:被实现为对象,效率低: 如果要实现随机访问,数组还是更好的选择: 链表: 结构图: 基本代码: function Node (elem ...

  4. js:数据结构笔记4--队列

    队列是一种特殊的列表,数据结构为FIFO: 定义: function Queue() { this.dataStore = []; this.enqueue = enqueue; this.deque ...

  5. js:数据结构笔记3--栈

    栈是一种特殊的列表,数据结构为LIFO: 定义: function Stack() { this.dataStore = []; this.top = 0; this.push = push; thi ...

  6. js:数据结构笔记1---数组

    JS中数组: 只是一种特殊的对象,比其他语言中效率低: 属性是用来表示偏移量的索引:在JS中,数字索引在内部被转化为字符串类型(这也是为什么写对象属性的时候可以不叫引号),因为对象中的属性必须是字符串 ...

  7. js:数据结构笔记14--高级算法

    动态规划: 递归是从顶部开始将问题分解,通过解决所有分解出小问题来解决整体问题: 动态规划从底部开始解决问题,将所有小问题解决,然后合并掉一个整体解决方案: function dynFib(n) { ...

  8. js:数据结构笔记13--检索算法

    顺序查找:也称线性查找,暴力查找的一种 基本格式: var nums = []; for(var i = 0; i < 10; ++i) { nums[i] = Math.floor(Math. ...

  9. js:数据结构笔记11--排序算法(1)

    基本准备: function CArray(numElems) { this.dataStore = []; this.pos = 0; this.numElems = numElems; this. ...

随机推荐

  1. CMS介绍

    CMS介绍 CMS是Content Management System的缩写,意为“内容管理系统”,它具有许多基于模板的优秀设计,可以加快网站开发的速度和减少开发的成本. CMS的功能不仅限于处理文本 ...

  2. sql重复记录查询

    1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 select * from people where peopleId in (select  peopleId  fro ...

  3. php面试题之四——Linux部分(高级部分)

    四.Linux部分 1.请解释下列10个shell命令的用途(新浪网技术部) top.ps.mv.find.df.cat.chmod.chgrp.grep.wc top:该命令提供了实时对系统处理器状 ...

  4. 在VMware上面安装Solaris 10

    导读 Oracle Solaris 11 是世界上最先进的企业操作系统,提供安全.速度.简单的企业云环境和DevOps.在这篇文章中我们将使用Solaris 10版本,但您可以按照同样的步骤,来安装刚 ...

  5. delphi 枚举类型

    枚举类型定义了一系列有序值的集合.枚举变量就是从这个既定的集合中取某个值.集合中的有序值可以称为元素,元素一般从0开始索引(也就是元素的顺序号). 定义一个枚举类型,采用以下的格式: type typ ...

  6. Java for LeetCode 066 Plus One

    Given a non-negative number represented as an array of digits, plus one to the number. The digits ar ...

  7. Java for LeetCode 060 Permutation Sequence

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  8. HDU 1087 Super Jumping! Jumping! Jumping! 最大递增子序列

    Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  9. codeforces 478B Random Teams 解题报告

    题目链接:http://codeforces.com/problemset/problem/478/B 题目意思:有 n 个人,需要将这班人分成 m 个 组,每个组至少含有一个人,同一个组里的人两两可 ...

  10. Extjs给gridPanel添加单价双击事件和获取当前行的数据

    有两个小属性,如下 this.on('rowdblclick', this.readContent, this); this.on('cellclick', this.gridCellClick, t ...