Javascript의 데이터 구조 및 알고리즘... 시작하려면
20937 단어 binarysearchtreequeuestackjavascript
이 문서는 DS 및 알고리즘 면접을 준비하는 사람을 위한 시작점으로 작성되었습니다.
1부에서는 다룰 예정입니다.
병합 정렬 및 이진 검색을 다룹니다.
배열에서 가장 자주 발생하는 항목, 중첩 배열의 요소 합계 및 배열 청크를 찾기 위한 솔루션을 다룹니다.
1. 스택
스택은 LIFO(Last In First Out) 또는 FILO(First In Last Out)를 따르는 선형 데이터 구조입니다. 스택에는 두 가지 주요 작업이 있습니다.
푸시 - 스택 상단에 요소를 추가합니다.
팝 - 가장 최근에 추가된 요소를 제거합니다(스택 맨 위에서).
![](https://s1.md5.ltd/image/5b9cb8945b78aa67d8a61e9752fab9ef.png)
class Node {
constructor(value) {
this.value = value;
this.prev = null;
}
}
class Stack {
constructor() {
this.top = null; // no nodes
}
push(value) {
let node = new Node(value);
node.prev = this.top;
this.top = node;
}
pop() {
if (this.top) {
let value = this.top.value;
this.top = this.top.prev;
return value;
} else {
return 'Stack is empty';
}
}
}
let stack1 = new Stack(); // stack to store integers
stack1.push(1);
stack1.push(2);
stack1.push(3);
console.log(stack1.pop()); // 3
console.log(stack1.pop()); // 2
console.log(stack1.pop()); // 1
console.log(stack1.pop()); // stack is empty
2. 대기열
대기열도 선형 데이터 구조입니다. 대기열은 FIFO(First In First Out)를 따릅니다. Queue의 두 가지 주요 작업은 다음과 같습니다.
Enqueue - Queue의 끝에 요소를 추가합니다.
Dequeue - Queue의 헤드에서 요소를 제거합니다.
![](https://s1.md5.ltd/image/2f8400099a0a05d1e9895afc98294186.png)
class Node {
constructor(val) {
this.value = val;
this.next = null;
}
}
class Queue {
constructor() {
this.head = null;
this.tail = null;
}
enqueue(val) {
// to tail
let node = new Node(val);
if (!this.head) {
//if queue is empty, point head and tail to this new node
this.head = this.tail = node;
} else {
this.tail.next = node;
this.tail = node; // make new node as tail
}
}
dequeue() {
// from head
if (this.head) {
let val = this.head.value;
this.head = this.head.next;
return val;
} else {
return 'Queue is empty';
}
}
}
let q1 = new Queue();
q1.enqueue(1);
q1.enqueue(2);
q1.enqueue(3);
console.log(q1.dequeue()); // 1
console.log(q1.dequeue()); // 2
console.log(q1.dequeue()); // 3
console.log(q1.dequeue()); // Queue is empty
3. 이진 검색 트리
이진 검색 트리는 정렬되거나 정렬된 이진 트리입니다.
![](https://s1.md5.ltd/image/2c55bfda31e84c14570cb6e6f9935c8b.png)
삽입
순회
3가지 일반적인 이진 트리 순회가 있습니다.
이진 검색 트리의 순회는 항상 노드 항목의 오름차순 정렬 목록을 생성합니다. 여기에서는 순서 순회를 구현했습니다.
class Node {
constructor(val) {
this.value = val;
this.left = null;
this.right = null;
}
}
class BinarySearchTree {
constructor() {
this.root = null; // root node
}
insertNode(parentNode, newNode) {
if (newNode.value < parentNode.value) {
//check the left child
parentNode.left !== null
? this.insertNode(parentNode.left, newNode)
: (parentNode.left = newNode);
} else {
// check the right child
parentNode.right !== null
? this.insertNode(parentNode.right, newNode)
: (parentNode.right = newNode);
}
}
insert(val) {
let newNode = new Node(val);
this.root !== null
? this.insertNode(this.root, newNode)
: (this.root = newNode);
}
printNode(node) {
if (node.left !== null) {
this.printNode(node.left); // traverse left subtree
}
console.log(node.value);
if (node.right !== null) {
this.printNode(node.right); // traverse right subtree
}
}
print() {
this.root !== null
? this.printNode(this.root)
: console.log('No nodes in the tree');
}
}
let bst1 = new BinarySearchTree();
bst1.insert(50);
bst1.insert(30);
bst1.insert(10);
bst1.insert(40);
bst1.insert(20);
bst1.insert(80);
bst1.insert(70);
bst1.insert(60);
bst1.insert(100);
bst1.insert(90);
bst1.print();
도움이 되었기를 바랍니다!
이 시리즈를 확인하는 것을 잊지 마십시오.
곧 추가 예정!!
Reference
이 문제에 관하여(Javascript의 데이터 구조 및 알고리즘... 시작하려면), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/jeenajohn/data-structures-and-algorithms-in-javascript-to-get-you-started-4hel텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)