RB tree๋ ์๊ฐ ๊ท ํ ์ด์ง ํ์ ํธ๋ฆฌ(Self-Balanced Binary Search Tree)์ ํ ์ข ๋ฅ๋ก, ๊ฒ์, ์ฝ์ , ์ญ์ ๋ฑ์ ๋์์ ์ต์ ์ ๊ฒฝ์ฐ์๋ O(log n) ๋ด์ ์ฒ๋ฆฌํ ์ ์๋๋ก ์ค๊ณ๋์๋ค. RB tree๋ ๊ฐ ๋ ธ๋๊ฐ ๋นจ๊ฐ ๋๋ ๊ฒ์ ์ ์๊น์ ๊ฐ์ง๋ฉฐ, root๋ถํฐ leaf(NIL)๊น์ง์ ๋ชจ๋ ๊ฒฝ๋ก์ ๋ํด ๋ธ๋ ๋ ธ๋์ ์๊ฐ ๋์ผํ๊ฒ ์ ์ง๋๋๋ก ๊ท์น์ ์ ์ฉํ์ฌ ๊ท ํ์ ๋ง์ถ๋ค. RB tree๋ฅผ ์ฌ์ฉํ๋ ์ด์ ๋ ๋ฐ์ดํฐ์ ์ฝ์ , ์ญ์ , ๊ฒ์ ์์ ์ 'ํญ์ ๊ท ํ ์ํ๋ฅผ ์ ์ง'ํจ์ผ๋ก์จ, ํธ๋ฆฌ์ ๋์ด๊ฐ ์ต์ํ์ผ๋ก ์ ์ง๋์ด ํจ์จ์ ์ธ ์๊ฐ ๋ณต์ก๋๋ฅผ ๋ณด์ฅํ๋ค.
Binary Search Tree๋,
- ์ ๋ ฌ๋(ordered, or sorted) binary tree์ด๋ฉฐ
- ๋ ธ๋๋ 2๊ฐ์ ์๋ธํธ๋ฆฌ๋ฅผ ๊ฐ์ง ์ ์์
- ํน์ ๋ ธ๋์ ์ผ์ชฝ์ ์๋ ํญ๋ชฉ์ด ์ค๋ฅธ์ชฝ์ ์๋ ํญ๋ชฉ๋ณด๋ค ๋ ์์
Example
๊ฐ๋จํ ๊ตฌ์กฐ์ BST์ด๋ค. ๋ค๋ง ์ต์๋จ์ root node์์ ํ ์ชฝ(์ผ์ชฝ ๋๋ ์ค๋ฅธ์ชฝ)์ผ๋ก ๋ ธ๋๊ฐ ์ ๋ ค ์์ผ๋ฉด ์ด๋ป๊ฒ ๋ ๊น? ์ต์ ์ ๊ฒฝ์ฐ ๋ชจ๋ ๋ ธ๋๋ฅผ ํ์ํ์ฌ์ผ ์ํ๋ ๋ ธ๋๋ฅผ ์ฐพ์ ์ ์๋ค.
์ด๋ฐ ๋นํจ์จ์ ํด๊ฒฐํ Search Tree๊ฐ Balanced Search Tree์ด๋ค.
Balanced Search Tree
์ด๋ n๊ฐ์ ๋ ธ๋๋ฅผ ํ์ ์ O(log n) time complexity๋ฅผ guarantee ํ๋ค.
Red - Black Tree(RB Tree)
RB Tree๋ Balanced Search Tree์ ํน์ ์ ํ ์ค ํ๋์ด๋ค.
RB Tree์ ๊ธฐ์ค(criteria)
- ๋ ธ๋๋ Red or Black
- ๋ฃจํธ(root)์ ์(leaves(NIL))์ Black
- ๋ ธ๋๊ฐ Red๋ผ๋ฉด children์ Black
- ๋ชจ๋ nodes to NIL paths์๋ ๊ฐ์ ์์ Black node๋ฅผ ๊ฐ์ง
+
- Nodes๋ ์์์ ํธ๋ํนํ๊ธฐ ์ํ bit storage๊ฐ ํ์ํจ
- ๊ฐ์ฅ ๊ธด path(root to farthest NIL)๋ ๊ฐ์ฅ ์งง์ path(root to nearest NIL)์ ๋ ๋ฐฐ๋ณด๋ค ๊ธธ ์ ์๋ค.
- ๊ฐ์ฅ ์งง์ path๋ ๋ชจ๋ Black nodes
- ๊ฐ์ฅ ๊ธด path๋ Red, Black ํจ๊ป ์กด์ฌ
RB Tree's Operation
- Search
- time complexity : O(log n)
- Insert
- time complexity : O(log n)
- RB Tree properties violation ์ rotaion ๊ธฐ๋ฒ ํ์
- Remove
- time complexity : O(log n)
- RB Tree properties violation ์ rotaion ๊ธฐ๋ฒ ํ์
์ฝ์ (Insertions)
RB Tree๋ Self-balancing Binary Search Tree์ด๋ฏ๋ก ์๋ก์ด ๋ ธ๋๋ฅผ ์ฝ์ ํ ๋ ํธ๋ฆฌ๊ฐ ์กฐ์ ๋๋ฉด์ RB Tree ์์ฑ์ด ์ถฉ์กฑ๋๋์ง ํ์ธํด์ผ ํ๋ค.
์ฝ์ ์ RB Tree properties๋ฅผ ์ ์งํ๊ธฐ ์ํด ์ฌ์ฉ๋๋ ๊ธฐ๋ฒ ์ค rotate๋ ๋ค์๊ณผ ๊ฐ๋ค.
*Rotation
- subtress๋ฅผ ์ฌ๋ฐฐ์ด(rearranging)ํ์ฌ tree ๊ตฌ์กฐ๋ฅผ ๋ฐ๊พธ๋ ๊ฒ
- ๋ชฉํ๋ tree์ height๋ฅผ ๋ฎ์ถ๊ธฐ ์ํจ!
- RB Tree : ์ต๋ height ์์ค์ O(log n)
- height๊ฐ ๋์ subtrees๋ ์ฌ๋ฆผ, ๋ฎ์ subtrees๋ ๋ด๋ฆผ
- elements์ ์์์ ์ํฅ์ ์ฃผ์ง๋ ์์(์ฌ์ ํ ์์ ์๊ฐ ์ผ์ชฝ์, ํฐ ์๊ฐ ์ค๋ฅธ์ชฝ์ ์์นํจ)
Right rotation example
- ํธ๋ฆฌ์ ํฌ์ธํฐ๋ฅผ ๋ฐ๊พธ๋ ๊ฒ๋ง์ผ๋ก rotation์ด ๊ฐ๋ฅํ๊ธฐ์ time complexity๋ O(1)
์ฝ์ ์ ๋ต
- Red Z node ์ฝ์
- Fix Violation(recolor and rotate nodes)
4๊ฐ์ง ์ฃผ์ ์๋๋ฆฌ์ค
- Z = root
- Z.uncle = red
- Z.uncle = black(triangle)
- Z.uncle = black(line)
โ๏ธ Z = root
Z๋ ์ฝ์ ๋๋ ๋ ธ๋๋ฅผ ๋ปํ๋ค.
์ด Case๋ ์ฝ์ ๋๋ ๋ ธ๋๊ฐ root๊ฐ ๋๋ ๊ฒ์ด๋ค. RB Tree ์์ฑ์ ๋ง๊ฒ Red → Black์ผ๋ก recolor๋ง ํ๋ฉด ๋๋ค.
โ๏ธ Z.uncle = red
์ฝ์ ๋๋ ๋ ธ๋์ uncle(= Z.uncle)์ color๊ฐ red์ผ ๋, Z.uncle๊ณผ Z.grandparent์ color๋ฅผ swap ํ์ฌ Fix ํ๋ค.
swap ํ violation์ ์ผ์ผํค๋ ๋ ธ๋๊ฐ Z๊ฐ ๋๋ฉฐ ์๋๋ฆฌ์ค๋ฅผ ์ฒดํฌํ๋ค.
โ๏ธ Z.uncle = black(triangle)
Z.uncle์ color๊ฐ black์ด๊ณ , Z.parent๊ฐ Z.grandparent์ right์ด๋ผ๋ฉด, Z๊ฐ Z.parent์ left์ผ ๋, Z.parent๋ฅผ RightRotate ํ๋ค.
์ญ์ผ๋ก Z.uncle์ color๊ฐ black์ด๊ณ , Z.parent๊ฐ Z.grandparent์ left์ด๋ผ๋ฉด, Z๊ฐ Z.parent์ right์ผ ๋, Z.parent๋ฅผ LeftRotate ํ๋ค.
Rotate ํ violation์ ์ผ์ผํค๋ ๋ ธ๋๊ฐ Z๊ฐ ๋๋ฉฐ ์๋๋ฆฌ์ค๋ฅผ ์ฒดํฌํ๋ค.
โ๏ธ Z.uncle = black(line)
Z.uncle์ color๊ฐ black์ด๊ณ , Z.parent๊ฐ Z.grandparent์ right์ด๋ผ๋ฉด, Z๊ฐ Z.parent์ right์ผ ๋, Z.parent๋ฅผ LeftRotate ํ๋ค.
์ญ์ผ๋ก Z.uncle์ color๊ฐ black์ด๊ณ , Z.parent๊ฐ Z.grandparent์ left์ด๋ผ๋ฉด, Z๊ฐ Z.parent์ left์ผ ๋, Z.parent๋ฅผ RightRotate ํ๋ค.
Rotate ํ violation์ ์ผ์ผํค๋ ๋ ธ๋๊ฐ Z๊ฐ ๋๋ฉฐ ์๋๋ฆฌ์ค๋ฅผ ์ฒดํฌํ๋ค.
์ ์๋๋ฆฌ์ค๋ฅผ ๊ฑฐ์น ํ RB Tree properties๋ฅผ ๋ง์กฑ์ํฌ ๊ฒฝ์ฐ ์ฝ์ ์๋ฃ๊ฐ ๋๋ค.
๐ sudo Code
