BootCamp_Codestates/Sprint Review

IM - data-structure

Nomad Kim 2020. 12. 18. 20:05
  • Queue
  • stack
  • hashTable
  • linkedList
  • BST
  • graph
  • tree

Queue

class Queue {
  constructor() {
    this.storage = {};
    this.front = 0;
    this.rear = 0;
  }

  size() {
    return Object.keys(this.storage).length;
    //reference:  return this.rear - this.front;
  }

  enqueue(element) {
    this.storage[this.rear] = element;
    this.rear++;
  }

  dequeue() {
    if(this.size() === 0){
      this.front = this.rear;
      return undefined;
    }
    //reference:
    // if (this.size() === 0) {
    //   return;
    // }
    const deletedVal = this.storage[this.front];
    delete this.storage[this.front];
    this.front++;
    return deletedVal;
  }
}

 


stack

class Stack {
  constructor() {
    this.storage = {};
    this.top = -1;
  }

  size() {
    return Object.keys(this.storage).length;
  }

  push(element) {
    this.top++;
    this.storage[this.top] = element;
  }

  pop() {
    if(this.size() === 0){
      this.top = -1;
      return undefined;
    }
    //reference:
    // if (this.size() <= 0) {
    //   return;
    // }
    const result = this.storage[this.top];
    delete this.storage[this.top];
    this.top--;

    return result;
  }
}

hashTable

๋ณธ์ธ์ด ์ž‘์„ฑํ•œ ์ฝ”๋“œ์™€ ๋ ˆํผ๋Ÿฐ์Šค ์ฝ”๋“œ์˜ ๋น„๊ต

๊ฐ€์žฅ ํฐ ์ฐจ์ด์ ์€ ๋ฒ„ํ‚ท์„ ๊ฐ์ฒด๋กœ ์“ฐ๋Š๋ƒ ๋ฐฐ์—ด๋กœ ์“ฐ๋Š๋ƒ ์˜ ์ฐจ์ด.


linkedList

๋ ˆํผ๋Ÿฐ์Šค์ฝ”๋“œ

class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

class LinkedList {
  constructor() {
    this.head = null;
    this.tail = null;
    this._size = 0;
  }

  addToTail(value) {
    const newNode = new Node(value);

    if (this.head === null) {
      this.head = newNode;
      this.tail = newNode;
    } else {
      this.tail.next = newNode;
      this.tail = newNode;
    }

    this._size += 1;
  }

  remove(value) {
    const index = this.indexOf(value);
    if (index === -1) {
      return;
    }

    if (index === 0) {
      if (this.head === this.tail) {
        this.head = null;
        this.tail = null;
        this._size = 0;
      } else {
        this.head = this.head.next;
        this._size -= 1;
      }

      return;
    }

    const prevNode = this.getNodeAt(index - 1);
    const removedNode = prevNode.next;

    if (removedNode === this.tail) {
      prevNode.next = null;
      this.tail = prevNode;
      this._size -= 1;
      return;
    }

    prevNode.next = removedNode.next;
    this._size -= 1;
  }

  getNodeAt(index) {
    let counter = -1;

    let currentNode = this.head;
    while (currentNode) {
      counter += 1;
      if (index === counter) {
        return currentNode;
      }
      currentNode = currentNode.next;
    }

    return undefined;
  }

  contains(value) {
    return this.indexOf(value) !== -1;
  }

  indexOf(value) {
    let index = 0;

    let currentNode = this.head;
    while (currentNode) {
      if (currentNode.value === value) {
        return index;
      }
      index += 1;
      currentNode = currentNode.next;
    }

    return -1;
  }

  size() {
    return this._size;
  }
}

 


Binary Search Tree

๋ ˆํผ๋Ÿฐ์Šค์ฝ”๋“œ

class BinarySearchTreeNode {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }

  insert(value) {
    if (value < this.value) {
      if (this.left === null) {
        this.left = new BinarySearchTreeNode(value);
      } else {
        this.left.insert(value);
      }
    } else if (value > this.value) {
      if (this.right === null) {
        this.right = new BinarySearchTreeNode(value);
      } else {
        this.right.insert(value);
      }
    } else {
      // do nothing: The tree already contains this value
    }
  }
  //root => left => right ์ˆœ์œผ๋กœ value ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜๋Š”๋ฐ(์ „์œ„์ˆœํšŒ), 
  //recursion ์„ ์‚ฌ์šฉํ•˜์—ฌ ๊นŠ์ด ํƒ์ƒ‰ํ•œ๋‹ค. ์‹œ๊ฐ„๋ณต์žก๋„๋Š” O(logn) ์„ ๊ฐ–๋Š”๋‹ค. 
  contains(value) {
    if (value === this.value) {
      return true;
    }
    if (value < this.value) {
      return !!(this.left && this.left.contains(value));
    }
    //์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ๋Š๋‚Œํ‘œ๋‘๊ฐœ(!!)๋Š” ๋‹ค๋ฅธ ํƒ€์ž…์˜ ๋ฐ์ดํ„ฐ๋ฅผ Boolean ํƒ€์ž…์œผ๋กœ 
    //๋ช…์‹œ์ ์œผ๋กœ ํ˜• ๋ณ€ํ™˜(Type Conversion)ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. 
    if (value > this.value) {
      return !!(this.right && this.right.contains(value));
    }
  }

  inorder(callback) {
    if (this.left) {
      this.left.inorder(callback);
    }
    callback(this.value);
    if (this.right) {
      this.right.inorder(callback);
    }
  }
}
/* callback 
    let arr = [];
    let cb = function (value) {
      arr.push(value);
    };
    
    arr = [3, 5, 7, 8, 10, 11, 14, 15, 16];
*/

 


Graph

๋ ˆํผ๋Ÿฐ์Šค ์ฝ”๋“œ

/* Undirected Graph */
class Graph {
  constructor() {
    this.nodes = {};
  }

  addNode(node) {
    this.nodes[node] = this.nodes[node] || [];
  }

  contains(node) {
    return !!this.nodes[node];
  }

  removeNode(node) {
    if (this.contains(node)) {
      for (let i = 0; i < this.nodes[node].length; i += 1) {
        this.removeEdge(this.nodes[node][i], node);
      }
      delete this.nodes[node];
    }
  }
  //fromNode๊ฐ€ toNode Edge๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”์ง€ ์—ฌ๋ถ€ ํ™•์ธ 
  hasEdge(fromNode, toNode) {
    if (!this.contains(fromNode)) {
      return false;
    }
    return !!this.nodes[fromNode].includes(toNode);
  }

  addEdge(fromNode, toNode) {
    //this๊ฐ€ fromNode ์™€ toNode๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ๋‹ค๋ฉด, ์•„๋ž˜ if๊ตฌ๋ฌธ์„ ํ†ต๊ณผํ•œ๋‹ค. 
    if (!this.contains(fromNode) || !this.contains(toNode)) {
      return;
    }

    // Add an edge to each node pointing to the other
    if (!this.hasEdge(fromNode, toNode)) {
      this.nodes[fromNode].push(toNode);
    }

    if (!this.hasEdge(toNode, fromNode)) {
      this.nodes[toNode].push(fromNode);
    }
  }

  removeEdge(fromNode, toNode) {
    //this๊ฐ€ fromNode ์™€ toNode๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ๋‹ค๋ฉด, ์•„๋ž˜ if๊ตฌ๋ฌธ์„ ํ†ต๊ณผํ•œ๋‹ค. 
    if (!this.contains(fromNode) || !this.contains(toNode)) {
      return;
    }

    // Remove edges from each node's edge list
    if (this.hasEdge(fromNode, toNode)) {
      const index1 = this.nodes[fromNode].indexOf(toNode);
      this.nodes[fromNode].splice(index1, 1);
    }

    if (this.hasEdge(toNode, fromNode)) {
      const index2 = this.nodes[toNode].indexOf(fromNode);
      this.nodes[toNode].splice(index2, 1);
    }
  }
}

 


Tree

class TreeNode {
  constructor(value) {
    this.value = value;
    this.children = [];
  }

  insertNode(value) {

    const curNode = new TreeNode;
    curNode.value = value;
    this.children.push(curNode);

  }
  //๋ฐฉ๋ฒ•1 
  contains(value) {

    let result = false;

    function recursion(element) {

      if(element.value === value){
        result = true;
        return;
      }
      if(element.children.length > 0){
        for(let i = 0; i < element.children.length; i++){
          recursion(element.children[i]);
        }
      }
    }
    recursion(this);
    return result;
  }
  /*๋ฐฉ๋ฒ•2 (๋ ˆํผ๋Ÿฐ์Šค ์ฝ”๋“œ)
    contains(value) {
    if (this.value === value) {
      return true;
    }

    for (let i = 0; i < this.children.length; i += 1) {
      const childNode = this.children[i];
      if (childNode.contains(value)) {
        return true;
      }
    }
    return false;
  }*/
}