js模拟简单数据结构:栈、队列、单向链表、二叉查找树

1,293 阅读2分钟

本文使用js简单模拟下几种数据结构,主要加深自己的理解

栈结构

定义

  • 允许插入和删除的一端称为栈顶,另一端称为栈底
  • 不含任何数据元素的栈称为空栈
  • 后进先出的数据结构

简单图形示例

代码实现

了解了栈的基本属性后,来用js模拟下

   class Stack{
       constructor() {
           this.stack = []
       }
       put(element){         //进栈
           this.stack.push(element)
       }
       pop() {             //出栈
           this.stack.pop()
       }
   }
   
   //调用
   let stack = new Stack()
   stack.put(1)
   stack.put(2)
   stack.pop()
   console.log(stack.stack)

队列

定义

  • 先进先出的数据结构
  • 只允许在一端进行插入,而在另外一端进行删除
  • 允许插入的称为队尾,允许删除的称为队头

简单图形实例

js模拟

class Queye {
   constructor() {
       this.queye = []
   }
   enqueye(element) {       //入队列
       this.queye.push(element)
   }
   dequeye() {           //入队列
       this.queye.shift()
   }
}

let queye = new Queye()
queye.enqueye(1)
queye.enqueye(2)
queye.dequeye()
console.log(queye.queye)    //[2]

单向链表

定义

  • 链表中的元素不是连续放置的
  • 每个元素由一个存储元素本身的节点和指向下一个元素的引用组成
  • 添加或移动元素的时候不需要移动其它元素

图标示例

js 模拟



//元素节点
class Node {
  constructor(element) {
      this.element = element
      this.next = null
  }
}
//单向链表类
class LinkList {
  constructor() {
      this.head = null
      this.length = 0
  }
  //往链表中插入数据
  insert(pos, element) {
      let node = new Node(element)
      if(!this.head) {
          this.head = node
      } else {
          let index = 0;
          let current = this.head;
          let previous = null;
          while(index++ < pos) {
              previous = current;
              current = current.next;
          }
          previous.next = node
          node.next = current;
      }
      this.length ++;
  }
  //往链表里放数据
  append(element) {
      let node = new Node(element)
      if(!this.head){
          this.head = node
      } else {
          let index = 0;   //从0项开始查找
          let current = this.head;
          while(++index < this.length) {
              current = current.next;
          }
          current.next = node
      }
      this.length ++;
  }
}

let ll = new LinkList()
ll.append(1)
ll.append(2)
ll.insert(2, 100)
console.log(ll)

二叉查找树

二叉树定义

  • n个节点的有限集合
  • 每个节点最多两个字数
  • 左子树和右子树是有顺序的
    • 小的放左边
    • 大的放右边

图例

js模拟

class Node {
  constructor(element) {
      this.element = element;
      this.left = null;
      this.right = null;
  }
}

class Tree {
  constructor() {
      this.root = null;
  }
  insert(root, newNode) {
      if(newNode.element < root.element) {
          if(root.left === null){
              root.left = newNode;
          } else {
              this.insert(root.left, newNode)
          }
      } else {
          if(root.right === null ){
              root.right = newNode;
          } else {
              this.insert(root.right, newNode)
          }
      }
  }
  add(element) {
      let node = new Node(element)
      if(!this.root) {
          this.root = node
      } else {
          this.insert(this.root, node)
      }
  }
}


let tree = new Tree()
tree.add(50)
tree.add(25)
tree.add(75)
tree.add(12)
tree.add(37)
console.log(tree)