读书笔记----数据结构与算法JavaScript描述

 
第二章 数组
数组存取元素
indexof() 返回元素在数组中的索引,不存在就是false
 
数组删除元素
pop()
删除数组第一个添加的元素
shitf()
删除数组第一个元素
 
foreach()
 
function square(num) { print(num, num * num); } var nums = [1,2,3,4,5,6,7,8,9,10]; nums.forEach(square);
 
生成新数组的迭代器
map(),filter()
 
 
var arr = [1,2,3,4]
 
function add(n){
 return n+1;
}
 
var newArr = arr.map(add)
 
 
第三章 列表
 
 
 
第四章 栈
 
push() 入栈,从尾部进
function push(e){
    this.dataStore[this.top++] = e;
}
pop()出栈,从尾部出,删除尾元素
function pop(){
   return this.dataStore[--this.top];
}
peek()出栈从尾部出栈,不删除尾元素
function peek(){
   return this.dataStore[this.top-1];
}
 
clear()清楚所有元素
 
function clear(){
 this.top = 0;
}
 
Stack类
function Stack(){
   this.dataStore = [];
   this.top = 0;
   this.push = push();
   this.pop = pop();
   this.peek = peek();
   this.clear = clear();
   this.length = length;
}
 
第五章 队列
 
push() 入队,从末尾入队
 
shift()出队,从头部出队
 
function Queue() {
 this.dataStore = []; 
this.enqueue = enqueue;
 this.dequeue = dequeue;
 this.front = front; 
this.back = back; 
this.toString = toString;
 this.empty = empty;
 }
enqueue() 方法向队尾添加一个元素:
function enqueue(element) { this.dataStore.push(element); }
 
dequeue() 方法删除队首的元素:
 function dequeue() { return this.dataStore.shift(); }
 
 
可以使用如下方法读取队首和队尾的元素:
 function front() { return this.dataStore[0]; } 
function back() { return this.dataStore[this.dataStore.length-1]; }
 
第六章 链表
一组节点组成的集合
 
 
第七章 字典
 
 
第八章 散列
HashTable 类的构造函数定义如下:
function HashTable() {
    this.table = new Array(137);
    this.simpleHash = simpleHash;
    this.showDistro = showDistro;
    this.put = put; //this.get = get; }
 
    第九章 集合
 
    集合是由一组无序但彼此之间又有一定相关性的成员构成的, 每个成员在集合中只能出现 一次
 
 
    function Set() {
        this.dataStore = [];
        this.add = add;
        this.remove = remove;
        this.size = size;
        this.union = union;
        this.intersect = intersect;
        this.subset = subset;
        this.difference = difference;
        this.show = show;
    }
 
    function add(data) {
        if (this.dataStore.indexOf(data) < 0) {
            this.dataStore.push(data);
            return true;
        } else {
            return false;
        }
    }
 
    function remove(data) {
        var pos = this.dataStore.indexOf(data);
        if (pos > -1) {
            this.dataStore.splice(pos, 1);
            return true;
        } else {
            return false;
        }
    }
 
 
    function contains(data) {
        if (this.dataStore.indexOf(data) > -1) {
            return true;
        } else {
            return false;
        }
    }
 
第十章 树
二叉查找树
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;
}
 
function insert(data) {
    var n = new Node(data, left, right);
    if (this.root == null) {
        this.root = n;
    } else {
        var current = this.root;
        var parent;
        while (true) {
            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);
   putstr(node.show()+" ");
  inOrder(node.right);
}
}
 
先序遍历
function preOrder(node){
    if(!(node==null)){
    putstr(node.show()+" ");
    preOrder(node.left);
   preOrder(node.right);
}
}
 
 
后续遍历
function postOrder(){
if(!(node==null)){
   postOrder(node.left);
postOrder(node.right);
putstr(node.show()+" ");
}
}
 
 
第十一章 图
function Graph(v){
this.vertices = v;
this.edges = 0;
this.adj = [];
for(var i =0;i<this.vertiles;++i){
this.adj[i] = [];
this.adj[i].push("");
}
this.addEdge = addEdge;
this.toString = toString;
 
}
 
 
第十二章 算法排序
 
冒泡排序
function bubbleSort(){
var numElements = this.dataStore.length;
var temp;
for(var outer = numElements;outer>=2;--outer){
   for(var inner =0;inner<=outer-1;inner++){
    if(this.dataStore[inner]>this.dataStore[inner+1]){
      swap(thisdataStore,inner,innner+1)
}
}
}
}
 
 
选择排序
function selectionSort(){
   var min,temp;
for(var outer=0;outer<=this.dataStore.length-2;++outer){
     min = outer;
    for(var inner=outer+1;inner<=this.dataStore.length-1;i++){
   if(this.dataStore[inner]<this.dataStore[min]){
        min = innner;
}
swap(this.dataStore,outer,inner);
}
}
}
 
 
 
插入排序
 
function insertSort(){
    var temp,inner;
    for(var outer=1;outer<=this.dataStore.length-1;++outer){
    temp = this.dataStore[outer];
   inner = outer;
   while(inner>0&&(this.dataStore[inner-1]>=temp)){
    this.dataStore[inner] = this.dataStore[inner-1];
--inner;
}
this.dataStore[inner] = temp;
}
}
 
 
 
希尔排序
function shellsort() {
    for (var g = 0; g < this.gaps.length; ++g) {
        for (var i = this.gaps[g]; i < this.dataStore.length; ++i) {
            var temp = this.dataStore[i];
            for (var j = i; j >= this.gaps[g] && this.dataStore[j - this.gaps[g]] > temp; j -= this.gaps[g]) {
                this.dataStore[j] = this.dataStore[j - this.gaps[g]];
            }
            this.dataStore[j] = temp;
        }
    }
}
 
归并排序
 
function mergeSort(arr) {
    if (arr.length < 2) {
        return;
    }
    var step = 1;
    var left, right;
    while (step < arr.length) {
        left = 0;
        right = step;
        while (right + step <= arr.length) {
            mergeArrays(arr, left, left + step, right, right + step);
            left = right + step;
            right = left + step;
        }
        if (right < arr.length) {
            mergeArrays(arr, left, left + step, right, arr.length);
        }
        step *= 2;
    }
}
 
function mergeArrays(arr, startLeft, stopLeft, startRight, stopRight) {
    var rightArr = new Array(stopRight - startRight + 1);
    var leftArr = new Array(stopLeft - startLeft + 1);
    k = startRight;
    for (var i = 0; i < (rightArr.length - 1); ++i) {
        rightArr[i] = arr[k];
        ++k;
    }
    k = startLeft;
    for (var i = 0; i < (leftArr.length - 1); ++i) {
        leftArr[i] = arr[k];
        ++k;
    }
    rightArr[rightArr.length - 1] = Infinity; // 哨兵值
    leftArr[leftArr.length - 1] = Infinity; // 哨兵值
    var m = 0;
    var n = 0;
    for (var k = startLeft; k < stopRight; ++k) {
        if (leftArr[m] <= rightArr[n]) {
            arr[k] = leftArr[m];
            m++;
        } else {
            arr[k] = rightArr[n];
            n++;
        }
    }
    print("left array - ", leftArr);
    print("right array - ", rightArr);
}
var nums = [6, 10, 1, 9, 4, 8, 2, 7, 3, 5];
print(nums);
print();
mergeSort(nums);
print();
print(nums);
 
 
原文地址:https://www.cnblogs.com/SunlikeLWL/p/8670373.html