









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
An overview of binary trees and binary search trees, including their structure, types, and operations such as traversal, search, insertion, and deletion. It also discusses the importance of balancing binary search trees for optimal performance.
Typology: Lecture notes
1 / 15
This page cannot be seen from the preview
Don't miss anything!
subtree (부분 트리)
root (루트 노드)
leaf node (단말 노드)
internal node, intermediate node (중간 노드, 내부 노드)
포화 이진 트리(full binary tree):
완전 이진 트리(complete binary tree):
단말 노드의 수가 n 이면 전체 노드의 수는 N =2 n -1 이다.
2 일 때 단말 노드의 수가 n 이면 전체 노드의 수는 N =2 n -1 이다.
left subtree
right subtree
height: 3 (^) height: 4
height: 9 편향 이진 트리 (skewed binary tree)
public class BinarySearchTree implements BST{ protected class BSTNode{ public Object info; public BSTNode left; public BSTNode right; } protected class TreeIterator implements Iterator{ LinkedQueue traverseQueue; public TreeIterator(int type){…} public boolean hasNext(){…} public Object next(){…} public void remove(){…} private void preOrder(BSTNode node){…} private void inOrder(BSTNode node){…} private void postOrder(BSTNode node){…} } protected BSTNode root = null; protected int numOfNodes = 0; public BinarySearchTree(){} public boolean isEmpty(){ return (root==null); } public boolean isFull(){ return false; } void clear() { … } public int numOfNodes(){ return numOfNodes; } public boolean search(Object item){…} public Object retrieve(Object item){…} public void insert(Object item){…} public boolean delete(Object item){…} public Iterator iterator(int type){ return new TreeIterator(type); } }
node.left node.right
node.info
public int size(){ return size(root); }
private int size(BSTNode node){ if(node == null) return 0; // leaf node else return(size(node.left)+size(node.right)+1); }
Tip. 트리는 재귀적 구조이므로 재귀적 방법으로 구현하는 것이 쉽다.
count = 0; 트리가 empty가 아니면 다음을 스택스택을 하나 생성 루트 노드를 스택에 push 스택이 empty가 아니면 다음을 반복 현재 노드를 스택 top에 있는 노드를 가리키도록 함 스택에서 노드를 하나 pop한 다음에 count를 1 증가 만약 현재 노드가 왼쪽 자식이 있으면 그 자식을 스택에 push 만약 현재 노드가 오른쪽 자식이 있으면 그 자식을 스택에 push return count;
public boolean search(Object item){ if(isEmpty()) throw new TreeUnderflowException(“…”); if(item==null) throw new NullPointerException(“…”); Comparable x = (Comparable)item; return search(x, root); }
private boolean search(Comparable item, BSTNode node){ if(node == null) return false; int comp = item.compareTo(node.info); if(comp<0) return search(item, node.left); else if(comp>0) return search(item, node.right); else return true; }
public void insert(Object item){ if(item==null) throw new NullPointerException(“…”); Comparable x = (Comparable)item; root = insert(x, root); } private BSTNode insert(Comparable item, BSTNode node){ if(node == null){ BSTNode newNode = new BSTNode(); newNode.info = item; newNode.left = null; newNode.right = null; numOfNodes++; return newNode; } int comp = item.compareTo(node.info); if(comp<0) node.left = insert(item, node.left); else if(comp>0) node.right = insert(item, node.right); else node.info = item; return node; }
delete(8)
delete(2)
delete(9)
왼쪽 부분 트리 중 가장 큰 노드(predecessor)와 교체
predecessor를 찾아 이것을 삭제할 노드의 값과 바꿈 predecessor 노드를 삭제
protected class TreeIterator implements Iterator{ LinkedQueue traverseQueue; public TreeIterator(int type){ traverseQueue = new LinkedQueue(); switch(type){ case INORDER: inOrder(root); return; case PREORDER: preOrder(root); return; case POSTORDER: postOrder(root); return; } // switch } public boolean hasNext() { return !traverseQueue.isEmpty(); } public Object next(){ return traverseQueue.deq(); } public void remove(){ return new UnsupportedOperationException(“…”); } private void preOrder(BSTNode node){ … } private void inOrder(BSTNode node){ if(node!=null){ if(node.left!=null) inOrder(node.left); traverseQueue.enq(node.info); if(node.right!=null) inOrder(node.right); } } private void postOrder(BSTNode node){ … } }
O(N) O(1) O(N)
O(log 2 N) O(N) O(N)
O(log 2 N) O(log 2 N) O(log 2 N)
delete Find process Total
O(N) O(1) O(N)
O(log 2 N) O(N) O(N)
O(log 2 N) O(1) O(log 2 N)
insert Find process Total
O(N) O(1) O(N)
O(log 2 N) O(1) O(log 2 N)
O(log 2 N) O(1) O(log 2 N)
retrieve Find process Total
search O(log 2 N) O(log 2 N) O(N)
constructor O(1) O(N) O(1)
BST* 배열 리스트 연결 리스트
*. 균형 트리일 경우에만 O(log 2 N)
postorder: 3 7 8 5 12 10
inorder
preorder
postorder
preorder: 10 5 3 8 7 12
inorder: 3 5 7 8 10 12