2014년 5월 16일 금요일

Red-Black Tree2

저번 포스팅에 이어 레드 블랙 트리가 노드의 추가, 삭제 시 규칙을 준수하도록 하여 트리가 균형을 유지하는 과정과 소스코드를 한번 공부해봤습니다.






<노드의 삽입>
레드 블랙 트리에 노드를 삽입하기 위해서 먼저 이진탐색트리와 같이 노드의 데이터 값을 기준으로 삽입할 위치를 이진탐색으로 찾아냅니다.






여기서 노드의 삽입으로 무너졌을지도 모를 레드-블랙 트리의 규칙을 살펴보겠습니다.






※ 레드-블랙 트리의 규칙
1. 모든 노드는 Red or Black
2. 루트 노드는 검은색
3. 잎 노드는 검은색
4. 빨간 노드의 자식 노드는 모두 검은색 but 검은색 노드의 자식이 빨간색일 필요는 없음
5. 루트 노드에서 모든 잎 노드 사이에 있는 경로의 검은색 노드 수는 모두 동일






먼저 새로운 노드를 추가할 때 무조건 추가되는 노드는 빨간색으로 가정하겠습니다.






이렇게 가정하는 이유는 일단 5번 규칙을 만족시키기 위해서 입니다.






무조건 추가되는 노드가 빨간색이라면 아무리 빨간색 노드가 많이 추가되어도 루트에서 잎 노드로 가는 경로의 검정 노드의 수는 항상 같기 때문입니다.






그리고 새로운 노드를 Red-Black 트리에 추가한다고 1번 규칙이 무너지지도 않습니다.






왜냐하면 새로운 노드는 항상 빨간색이고 원래 트리에 있는 모든 노드는 빨간색 or 검은색이므로 1번 규칙 준수하기 때문입니다.






그리고 마지막으로 3번 규칙을 만족시켜주기 위해서 제가 만들 트리에는 잎 노드의 자식으로 검정색 더미 노드를 만들 것입니다.


즉 예를 들면









이와 같은 형식으로 레드 블랙 트리가 구성 되는데 여기서 NIL은 3번 규칙을 만족 시키기 위해서 임의적으로 추가한 아무런 데이터도 가지지 않는 더미노드입니다.


여기서 이론적으로는 위의 그림과 같지만 메모리의 낭비를 막기 위해서





이와 같은 방식으로 구성하였습니다. 어차피 NIL은 아무런 의미없는 3번 규칙만 만족시켜주기 위한 더미노드이기 때문에 이렇게 구성한다고 문제될 건 아무것도 없습니다.


여기서 새로운 노드를 추가할 경우에는 빨간색 노드가 특정 잎 노드의 자식노드로 추가되게 됩니다.


그리고 새롭게 추가된 노드의 양쪽 자식으로 NIL 더미 노드에 연결시켜 주면 새로운 노드가 추가되어도 3번 잎노드는 검은색이라는 규칙을 어기지 않게 됩니다.


즉 새로운 빨간색 노드를 추가했을 경우 1번 3번 5번 규칙은 어기지 않게 되지만 2번 4번 규칙을 어길수 있습니다.


먼저 2번 규칙을 어길경우는 새로 추가되는 노드가 트리의 첫번째, 즉 루트노드일 경우입니다.


이 경우는 단순히 그 노드를 검은색으로 바꿔주기만 하면 됩니다.


그리고 이 경우 말고도 나중에 2번 규칙이 어겨지는 경우가 있는데 그건 그 때 다시 설명 드리겠습니다.


또 4번 규칙을 어기는 경우는 리프노드(더미 리프노드가 아닌 실제 리프노드)가 빨간색인 곳의 자식노드로 새로운 노드가 추가되는 경우입니다.


여기서부터 3가지 경우에 따라서 재구성 되는데 이 때 사용하는 연산이 제가 처음 레드 블랙 트리를 포스팅 했던 곳에서 올렸던 회전 연산입니다.


(새로 추가한 노드의 부모노드가 할아버지 노드의 왼쪽 자식일 경우)


밑에서 설명할 때 레드-블랙 트리의 일부분만 가져와서 설명했습니다.


즉 할아버지 노드의 위로 부모노드 1개까지 나타냈는데 그 위로도 많은 부모노드와 형제노드가 있을 수 있다고 생각하시면 됩니다.


1) 삼촌 노드가 빨간색일 경우

이러한 경우에는 부모 노드와 삼촌 노드를 검은색으로 바꾸고 할아버지 노드를 빨간색으로 바꿉니다.

그러면 이렇게 변하게 되는데 이렇게 변했을 경우 할아버지 노드의 부모 노드가 검은색이라면 상관이 없지만 빨간색이면 다시 빨간색 노드가 빨간색 노드를 자식으로 가지지 못하는 규칙을 위반하게 됩니다.


그러나 저희는 일단 할아버지, 부모, 새로 추가한 자식 이렇게 3개의 노드만 레드블랙 트리의 규칙을 준수하도록 변환하고 그 다음 할아버지 노드의 색이 바뀜으로써 깨지게 되는 레드블랙 트리의 규칙은 다시 할아버지 노드가 새로 추가한 노드로 보면서 루트노드로 거슬로 올라가면서 재귀적으로 규칙을 적용해주면 됩니다.


즉 3대까지 규칙을 준수하도록 변환->다시 할아버지 노드를 새로 추가한 노드로 생각해서 그 노드가 1) 2) 3) 중 어떤 경우에 해당하는지 판단 후 각 경우에 따라서 변환->만약 할아버지 노드와 할아버지 노드의 부모노드가 규칙을 준수한다면 이 때는 재귀의 탈출조건 이렇게 재귀적으로 구성할 것이기 때문에 지금 생각할 것은 3대까지만 생각해서 각 경우에 따라 재구성 해주면 됩니다^^


2) 삼촌 노드가 검은색이며 새로 추가한 노드가 부모 노드의 오른쪽 자식인 경우

이러한 경우에는 부모노드를 기준으로 좌회전 연산을 해주어 3)의 경우로 바꾸어 줍니다. 즉

이런식으로 바뀌어 3)번의 경우로 넘어가게 됩니다.


3) 삼촌 노드가 검은색이며 새로 추가한 노드가 부모 노드의 왼쪽 자식인 경우

이 경우에는 부모 노드를 검은색, 할아버지 노드를 빨간색으로 칠한 다음 할아버지 노드를 기준으로 오른쪽으로 회전 시킵니다.

이렇게 바꾸면 연속되는 빨간 노드가 없기 때문에 4번 빨간 노드의 자식으로 빨간노드가 올수 없다는 규칙은 만족시킬 수 있지만 5번 루트노드에서 모든 잎노드까지의 검은색 노드 수는 같다는 5번 규칙은 깨지게 됩니다. (위의 그림에서 부모 노드의 오른쪽 서브트리 경로의 검정 노드 수가 왼쪽 서브트리 검정노드 수보다 1개 더 많다.)


따라서 추가로 새로 추가한 노드를 바로 검은색으로 바꾸어 주면 이제 5번규칙 까지 만족시키면서 또 여기서 반복 조건은 탈출하게 됩니다.(여기서 다시 재귀로 드갈 필요가 없는게 3대 세대의 루트노드가 검은색이므로 그 3대 세대 루트노드의 부모가 빨간색이든지 검은색이든지 규칙을 위반하지는 않음) 즉

최종적으로 이와 같이 변하게 됩니다.


흑 오늘 소스코드까지 포스팅 하려고 했는데 생각보다 시간이 조금 걸리네요 ^^;;


다음에는 노드 삭제시 트리가 재구성 되는 과정과 소스코드까지 올리겠습니다!

2014년 5월 14일 수요일

2014년 5월 13일 화요일

Morpheus

MorpheusMorpheusMorpheusMorpheusMorpheusMorpheusMorpheusMorpheusMorpheusMorpheusMorpheusMorpheusMorpheusMorpheusMorpheusMorpheusMorpheusMorpheusMorpheusMorpheusMorpheusMorpheusMorpheusMorpheusMorpheusMorpheusMorpheusMorpheusMorpheus

2014년 5월 11일 일요일

Morpheus3D

Morpheus3DMorpheus3DMorpheus3DMorpheus3DMorpheus3DMorpheus3DMorpheus3DMorpheus3DMorpheus3DMorpheus3DMorpheus3DMorpheus3DMorpheus3DMorpheus3DMorpheus3DMorpheus3DMorpheus3DMorpheus3DMorpheus3DMorpheus3DMorpheus3DMorpheus3DMorpheus3DMorpheus3DMorpheus3DMorpheus3DMorpheus3DMorpheus3DMorpheus3DMorpheus3DMorpheus3DMorpheus3DMorpheus3DMorpheus3DMorpheus3DMorpheus3DMorpheus3DMorpheus3DMorpheus3DMorpheus3DMorpheus3DMorpheus3DMorpheus3D

2014년 5월 8일 목요일

Red-Black Tree1

최근에 레드-블랙 트리에 대해서 공부하고 있었는데 정리 & 공유 위해서 자료 올립니다~^^

먼저 레드-블랙 트리가 탄생하게 된 이유는 이진탐색트리에서 시작합니다.

데이터를 이진탐색트리로 구성해 놓는 이유는 데이터의 값을 기준으로 빠르게 원하는 값을 찾

거나 추가, 삭제하기 위해서 입니다. 이렇게 데이터를 구성해 놓으면 최선의 경우 O(logN)의

효율로 데이터를 검색, 추가, 삭제 가능하다군요.

그러나 이진탐색트리에서 노드를 추가해나가다 보면 트리가 불균형하게 자라는 경우가 발생

할 수 있습니다.  예를 들면 데이터를 1,2,3,4,5의 순서로 이진탐색트리에 추가하게 된다면 (

이 경우 데이터 값의 크기를 기준으로 큰 값은 오른쪽 자식, 작은 값은 왼쪽 자식으로 구성

하였습니다.) 아래 그림과 같은 형태로 이진탐색트리가 구성되게 됩니다.

 
이렇게 되면 데이터를 찾아가는 시간복잡도도

선형적으로 되기 때문에 O(N)의 형태에 가까워

집니다. 그래서 이렇게 이진 탐색트리가 불균형

하게 자라는 것을 방지하기 위해서 데이터 추가

,삭제할 때마다 트리의 균형을 잡아주기 위하여

레드-블랙 트리가 나왔다고 합니다~!





레드-블랙 트리가 균형을 유지하는 비결은 다음의 6가지 규칙을 준수하기 때문인데요. 6가지

의 규칙으로는

1. 노드는 레드 or 블랙 중 하나의 색을 가진다.

2. 루트 노드는 반드시 블랙이다.

3. 모든 잎 노드는 블랙이다.

4. 레드노드의 자식노드는 블랙이다. 블랙노드의 자식으로는 어떤 노드가 와도 상관이 없다.

5. 루트노드에서 모든 잎노드까지의 경로상의 블랙노드의 개수는 모두 같다.

6. 새로 추가되는 노드는 레드노드이다.

이진탐색트리가 이런 규칙을 만족시킨다면은 이 트리의 특정 노드의 왼쪽 서브트리와 오른쪽

서브트리간에 2배 이상 노드의 불균형이 발생하지 않습니다.(2배이상 왼쪽 서브트리와 오른

쪽 서브트리의 높이차이가 발생하지 않습니다) 왜냐하면은 4번 조건으로 레드노

드는 자식으로 레드노드를 가지지 못하고(즉 연속으로 레드노드가 2번 나오지 못하고) 5번 조

건으로 루트노드에서 특정 리프노드로 가는 경로상의 모든 블랙노드의 갯수는 같아야 하기 때

문이죠.


그리고 레드-블랙 트리에서는 노드의 추가, 삭제 연산을 하고 트리를 균형잡힌 트리로 재 구성

하기 위해서 회전이라는 연산도 제공합니다. 회전은 우회전과 좌회전이 있습니다.

우회전은 특정 노드의 왼쪽 자식노드를 부모노드로 바꾸고 왼쪽 자식노드의 오른쪽 자식노드

를 부모노드로 연결하는 연산입니다. 이 때 원래 있던 왼쪽자식의 오른쪽 자식노드는 부모의

모의 왼쪽 자식노드로 변경하여 줍니다.

즉 위의 그림과 같이 됩니다. 그리고 좌회전은 우회전의 반대로 생각하시면 됩니다. 오늘은 여

기까지 올리고 다음에는 삽입, 삭제 시 어떻게 레드블랙트리가 재구성 되어 균형을 유지하는

지와 직접 소스코드 수준으로 만들어서 올려보겠습니다!~ ^^

2014년 5월 6일 화요일

Morpheus3D Morpheus3D

Morpheus3D Morpheus3D Morpheus3D Morpheus3D Morpheus3D Morpheus3D Morpheus3D Morpheus3D Morpheus3D Morpheus3D Morpheus3D Morpheus3D Morpheus3D Morpheus3D Morpheus3D Morpheus3D Morpheus3D Morpheus3D Morpheus3D Morpheus3D Morpheus3D Morpheus3D Morpheus3D Morpheus3D Morpheus3D Morpheus3D Morpheus3D Morpheus3D Morpheus3D Morpheus3D Morpheus3D

Morpheus3D

Morpheus3D Morpheus3D Morpheus3D Morpheus3D Morpheus3D Morpheus3D Morpheus3D Morpheus3D Morpheus3D Morpheus3D Morpheus3D Morpheus3D Morpheus3D Morpheus3D Morpheus3D Morpheus3D Morpheus3D Morpheus3D Morpheus3D Morpheus3D Morpheus3D Morpheus3D Morpheus3D Morpheus3D Morpheus3D Morpheus3D Morpheus3D Morpheus3D

Morpheus3d

Morpheus3d

Morpheus

MorpheusMorpheusMorpheusMorpheusMorpheusMorpheusMorpheusMorpheusMorpheusMorpheusMorpheusMorpheusMorpheusMorpheusMorpheusMorpheusMorpheusMorpheusMorpheusMorpheusMorpheusMorpheusMorpheusMorpheusMorpheusMorpheus

2014년 5월 3일 토요일

Morpheus

How to meet James Gosling.









BIO
Here's a brief academic-conference-style bio and some images:
James Gosling received a BSc in Computer Science from the University of Calgary, Canada in 1977. He received a PhD in Computer Science from Carnegie-Mellon University in 1983. The title of his thesis was "The Algebraic Manipulation of Constraints". He spent many years as a VP & Fellow at Sun Microsystems. He has built satellite data acquisition systems, a multiprocessor version of Unix, several compilers, mail systems and window managers. He has also built a WYSIWYG text editor, a constraint based drawing editor and a text editor called `Emacs' for Unix systems. At Sun his early activity was as lead engineer of the NeWS window system. He did the original design of the Java programming language and implemented its original compiler and virtual machine. He has been a contributor to the Real-Time Specification for Java, and a researcher at Sun labs where his primary interest was software development tools. He then was the Chief Technology Officer of Sun's Developer Products Group and the CTO of Sun's Client Software Group. He briefly worked for Oracle after the acquisition of Sun. After a year off, he spent some time at Google and is now the chief software architect at Liquid Robotics where he spends his time writing software for the Waveglider, an autonomous ocean-going robot.


Long days in HawaiiMonday October 21, 2013
I'm still hacking away on robots... We're getting down to the finish line on our new robot model. It always amazes me that the big stuff is often easy, it's the little stuff that kills. Autonomously avoiding ships.... Easy! Getting a compass to report reliable headings.... Hard!For a small break from the grind, Stephen Chin from Oracle will be visiting and doing a live streaming video conversation here with me as part of his nighthacking tour. Should be fun. Tune in :-)

I haven't disappeared...Monday April 8, 2013
I've been in the clutches of a product release. It's been real Soul of a New Machine stuff. We've just announced the newest robot, so its a lot easier for me to talk. While it looks roughly the same as the previous machine, and operates on similar principles, every part is different. Every piece of hardware, the electronics, and a total software rewrite. Most important to me is that it's now got a real computer onboard: an ARM processor with more than enough horsepower to do interesting things. One of the more interesting things it does is autonomous navigation. We funded part of this work using a SBIR grant. They require progress reports, for which I did a couple (so far) of videos:
Episode 1: basic autonomy: avoiding obstacles from James Gosling on Vimeo.
Episode 2: Autonomous Ship Avoidance from James Gosling on Vimeo.

It's long past the time.....Sunday December 16, 2012

(as seen on Richard Branson's blog)

PapaMau arrives in AustraliaMonday November 19, 2012
No, I haven't disappeared. I've just been having way too much fun coding. It is so incredible being a user of the Java ecosystem. I'm always amazed at what's out there. And the tools... I've been using Netbeans, Jenkins, GitBlit, Jira and Artifactory. All lovely!The first of our 4 transpacific voyagers has made it to Australia. After a hectic final dash through the East Australia Current, Papa Mau is going to be picked up in the next day or two (the white line on the map). It got far ahead of the other waveglider headed for Australia (Benjamin) when the other took some damage to it's solar panel, so we had it detour (the red line) through Samoa and have a pit stop at a beach resort - our repair technicians get the best business trips.

JavaFX EnsembleThursday October 18, 2012
It's been ridiculously long since I've last written a blog entry. Lots has happened (like giving a talk at JavaOne, which was fun). But life has been such a mad dash that I didn't even blog that. But this evening someone pointed me at something so cool I just had to write something: JavaFX Ensemble. It's a really nicely put together app that demonstrates all the amazing stuff in the new JavaFX. What a lovely new world of UIs. There's a lot more to it than I knew. I can't wait to start playing with it.As cool as the new JavaFX is, I was doubly impressed by the fact that it's in the Apple app store. What an earthquake... Tectonic action...
If you're not reading this on a Mac, you can try out Ensemble here.

Working crazy hardTuesday August 21, 2012
Having lots of fun. My usually-slow blogging rate has gone totally into the trash. A couple of weeks ago I did a talk on some of what I'm working on. There were some issues with the recording: it's missing the animations in the slides and the demos. But you'll get the idea :-) The PacX challenge robots are strolling across the pacific, getting closer and closer to Australia and Japan. One major sadness is that one of the robots heading for Japan (Fontaine Maru) came to an unknown end - it's drifting with everything dead except its tracking beacon.I finally got a job opening for a JavaEE/cloud hacker, based in Sunnyvale. If you're looking for something different, apply!

Morpheus 3D

Morpheus 3D Morpheus 3D Morpheus 3D Morpheus 3D Morpheus 3D Morpheus 3D Morpheus 3D Morpheus 3D Morpheus 3D Morpheus 3D Morpheus 3D Morpheus 3D Morpheus 3D Morpheus 3D Morpheus 3D Morpheus 3D Morpheus 3D Morpheus 3D Morpheus 3D Morpheus 3D Morpheus 3D Morpheus 3D

Morpheus3D

Morpheus3D

대학원생의 투고 지도 ④ (완결) — 논문의 수준이란 무엇인가: 사다리와 사분위, 첫 논문 전략

완결편이다. "논문 수준"이라는 말을 해부하고, 에듀테크 석사생의 첫 논문 전략으로 마친다. 학위논문과 학술지 논문은 다른 장르다 먼저 가장 흔한 혼동부터. 학위논문(thesis)은 "내가 연구를 수행할 줄 안다...