Upload
buzz
View
51
Download
0
Embed Size (px)
DESCRIPTION
문병로 교수님 강의자료를 많이 인용하였습니다. Data Structure. S amsung C onvergence S oftware C ourse 방학 특강 2014. 6. 23 ( 월 ). 들어가기 전에. 단대가 다들 어떻게 되시나요 ? 인문대 / 사회대 / 경영대 / 자연대 / 공대 프로그래밍 , 얼마나 아시나요 ? 프로그래밍 해본 적 있다 / 없다 어떤 언어 ? 객체 지향 프로그래밍 / 자료 구조에 대해 아시나요 ? 객체 지향 프로그래밍에 대해 안다 / 모른다 - PowerPoint PPT Presentation
Citation preview
DATA STRUCTURESamsung Convergence Software Course
방학 특강2014. 6. 23 ( 월 )
문병로 교수님 강의자료를 많이 인용하였습니다 .
들어가기 전에• 단대가 다들 어떻게 되시나요 ?
• 인문대 / 사회대 / 경영대 / 자연대 / 공대
• 프로그래밍 , 얼마나 아시나요 ?• 프로그래밍 해본 적 있다 / 없다• 어떤 언어 ?
• 객체 지향 프로그래밍 / 자료 구조에 대해 아시나요 ?• 객체 지향 프로그래밍에 대해 안다 / 모른다• Linked List, Stack, Queue, Tree 에 대해 안다 / 모른다
저는…• 서울대학교 컴퓨터공학부• 지능형 데이터 시스템 연구실 석박통합과정
• 데이터 마이닝 , 추천 , 텍스트 데이터 분석 , 빅데이터 병렬 처리 , 등등
• Interested in…• 사용자들이 남겨놓은 다양한 데이터들 (SNS/ 리뷰 등 텍스트 기록 , 상품구매 기록 , 웹
서핑 기록 , TV 시청 기록 , 장소 체크인 기록 , 등 )• 이를 활용하여 사용자의 Preference 를 예측하여 사용자들에게 다양한 Contents /
Item / Service 들을 추천하는 연구
• 이한빛 , [email protected]
저는…• 서울대학교 컴퓨터공학부• 지능형 데이터 시스템 연구실 석박통합과정
• 데이터 마이닝 , 추천 , 텍스트 데이터 분석 , 빅데이터 병렬 처리 , 등등
• Interested in…• 사용자들이 남겨놓은 다양한 데이터들 (SNS/ 리뷰 등 텍스트 기록 , 상품구매 기록 , 웹
서핑 기록 , TV 시청 기록 , 장소 체크인 기록 , 등 )• 이를 활용하여 사용자의 Preference 를 예측하여 사용자들에게 다양한 Contents /
Item / Service 들을 추천하는 연구
• 이한빛 , [email protected]
데이터 , Data
• 46
• 18.4
• “ 정도전”• true/false
• “ 조재현” , “ 유동근” , “ 박영규”
int curr_ep_num = 46;
double ratings = 18.4;
String title = “ 정도전” ;
boolean live_yn = true;
조금 더 높은 추상 단계의 데이터 ?예를 들어 , TV_Program?
String[ ] cast = {“ 조재현” , “ 유동근” , “ 박영규” };
데이터 , Data
• TV_program?• 정도전 , 너포위 , 마녀사냥 , 아빠어디가 , …
• TV_program 이 공통적으로 가지는 정보 ? 속성 ?• 프로그램이름 , 방영시간 , 장르 , 출연진 , 시청률 , …
• 이 정보 / 속성들을 이용해 할 수 있는 기능 ?• 방영시간 변경 , 출연진 리스트 가져오기 , …
데이터 구조 , Data Structure
• 앞에서 말한 개념적인 데이터를 담는 구체적인 구현 .
• Object-Oriented Programming ( 객체 지향 프로그래밍 )
• Abstraction ( 추상화 )
Java 의 경우에는…Class TV_program {
String pg_title;int pg_duration;String genre;double rating;
public String getPg_title() {return this.pg_title;
}public void setPg_title(String new_title) {
this.pg_title = new_title;}…
}
field
method
객체 지향 프로그래밍 , Object-oriented Programming
정도전 왕좌의 게임 마녀사냥 슈퍼맨이 돌아왔다
TV Program
객체 지향 프로그래밍 , Object-oriented Programming
Class TV_program { String pg_title; int pg_duration; String genre; double rating;
public String getPg_title() {return this.pg_title;
} public void setPg_title(String new_title) {
this.pg_title = new_title; } …}
TV_program 정도전 = new TV_program ( );
개념 (Concept)
객체 (Object)
객체 지향 프로그래밍 , Object-oriented Programming
Class TV_program { String pg_title; int pg_duration; String genre; double rating;
public String getPg_title() {return this.pg_title;
} public void setPg_title(String new_title) {
this.pg_title = new_title; } …}
TV_program 왕좌의게임 = new TV_program ( )
개념 (Concept)
객체 (Object)
객체 지향 프로그래밍 , Object-oriented Programming
Class TV_program { String pg_title; int pg_duration; String genre; double rating;
public String getPg_title() {return this.pg_title;
} public void setPg_title(String new_title) {
this.pg_title = new_title; } …}
TV_program 마녀사냥 = new TV_program ( )
개념 (Concept)
객체 (Object)
객체만 만들면…TV_program 정도전 = new TV_program( );
정도전 .setPg_name(“ 대하드라마 , 정도전” );
정도전 .setPg_duration(1);
정도전 .setGenre(“drama”);
정도전 .setRating(18.4);
TV_program 정도전 = new TV_program(“ 정도전” , 1, “drama”, 18.4);
다시 , 클래스• 필드 , field
• 생성자 , constructor
• 메쏘드 , method• Getter/Setter : 모든 필드 변수들에 대해 보통 구현됨• 이외의 method 들
Class TV_program {
String pg_title; int pg_duration; String genre; double rating;
TV_program( ) {this.pg_title = null;this.pg_duration = 1;this.genre = null;this.rating = 0.0;
}
TV_program( String title, int dur, String genre, double rating) {this.pg_title = title;this.pg_duration = dur;this.genre = genre;this.rating = rating;
}
public String getPg_title() {
return this.pg_title;
}
public void setPg_title(String new_title) {
this.pg_title = new_title;
}
… (also for pg_duration, genre, rat-ing)
public double ratingToPercent( ) {
return (this.rating * 100);
}
}
field
constructor
Getter/setter
Other method
사용자 정의 데이터 타입
Class TV_program { String pg_title; int pg_duration; String genre; double rating;
public String getPg_title() {return this.pg_title;
} public void setPg_title(String new_title) {
this.pg_title = new_title; } …}
Class EPG_schedule { int year; int month; int day; String dayofweek; TV_program[ ] pro-gram_list;
…}
Java 의 OOP 적 기능들• 상속 , inheritance
• 부모클래스와 자식클래스 . 자식클래스는 부모클래스의 것들을 상속받아 재정의하지않고 사용하면서 자신만의 새로운 것들을 정의하여 사용 가능 .
• 오버로딩 , overloading• 하나의 이름으로 된 함수들을 여러 타입의 파라미터들에 대해 사용 가능 .
• 인터페이스 , interface• 구현하는 함수들의 이름 , 입력 , 출력 형태만 적어놓고 이 인터페이스를 구현하는 클래스를
따로 두어 실제 구현을 함으로써 안의 구현을 몰라도 사용가능하도록 캡슐화 .
• 등등
• 자세한 설명은 생략한다 .
대표적인 데이터 구조• Linked List, Stack, Tree, …
• 데이터를 어떤 형태의 구조로 담을 것인가 ?
클래스로 정의하는 법만 잘 알면 된다 .
• 클래스의 필드 , 메쏘드만 잘 정의할 수 있으면 , Java 에서 어떤 데이터 구조든 자기 입맛에 맞는 데이터 구조를 정의하여 사용이 가능하다 .
• 나중에 알게 될 진실 .
연결 리스트 , Linked List
Figure 5.1
a) A linked list of integers; b) insertion; c) deletion
Linked List
• Each node contains• Data (item)• Link
number, string, object, …
public class IntegerNode {
public int item;
public IntegerNode next;
}
A naïve structure
Not good information hiding Not good data abstraction
Example: IntegerNode n1 = new IntegerNode( ); IntegerNode n2 = new IntegerNode( ); n1.item = 5; n2.item = 9; n1.next = n2;
public class IntegerNode {private int item;private IntegerNode next;public void setItem(int newItem) {
item = newItem;}public int getItem( ) {
return item;}public void setNext(IntegerNode
nextNode) {next = nextNode;
}public IntegerNode getNext( ) {
return next;}
}
Example: IntegerNode n1 = new IntegerNode( ); IntegerNode n2 = new IntegerNode( ); n1.setItem(5); n2.setItem(9); n1.setNext(n2);
An Intermediate Version
public class IntegerNode {private int item;private IntegerNode next;// constructorspublic IntegerNode(int newItem) {
item = newItem;next = null;
}public IntegerNode(int newItem, IntegerNode
nextNode) {item = newItem;next = nextNode;
}// setItem, getItem, setNext, getNext as before…
}
Example: IntegerNode n2 = new IntegerNode(9); IntegerNode n1 = new IntegerNode(5, n2);
An Improved Version
Example: IntegerNode n1 = new IntegerNode(5, new IntegerNode(9));
Head Node
•Linked lists usually have a head reference
Node head = null;
Node head = new Node(new Integer(5));
Here, head is a simple reference variable
Displaying the Contents
for (Node curr = head; curr != null; curr = curr.getNext( )) {System.out.println(curr.getItem( ));
}
• Sequential display of the contents of the linked list refer-enced by head
Deleting a Specified Node
prev.setNext(curr.getNext( ));
•Removing the node referenced by variable curr• The previous node of curr is referenced by prev
In C, prev->next = curr->next;
Removing the 1st node
head = curr.getNext( );
Inserting a Node
• Inserting a node between prev and curr
In C,newNode = malloc(sizeof Node);newNode->item = 30;newNode->next = curr;prev.next = newNode;
newNode = new Node(new Integer(30));newNode.setNext(curr);prev.setNext(newNode);
스택 , Stack
Stack•도입을 위한 예
• “←” in keyboard input line• E.g., abcd←←efgh←←←ij←km← abeik
• 한 character 를 읽어 ‘←’ 이 아니면 저장하고
‘←’ 이면 최근에 저장된 character 를 제거한다 .
Stack of cafeteria dishes
최근에 쌓은 접시를 꺼낸다
A Stack Example
Example: Checking Balance of Braces
Stack Implementation
public interface StackInterface {
public boolean isEmpty( );
public void push(Object newItem);
public Object pop( );
public void popAll ( );
public Object peek( );
}
Array-Based Implementation
public class StackArrayBased implements StackInterface {
final int MAX_STACK = 50;
private Object items[ ];
private int top; // index for stack top
public StackArrayBased( ) {
items = new Object[MAX_STACK];
top = –1;
}
0 1 2 3 4 … MAX_STACK–1
–1top Items[ ]
public boolean isEmpty( ) {return (top < 0);
}public boolean isFull ( ) {
return (top == MAX_STACK–1);}public void push(Object newItem) {
if (!isFull( )) items[++top] = newItem;else {exception 처리 }
}
public Object pop( ) {if (!isEmpty( )) return items[top--];else {exception 처리 }
}public void popAll( ) {
items = new Object[MAX_STACK];top = –1;
}public Object peek( ) {
if (!isEmpty( )) return items[top];else {exception 처리 }
}} // end class StackArrayBased
Reference-Based Implementation
public class StackReferenceBased implements StackInterface{
private Node top;
public StackReferenceBased( ) {
top = null;
}
5
public boolean isEmpty( ) {
return (top == null);
}
public void push(Object newItem) {
top = new Node(newItem, top);
}
public Object pop( ) {
if (!isEmpty( )) {
Node temp = top;
top = top.getNext( );
return temp.getItem( );
} else {exception 처리 }
}
public void popAll ( ) {top = null;
}public Object peek( ) {
if (!isEmpty( )) return top.getItem( );
else {exception 처리 }}
} // end class StackReferenceBased
트리 , Tree
An organization chart A family tree
Reference-Based Implementation of Bi-nary Tree
public class TreeNode {
private Object item;
private TreeNode leftChild;
private TreeNode rightChild;
public TreeNode(Object newItem) {
item = newItem;
leftChild = rightChild = null;
}
public TreeNode(Object newItem, TreeNode left, TreeNode right) {
item = newItem;
leftChild = left;
rightChild = right;
}
newItem
newItem
left right
public Object getItem( ) {return item;
}public void setItem(Object newItem) {
item = newItem;}public TreeNode getLeft( ) {
return leftChild;}public TreeNode getRight( ) {
return rightChild;}public setLeft(TreeNode left) {
leftChild = left;}public setRight(TreeNode right) {
rightChild = right;}
} // end TreeNode
•
left
Search in a Binary Search Tree
search(root, searchKey) {
if (root is empty) return “Not found!”;
else if (searchKey == root’s key) return root;
else if (searchKey < root’s key)
return search(root’s left child, searchKey);
else
return search(root’s right child, searchKey);
}
Insertion in a Binary Search Treeinsert (root, newItem) {
if (root is null) {
newItem 을 key 로 가진 새 node 를 매단다 ;
}
else if (newItem < root’s key)insert(root’s left child,
newItem);
else
insert(root’s right child, newItem);
}
사실 이 모든 것은 Java 에 모두 아주 잘 구현이 되어 있습니다 .• 컴퓨터 공학에서 자료구조를 배우는 이유 : Object-Oriented Programming
을 배우기에 가장 좋은 주제 .
• 사실 이론이 아닌 실제 구현을 해보는 것이 목적 .
• Java 구현에 대해 조금이나마 아셨기를 바라며 !