광고
광고
반응형
class node { private data: String; public left: node|null; public right: node|null; constructor(data: String) { this.data = data; this.left = null; this.right = null; } preorder(cur:node|null) { if (cur == null) { return; } console.log(cur.data); this.preorder(cur.left); this.preorder(cur.right); } inorder(cur:node|null) { if (cur == null) { return; } this.preorder(cur.left); console.log(cur.data); this.preorder(cur.right); } postorder(cur:node|null) { if (cur == null) { return; } this.preorder(cur.left); this.preorder(cur.right); console.log(cur.data); } } class BinaryTree { public root: node|null; constructor() { this.root = null; } } const binaryTree = new BinaryTree(); binaryTree.root = new node("A"); binaryTree.root.left = new node("B"); binaryTree.root.right = new node("C"); binaryTree.root.left.left = new node("D"); binaryTree.root.left.right = new node("E"); binaryTree.root.right.left = new node("F"); binaryTree.root.right.right = new node("G"); binaryTree.root.right.left.left = new node("L"); binaryTree.root.right.left.right = new node("M");
Node라는 키워드가 타입스크립트에 이미 명세되어있어 node라고 표기하였다.
insert, remove, delete 코드는 이진 탐색트리가 아니기에 따로 구현하지 않았다.
간단하게 타입스크립트 연습 겸 자료구조, 알고리즘 코드를 올려보고자 한다.
반응형
'Basic > TypeScript' 카테고리의 다른 글
error TS18046: 'err' is of type 'unknown' 에러 해결 (0) | 2023.05.12 |
---|---|
[Typescript] Type guard 개념 및 예시 (0) | 2023.03.19 |
TypeScript란? (0) | 2022.10.30 |