๐Ÿ‘จโ€๐Ÿ’ป Seungineer's GitHub Contribution

๐Ÿงญ KAIST JUNGLE/Algorithm

[SW ์ •๊ธ€ 26์ผ์ฐจ] Red-Black tree (#์‚ฝ์ž…)

seungineer = seungwoo + engineer 2024. 4. 8. 03:28

RB tree๋Š” ์ž๊ฐ€ ๊ท ํ˜• ์ด์ง„ ํƒ์ƒ‰ ํŠธ๋ฆฌ(Self-Balanced Binary Search Tree)์˜ ํ•œ ์ข…๋ฅ˜๋กœ, ๊ฒ€์ƒ‰, ์‚ฝ์ž…, ์‚ญ์ œ ๋“ฑ์˜ ๋™์ž‘์„ ์ตœ์•…์˜ ๊ฒฝ์šฐ์—๋„ O(log n) ๋‚ด์— ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค๊ณ„๋˜์—ˆ๋‹ค. RB tree๋Š” ๊ฐ ๋…ธ๋“œ๊ฐ€ ๋นจ๊ฐ• ๋˜๋Š” ๊ฒ€์ •์˜ ์ƒ‰๊น”์„ ๊ฐ€์ง€๋ฉฐ, root๋ถ€ํ„ฐ leaf(NIL)๊นŒ์ง€์˜ ๋ชจ๋“  ๊ฒฝ๋กœ์— ๋Œ€ํ•ด ๋ธ”๋ž™ ๋…ธ๋“œ์˜ ์ˆ˜๊ฐ€ ๋™์ผํ•˜๊ฒŒ ์œ ์ง€๋˜๋„๋ก ๊ทœ์น™์„ ์ ์šฉํ•˜์—ฌ ๊ท ํ˜•์„ ๋งž์ถ˜๋‹ค. RB tree๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๋Š” ๋ฐ์ดํ„ฐ์˜ ์‚ฝ์ž…, ์‚ญ์ œ, ๊ฒ€์ƒ‰ ์ž‘์—… ์‹œ 'ํ•ญ์ƒ ๊ท ํ˜• ์ƒํƒœ๋ฅผ ์œ ์ง€'ํ•จ์œผ๋กœ์จ, ํŠธ๋ฆฌ์˜ ๋†’์ด๊ฐ€ ์ตœ์†Œํ•œ์œผ๋กœ ์œ ์ง€๋˜์–ด ํšจ์œจ์ ์ธ ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๋ณด์žฅํ•œ๋‹ค.

Binary Search Tree๋ž€,

  1. ์ •๋ ฌ๋œ(ordered, or sorted) binary tree์ด๋ฉฐ
  2. ๋…ธ๋“œ๋Š” 2๊ฐœ์˜ ์„œ๋ธŒํŠธ๋ฆฌ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Œ
  3. ํŠน์ • ๋…ธ๋“œ์˜ ์™ผ์ชฝ์— ์žˆ๋Š” ํ•ญ๋ชฉ์ด ์˜ค๋ฅธ์ชฝ์— ์žˆ๋Š” ํ•ญ๋ชฉ๋ณด๋‹ค ๋” ์ž‘์Œ

Example

BST

๊ฐ„๋‹จํ•œ ๊ตฌ์กฐ์˜ 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)

  1. ๋…ธ๋“œ๋Š” Red or Black
  2. ๋ฃจํŠธ(root)์™€ ์žŽ(leaves(NIL))์€ Black
  3. ๋…ธ๋“œ๊ฐ€ Red๋ผ๋ฉด children์€ Black
  4. ๋ชจ๋“  nodes to NIL paths์—๋Š” ๊ฐ™์€ ์ˆ˜์˜ Black node๋ฅผ ๊ฐ€์ง

+

  1. Nodes๋Š” ์ƒ‰์ƒ์„ ํŠธ๋ž˜ํ‚นํ•˜๊ธฐ ์œ„ํ•œ bit storage๊ฐ€ ํ•„์š”ํ•จ
  2. ๊ฐ€์žฅ ๊ธด 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

  1. subtress๋ฅผ ์žฌ๋ฐฐ์—ด(rearranging)ํ•˜์—ฌ tree ๊ตฌ์กฐ๋ฅผ ๋ฐ”๊พธ๋Š” ๊ฒƒ
  2. ๋ชฉํ‘œ๋Š” tree์˜ height๋ฅผ ๋‚ฎ์ถ”๊ธฐ ์œ„ํ•จ!
    • RB Tree : ์ตœ๋Œ€ height ์ˆ˜์ค€์€ O(log n)
    • height๊ฐ€ ๋†’์€ subtrees๋Š” ์˜ฌ๋ฆผ, ๋‚ฎ์€ subtrees๋Š” ๋‚ด๋ฆผ
  3. elements์˜ ์ˆœ์„œ์— ์˜ํ–ฅ์„ ์ฃผ์ง€๋Š” ์•Š์Œ(์—ฌ์ „ํžˆ ์ž‘์€ ์ˆ˜๊ฐ€ ์™ผ์ชฝ์—, ํฐ ์ˆ˜๊ฐ€ ์˜ค๋ฅธ์ชฝ์— ์œ„์น˜ํ•จ)

Right rotation example

  • ํŠธ๋ฆฌ์˜ ํฌ์ธํ„ฐ๋ฅผ ๋ฐ”๊พธ๋Š” ๊ฒƒ๋งŒ์œผ๋กœ rotation์ด ๊ฐ€๋Šฅํ•˜๊ธฐ์— time complexity๋Š” O(1)

์‚ฝ์ž… ์ „๋žต

  1. Red Z node ์‚ฝ์ž…
  2. Fix Violation(recolor and rotate nodes)

4๊ฐ€์ง€ ์ฃผ์š” ์‹œ๋‚˜๋ฆฌ์˜ค

  1. Z = root
  2. Z.uncle = red
  3. Z.uncle = black(triangle)
  4. Z.uncle = black(line)

โœ”๏ธ Z = root

Z๋Š” ์‚ฝ์ž…๋˜๋Š” ๋…ธ๋“œ๋ฅผ ๋œปํ•œ๋‹ค.

์ด Case๋Š” ์‚ฝ์ž…๋˜๋Š” ๋…ธ๋“œ๊ฐ€ root๊ฐ€ ๋˜๋Š” ๊ฒƒ์ด๋‹ค. RB Tree ์†์„ฑ์— ๋งž๊ฒŒ RedBlack์œผ๋กœ recolor๋งŒ ํ•˜๋ฉด ๋œ๋‹ค.

 

โœ”๏ธ Z.uncle = red

Fix ํ›„ ๊ฒฐ๊ณผ ์ด๋ฏธ์ง€(B node๊ฐ€ Red์ธ ๊ฒƒ์€ subtree ์ค‘ ํ•˜๋‚˜์ด๊ธฐ ๋•Œ๋ฌธ)

์‚ฝ์ž…๋˜๋Š” ๋…ธ๋“œ์˜ uncle(= Z.uncle)์˜ color๊ฐ€ red์ผ ๋•Œ, Z.uncle๊ณผ Z.grandparent์˜ color๋ฅผ swap ํ•˜์—ฌ Fix ํ•œ๋‹ค.

 

swap ํ›„ violation์„ ์ผ์œผํ‚ค๋Š” ๋…ธ๋“œ๊ฐ€ Z๊ฐ€ ๋˜๋ฉฐ ์‹œ๋‚˜๋ฆฌ์˜ค๋ฅผ ์ฒดํฌํ•œ๋‹ค.

 

โœ”๏ธ Z.uncle = black(triangle)

ํ•ด๋‹น Case์˜ subtree ์˜ˆ์‹œ

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)

ํ•ด๋‹น ์ผ€์ด์Šค์˜ subtree ์˜ˆ์‹œ

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

๋”๋ณด๊ธฐ
RB Tree ์‚ฝ์ž…์— ๋Œ€ํ•œ sudo code