js:数据结构笔记9--二叉树

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

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

BST:二叉树查找:

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

实现的基本代码:

 function Node (data,left,right) {
   this.data = data;
   this.show = sh
 }
 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) {
         currNode = currNode.left;
         if(currNode === null) {
           parent.left = n;
           break;
         }
       } else {
         currNode = currNode.right;
         if(currNode === null) {
           parent.right = n;
           break;
         }
       }
     }
   }
 }

  

时间: 2024-08-09 10:41:52

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

数据结构笔记_二叉树的性质

二叉树的性质: 性质1.在二叉树第i层上至多有2^(i-1)个结点(i>=1). 性质2.深度为k的二叉树上至多含2^(k)-1个结点(k>=1). 性质3.对任何一颗二叉树,若它含有n0个叶子结点,n2个度为2的结点,则必存在关系式n0=n2+1. 证明: 设二叉树上的结点总数为n,则n=n0+n1+n2,其中n1为度为1的结点数. 又二叉树上分支总数b=n1+2*n2; 而b=n-1(因为除根结点外,每个结点都有一个指向它的分支)=n0+n1+n2-1,由此,n0=n2+1: 两类特殊二叉

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

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

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

队列是一种特殊的列表,数据结构为FIFO: 定义: function Queue() { this.dataStore = []; this.enqueue = enqueue; this.dequeue = dequeue; this.front = front; this.back = back; this.length = length; this.toString = toString; this.isEmpty = isEmpty; } function enqueue(elem) {

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

JS中数组: 只是一种特殊的对象,比其他语言中效率低: 属性是用来表示偏移量的索引:在JS中,数字索引在内部被转化为字符串类型(这也是为什么写对象属性的时候可以不叫引号),因为对象中的属性必须是字符串类型: 操作: 判断:isArray(); 复制: 浅复制: var arr1 = arr2; 深复制: function copy(arr1) { var arr2 = []; for(var i = 0; i < arr1.length; i++) { arr2[i] = arr1[i]; }

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

数组: 其他语言的数组缺陷:添加/删除数组麻烦: js数组的缺点:被实现为对象,效率低: 如果要实现随机访问,数组还是更好的选择: 链表: 结构图: 基本代码: function Node (elem) { this.elem = elem; this.next = null; } function LList() { this.head = new Node("head"); this.find = find; this.insert = insert; this.findPrevi

js:数据结构笔记8--集合

集合:唯一性,无序性: 基本结构: function Set () { this.dataStore = []; this.add = add; this.remove = remove; this.contains =contains; this.show = show; } function contains(data) { var pos = this.dataStore.indexOf(data); if(pos > -1) { return true; } else { return

js:数据结构笔记10--图和图算法

图:是由边和定点的集合组成:  按照图的定点对是否有序可以分为:有向图和无向图:

js:数据结构笔记2---列表

列表: 定义:一组有序的数据: function List() { this.listSize = 0; this.pos = 0; this.dataStore = []; this.find = find; ......................... } 方法: append:添加数据 function append(element) { this.dataStore[this.listSize++] = element; } find:查找元素://indexOf function

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

高级排序算法:(处理大数据:百万以上) 希尔排序:是插入排序的优化版: 首先设置间隔数组,然后按照每个间隔,分别进行排序: 如第一个间隔为5,首先a[5]与a[0]进行插入排序;然后a[6]和a[0],a[1]进行插入排序,直到最后一个: 然后换下一个间隔值,直到所有间隔值排序完(当间隔值为1时,就是一般的插入排序): 效果: 首先在类中添加间隔数组: this.gaps = [5,3,1]; 然后添加函数: function shellsort() { for(var g = 0; g < t