728x90
반응형
트리 구조가지고 있는 예시들
- HTML DOM
- 네트워크 라우팅
- 인공지능
- 추상 구문 트리
- 컴퓨터 파일 시스템
*추상 구문 트리
-프로그래밍 언어로 작성된 소스코드의 추상 구조
https://ko.wikipedia.org/wiki/%EC%B6%94%EC%83%81_%EA%B5%AC%EB%AC%B8_%ED%8A%B8%EB%A6%AC
추상 구문 트리 - 위키백과, 우리 모두의 백과사전
위키백과, 우리 모두의 백과사전. 유클리드 호제법을 사용하여 다음의 코드를 나타낸 추상 구문 트리: while b ≠ 0 if a > b a := a − b else b := b − a return a 컴퓨터 과학에서 추상 구문 트리(abstract sy
ko.wikipedia.org
*이진 탐색 트리는 정렬되어있는 트리
이진 탐색 트리
class Node{
constructor(value){
this.value=value
this.left=null
this.right=null
}
}
class BinarySearchTree{
constructor(){
this.root=null
}
...
▷ Insert
insert(value){
var newNode=new Node(value)
if(this.root===null){
this.root=newNode
return this
}else{
var current=this.root
while(true){
if(value===current.value) return undefined
if(value<current.value){
if(current.left===null){
current.left=newNode
return this
}else{
current=current.left
}
}else if(value>current.value){
if(current.right===null){
current.right=newNode
return this
}
else{
current=current.right
}
}
}
}
}
▷ Search
find(value){
if(this.root===null) return undefined
var current=this.root;
found=false
while(current && !found){
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(value){
if(this.root===null) return undefined
var current=this.root;
found=false
while(current && !found){
if(value <current.value){
current=current.left
}else if(value>current.value){
current=current.right
}else{
return true
}
}
return false
}
}
Big O
Best or Average
Insertion - O(log n)
Searching - O(log n)
728x90
반응형
'알고리즘(Algorithm)' 카테고리의 다른 글
힙 + 우선 순위 큐 (0) | 2022.08.26 |
---|---|
스택+큐 (0) | 2022.08.23 |
트리 순회 (0) | 2022.08.22 |
이중 연결 리스트 (0) | 2022.08.16 |
단일 연결 리스트 (1) | 2022.08.08 |