数据结构

前言

随着node的兴起, 将javascript推向的一个前所未有的高度, node作为为建立高性能的服务端而创建的js运行平台随着时间的推移和生态链的完善已经不再局部于服务端,包括前端,后端,桌面......
正如Jeff Atwood在2007年提出的:任何可以使用JavaScript来编写的应用,最终会由JavaScript编写, 虽然人们大多或多或少对笔者的文章有点断章取义, 但无可变非的是javascript正在大行其道 ^_^,
但正如伟大的js之父亲布兰登.艾奇--(每每想起他和 冰与火之歌中史塔克家族什么关系 哈哈)所说js是C语言和Self语言一夜情的产物, 他的生命力与发展远远超出作者的预期, 间接导致设计之初并没有过多的考虑对数据结构的支持,
但es规范制定者似乎已经察觉到了这一点, 于是最es6标准增加了set集合和map集合,有兴趣的可以查阅最新的es标准,这里推荐阮一峰的es6入门 笔者认为是一部不错的教材(^_^笔者可不是做广告),有兴趣的可以查阅,下面给出连接

es6 阮一峰http://es6.ruanyifeng.com


二维数组

    Array.matrix = function(numrows, numcols, initial) {
      let arr = [];
      for (let i = 0; i < numrows; ++i) {
        let columns = [];
        for (let j = 0; j < numcols; ++j) {
          columns[j] = initial;
        }
        arr[i] = columns;
      }
      return arr;
    }

    let Stack = (function(){
      function Stack() {
        this.dataStore = [];
        this.top = 0;
      }
      Stack.prototype.push = function(element) {
        this.dataStore[this.top++] = element;
      }
      Stack.prototype.peek = function() {
        return this.dataStore[this.top-1];
      }
      Stack.prototype.pop = function() {
        return this.dataStore[--this.top];
      }
      Stack.prototype.clear = function() {
        this.top = 0;
      }
      Stack.prototype.length = function() {
        return this.top;
      }
      return Stack;
    })();

队列

    let Queue = (function () {
      function Queue() {
        this.dataStore = [];
      }
      Queue.prototype.enqueue = function(element) {
        this.dataStore.push(element);
      }
      Queue.prototype.dequeue = function() {
        return this.dataStore.shift();
      }
      Queue.prototype.front = function() {
        return this.dataStore[0];
      }
      Queue.prototype.back = function() {
        return this.dataStore[this.dataStore.length-1];
      }
      Queue.prototype.toString = function() {
        var retStr = "";
        for (var i = 0; i < this.dataStore.length; ++i) {
          retStr += this.dataStore[i] + "\n";
        }
        return retStr;
      }
      Queue.prototype.empty = function() {
        if (this.dataStore.length == 0) {
          return true;
        } else {
          return false;
        }
      }
      return Queue;
    })()

优先队列

    let PriQueue = (function() {
      function Ele(value, code) {
        this.value = value;
        this.code = code;
      }
      function PriQueue() {
        this.dataStore = [];
      }
      PriQueue.prototype.enqueue = function(element) {
        this.dataStore.push(element);
      }
      PriQueue.prototype.dequeue = function() {
        var priority = this.dataStore[0].code;
        for (var i = 1; i < this.dataStore.length; ++i) {
          if (this.dataStore[i].code < priority) {
            priority = i;
          }
        }
        return this.dataStore.splice(priority,1);
      }
      PriQueue.prototype.front = function() {
        return this.dataStore[0];
      }
      PriQueue.prototype.back = function() {
        return this.dataStore[this.dataStore.length-1];
      }
      PriQueue.prototype.toString = function() {
        var retStr = "";
        for (var i = 0; i < this.dataStore.length; ++i) {
          retStr += this.dataStore[i].name + " code: " + this.dataStore[i].code + "\n";
        }
        return retStr;
      }
      PriQueue.prototype.empty = function() {
        if (this.dataStore.length == 0) {
          return true;
        } else {
          return false;
        }
      }
      return PriQueue;
    })()

单向链表

    let LList = (function() {
      function Node(element) {
        this.element = element;
        this.next = null;
      }
    
      function LList() {
        this.head = new Node("head");
        this.foot = new Node("foot");
        this.head.next = this.foot;
      }
      LList.prototype.remove = function(item) {
        var prevNode = this.findPrevious(item);
        if (!(prevNode.next == null)) {
          prevNode.next = prevNode.next.next;
        }
      }
      LList.prototype.findPrevious = function(item) {
        var currNode = this.head;
        while ((currNode.next !== null) && (currNode.next.element != item)) {
          currNode = currNode.next;
          if (currNode.next === null) {
            currNode =null;
            break;
          }
        }
        return currNode;
      }
      LList.prototype.display = function() {
        var currNode = this.head;
        console.log(currNode.element);
        while (!(currNode.next == null)) {
          currNode = currNode.next;
          console.log(currNode.element);
        }
      }
      LList.prototype.find = function(item) {
        var currNode = this.head;
        while (currNode.element != item) {
          currNode = currNode.next;
          if (currNode === null) {
            break;
          }
        }
        return currNode;
      }
      LList.prototype.insert = function(newElement, item) {
        var newNode = new Node(newElement);
        var current = this.find(item);
        newNode.next = current.next;
        current.next = newNode;
      }
      return LList;
    })()

双向链表

    let DLList = (function () {
      function Node(element) {
        this.element = element;
        this.next = null;
        this.previous = null;
      }
      function DLList() {
        this.head = new Node("head");
        this.foot = new Node("foot");
        this.head.next = this.foot;
        this.foot.previous = this.head;
      }
      DLList.prototype.find = function(item) {
        var currNode = this.head;
        while (currNode.element != item) {
          currNode = currNode.next;
          if (currNode === null) {
            break;
          }
        }
        return currNode;
      }
      DLList.prototype.insert = function(newElement, item) {
        var newNode = new Node(newElement);
        var current = this.find(item);
        newNode.next = current.next;
        newNode.previous = current;
        current.next.previous = newNode;
        current.next = newNode;
      }
      DLList.prototype.remove = function(item) {
        var currNode = this.find(item);
        if (!(currNode.next == null)) {
          currNode.previous.next = currNode.next;
          currNode.next.previous = currNode.previous;
          currNode.next = null;
          currNode.previous = null;
        }
      }
      DLList.prototype.display = function() {
        var currNode = this.head;
        console.log(currNode.element);
        while (currNode.next !== null) {
          currNode = currNode.next;
          console.log(currNode.element);
        }
      }
      return DLList;
    })()

    let BST = (function () {
      let Node = (function () {
        function Node(data, left, right) {
          this.data = data;
          this.left = left;
          this.right = right;
        }
        Node.prototype.show = function () {
          return this.data;
        }
        return Node;
      })()
    
      function BST() {
        this.root = null;
      }
      BST.prototype.insert = function(data) {
        var n = new Node(data, null, null);
        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;
              }
            }
          }
        }
      }
      BST.prototype.inOrder = function(node) {
        if (!(node === null)) {
          this.inOrder(node.left);
          console.log(node.show() + " ");
          this.inOrder(node.right);
        }
      }
      BST.prototype.preOrder = function(node) {
        if (!(node === null)) {
          console.log(node.show() + " ");
          this.preOrder(node.left);
          this.preOrder(node.right);
        }
      }
      BST.prototype.postOrder = function(node) {
        if (!(node === null)) {
          this.postOrder(node.left);
          this.postOrder(node.right);
          console.log(node.show() + " ");
        }
      }
      BST.prototype.getMin = function() {
        var current = this.root;
        while (!(current.left == null)) {
          current = current.left;
        }
        return current.data;
      }
      BST.prototype.getMax = function() {
        var current = this.root;
        while (!(current.right == null)) {
          current = current.right;
        }
        return current.data;
      }
      BST.prototype.find = function(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;
      }
      BST.prototype.remove = function(data) {
        root = this.removeNode(this.root, data);
      }
      BST.prototype.removeNode = function(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;
          }
          // 有两个子节点的节点
          var tempNode = this.getSmallest(node.right);
          node.data = tempNode.data;
          node.right = this.removeNode(node.right, tempNode.data);
          return node;
        }
        else if (data < node.data) {
          node.left = this.removeNode(node.left, data);
          return node;
        }
        else {
          node.right = this.removeNode(node.right, data);
          return node;
        }
      }
      BST.prototype.getSmallest = function(node) {
        if (node.left == null) {
          return node;
        } else {
          return this.getSmallest(node.left);
        }
     }
    
      return BST;
    })()

    let Graph = (function() {
      function Graph(v) {
        this.vertices = v;
        this.vertexList = [];
        this.edges = 0;
        this.adj = [];
        for (var i = 0; i < this.vertices; ++i) {
          this.adj[i] = [];
          //  this.adj[i].push("");
        }
        this.marked = [];
        for (var i = 0; i < this.vertices; ++i) {
          this.marked[i] = false;
        }
        this.edgeTo = [];
      }
      Graph.prototype.addEdge = function (v, w) {
        this.adj[v].push(w);
        this.adj[w].push(v);
        this.edges++;
      }
      Graph.prototype.showGraph = function() {
        for (var i = 0; i < this.vertices; ++i) {
          let st = i + " -> " ;
          for (var j = 0; j < this.vertices; ++j ) {
            if (this.adj[i][j] != undefined) {
              st += this.adj[i][j] + ' ';
            }
          }
          console.log(st);
        }
      }
      Graph.prototype.dfs = function(v) {
        this.marked[v] = true;
        if (this.adj[v] != undefined) { 
          console.log("Visited vertex: " + v);
        }
        for (const w of this.adj[v]) {
          if (!this.marked[w]) {
            this.dfs(w);
          }
        }
      }
      Graph.prototype.bfs = function(s) {
        var queue = [];
        this.marked[s] = true;
        queue.push(s); // 添加到队尾
        while (queue.length > 0) {
          var v = queue.shift(); // 从队首移除
          if (v !== undefined) {
            console.log("Visisted vertex: " + v);
          }
          for (var w of this.adj[v]) {
            if (!this.marked[w]) {
              this.edgeTo[w] = v;
              this.marked[w] = true;
              queue.push(w);
            }
          }
        }
        console.log(this.edgeTo);
      }
      Graph.prototype.hasPathTo = function(s, v) {
        this.bfs(s)
        return this.marked[v];
      }
      Graph.prototype.pathTo = function(s, v) {
        var source = s;
        if (!this.hasPathTo(s, v)) {
          return undefined;
        }
        var path = [];
        for (var i = v; i != source; i = this.edgeTo[i]) {
          path.unshift(i);
        }
        path.unshift(source);
        return path;
      }
      Graph.prototype.topSort = function() {
        var stack = [];
        var visited = [];
        for (var i = 0; i < this.vertices; i++) {
          visited[i] = false;
        }
        for (var i = 0; i < this.vertices; i++) {
          if (visited[i] == false) {
            this.topSortHelper(i, visited, stack);
          }
        }
        for (var i = 0; i < stack.length; i++) {
          if (stack[i] != undefined && stack[i] != false) {
            console.log(this.vertexList[stack[i]]);
          } 
        }
      }
      Graph.prototype.topSortHelper = function(v, visited, stack) {
        visited[v] = true; 
        for (var w in this.adj[v]) {
          if (!visited[w]) {
            this.topSortHelper(visited[w], visited, stack);
          }
        }
        stack.push(v);
      }
      return Graph;
    })()

镰月
313 声望13 粉丝

它的优秀之处并非原创,它的原创之处并不优秀。