[자료구조] 이진트리

2022. 11. 28. 15:33·개발/자료구조
728x90
반응형

1. 트리

연결리스트는 선형으로 한가지를 선택한다.

반면에 트리는 비선형으로 여러가지 선택을 할수 있다.

트리는 무조건 자식 노드만 가리켜야 하고 부모나 형제를 가리킬수 없다는 규칙이 있다.

 

트리 예시

2. 이진트리

트리중 이진 트리는 부모가 가질수 있는 최대 자식노드는 2개이다.

따라서 노드는 2개 이하의 자식노드를 가질 수 있다.

또한 이진트리는 자기보다 작은수는 왼쪽, 큰수는 오른쪽에 배치한다는 특징을 가지고 있다.

출처 : 위키디피아

3. 트리 용어

root : 꼭대기 노드

child : 자식 노드

parent : 부모 노드

siblings : 부모가 같은 자식들

leaf : 자식이 없는 노드

edge :  노드에서 다른 노드로 향하는 화살표

 

4. 이진트리 클래스

(1) 클래스 및 노드

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

class BinarySearchTree {
  constructor() {
    this.root = null;
  }
}

(2) insert

insert(val) {
    let newNode = new Node(val);
    if (!this.root) {
      this.root = newNode;
      return this;
    }

    let current = this.root;
    while (true) {
      if (val === current.value) return undefined;
      if (current.value < val) {
        if (!current.left) {
          current.left = newNode;
          return this;
        }
        current = current.left;
      }

      if (current.value > val) {
        if (!current.right) {
          current.right = newNode;
          return this;
        }
        current = current.right;
      }
    }
  }

(3) find

find(val) {
    if (!this.root) return false;
    let current = this.root,
      found = false;
    while (current && !found) {
      if (val < current.value) {
        current = current.left;
      } else if (val > current.value) {
        current = current.right;
      } else {
        found = true;
      }
    }
    return found ? current : undefined;
  }

5. 이진트리 빅O

insertion(삽입) searching(탐색)
O(logn) O(logn)

 

728x90
반응형

'개발 > 자료구조' 카테고리의 다른 글

[자료구조] 이진힙 - BinaryHeap  (0) 2022.12.02
[자료구조] 너비 우선 탐색(BFS), 깊이 우선 탐색(DFS)  (0) 2022.11.28
[자료구조] 스택-큐  (0) 2022.11.28
[자료구조] 양방향 연결리스트  (0) 2022.11.22
[자료구조] 단방향 연결리스트  (1) 2022.11.22
'개발/자료구조' 카테고리의 다른 글
  • [자료구조] 이진힙 - BinaryHeap
  • [자료구조] 너비 우선 탐색(BFS), 깊이 우선 탐색(DFS)
  • [자료구조] 스택-큐
  • [자료구조] 양방향 연결리스트
TeTedo.
TeTedo.
  • TeTedo.
    TeTedo 개발 일기
    TeTedo.
  • 전체
    오늘
    어제
    • 분류 전체보기 (319)
      • 개발 (274)
        • Article (4)
        • 정리 (21)
        • Spring Boot (17)
        • JPA (2)
        • JAVA (6)
        • Database (4)
        • 자료구조 (11)
        • 알고리즘 (32)
        • React (20)
        • Docker (10)
        • node.js (18)
        • Devops (11)
        • Linux (4)
        • TypeScript (3)
        • Go (10)
        • HyperLedger (4)
        • BlockChain (43)
        • html, css, js (48)
        • CS (3)
        • AWS (3)
      • 모아두고 나중에 쓰기 (3)
      • 팀프로젝트 (18)
        • SNS(키보드워리어) (9)
        • close_sea (9)
      • 개인프로젝트 (1)
        • Around Flavor (1)
        • CHAM (13)
        • ethFruitShop (5)
      • 독서 (0)
        • 스프링부트와 AWS로 혼자 구현하는 웹 서비스 (0)
  • 블로그 메뉴

    • 홈
    • 개발일기
    • CS
    • 실습
    • 코딩테스트
    • 웹
    • Go
    • node.js
    • 팀플
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    go언어
    30일챌린지
    컨테이너
    도커
    js
    CSS
    ERC721
    React
    erc20
    node.js
    mysql
    30일 챌린지
    html
    블록체인
    명령어
    node
    go
    프로그래머스
    nodejs
    하이퍼레저
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.1
TeTedo.
[자료구조] 이진트리
상단으로

티스토리툴바