javascript 큐, 스택, 트리

JIWON LEE·2021년 3월 5일

데이터의 추상적 형태와 데이터 다루는 방법만 정해놓은 것
ADT(Abstract Data Type) 추상 자료형 이라고 한다.

  • 선형 자료형
  • 먼저 집어넣은 데이터 먼저 나옴 (FIFO)

JavaScript 에서는 배열을 이용해 간단히 구현

class Queue{
	this._arr = []
}
enqueue(item){
	this._arr.push(item);
}
dequeue(){
	return this._arr.shift();
}

스택

  • 선형 자료형
  • 나중에 집어넣은 데이터가 먼저 나옴 (LIFO)
  • push pop peek

JavaScript 에서는 배열을 이용해 간단히 구현

class Stack{
	constructor(){
    	this._arr = [];
    }
    push(item){
    	this._arr.push(item);
    }
    pop(){
    	return this._arr.pop();
    }
    peek(){
    	return this._arr[this._arr.length -1];
    }
}

트리

  • 노드로 구성
class Node{
	constructor(content, children = []){
    	this.content = content;
        this.children = children;
    }
}
profile
포기잘하는 프론트엔드 개발자

0개의 댓글