TREES는 고전적인 데이터 구조를 뜻한다.
트리는 연결 리스트보다는 약간 더 상위 단계이고, 더 복잡한데 좀 더 흥미로워!
트리라는 데이터 구조는 많은 종류가 있다.
우리는 이진 트리를 대부분 배울 것이다.
=> 무언가를 찾아보는 것을 아주 빠르고 쉽게 만들어준다.
=> 무언가를 추가하는 것과 노드의 자리를 찾는 것도 쉽게 해준다.
기본 토대가 되는 두개의 클래스를 만들 것이다~~
class Node{
constructor(value){
this.value = value; // 각 노드는 값을 가진다.
this.left = null; // left 값과
this.right = null; // right 값
}
}
class BinarySearchTree{
constructor(){
this.root = null; // 딱 하나의 중요한 프로퍼티를 가진다. 루트!
}
}
let tree = new BinarySearchTree()
tree.root = new Node(10);
tree.root.right = new Node(15);
tree.root.left = new Node(7);
tree.root.left.right = new Node(9);
insert 메소드 : 비교를 통해서 어떤 요소가 어디로 가야하는지 알려준다.
트리에 무언가를 삽입하는 방법을 추가하겠어!
(find, contains 함수 거의 비슷함)
class Node{
constructor(value){
this.value = value; // 각 노드는 값을 가진다.
this.left = null; // left 값과
this.right = null; // right 값
}
}
class BinarySearchTree{
constructor(){
this.root = null; // 딱 하나의 중요한 프로퍼티를 가진다. 루트!
}
insert(value){
let newNode = new Node(value);
if(this.root === null){
this.root = newNode;
return this;
}else{
let current = this.root;
while(true){
if(value < current.value){
if(current.left === null){
current.left = newNode;
return this;
}
current = current.right;
}
}
}
// 값, 노드를 출력하는 find
find(value){
if(this.root === null) return false;
let current = this.root,
found = false;
while(!found && current){
if(value < current.value){
current = current.left;
}else if(value > current.value){
current = current.right;
}else{
found = true;
}
}
if(!found) return undefined; // 값에 이 트리에 없다면
return current;
}
// 참과 거짓을 출력하는 contains고
contains(value){
if(this.root === null) return false;
let current = this.root,
found = false;
while(!found && current){
if(value < current.value){
current = current.left;
}else if(value > current.value){
current = current.right;
}else{
return true;
}
}
return false;
}
}
최고와 평균적인 경우에,
Insertion - O(logn)
Searching - O(logn)
최악의 경우에, (요소가 백만개로 늘어났는데, 여전히 한쪽으로 치우친 트리일때)
삽입이나 탐색을 할 때 취해야 하는 단계의 숫자도 노드의 숫자 증가에 따라 커진다. ㅠ
O(n)
=> 그냥 이진 트리나 이진 탐색 트리를 사용하지 않는 것이 해결책