๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์•Œ๊ณ ๋ฆฌ์ฆ˜

์ œ11-1๊ฐ•. ์ด์ง„๊ฒ€์ƒ‰ํŠธ๋ฆฌ(Binary Search Tree)

by devshin.kr 2020. 11. 10.
728x90
  • ์ด์ง„๊ฒ€์ƒ‰ํŠธ๋ฆฌ(Binary Search Tree)

 

- ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ‚ค(key)๋ฅผ ์ €์žฅ

- ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์—ฐ์‚ฐ๋“ค์„ ์ง€์›ํ•˜๋Š” ์ž๋ฃŒ๊ตฌ์กฐ

   INSERT - ์ƒˆ๋กœ์šด ํ‚ค์˜ ์‚ฝ์ž…

   SEARTCH - ํ‚ค ํƒ์ƒ‰

   DELETE - ํ‚ค์˜ ์‚ญ์ œ

ex) ์‹ฌ๋ณผํ…Œ์ด๋ธ”

 

 

    search insert delete
๋ฐฐ์—ด ์ •๋ ฌ๋œ ๊ฒฝ์šฐ O(logn) O(n) O(n)
์ •๋ ฌ๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ O(n) O(1) O(1)
์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ ์ •๋ ฌ๋œ ๊ฒฝ์šฐ O(n) O(n) O(1)
์ •๋ ฌ๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ O(n) O(1) O(1)

 

 

  • ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•๋“ค

- ์ •๋ ฌ๋œ, ํ˜น์€ ์ •๋ ฌ๋˜์ง€ ์•Š์€ ๋ฐฐ์—ด ํ˜น์€ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ

  INSERT, SEARCH, DELETE ์ค‘ ์ ์–ด๋„ ํ•˜๋‚˜๋Š” O(n)

- ์ด์ง„ํƒ์ƒ‰ํŠธ๋ฆฌ(Binary Search Tree), ๋ ˆ๋“œ-๋ธ”๋ž™ํŠธ๋ฆฌ(red-black tree), AVL-ํŠธ๋ฆฌ ๋“ฑ์˜ ํŠธ๋ฆฌ์— ๊ธฐ๋ฐ˜ํ•œ ๊ตฌ์กฐ๋“ค

- Direct Address Table, ํ•ด์‰ฌ ํ…Œ์ด๋ธ” ๋“ฑ..

 

 

  • ๊ฒ€์ƒ‰ํŠธ๋ฆฌ

- Dynamic set์„ ํŠธ๋ฆฌ์˜ ํ˜•ํƒœ๋กœ ๊ตฌํ˜„

- ์ผ๋ฐ˜์ ์œผ๋กœ SEARCH, INSERT, DELETE ์—ฐ์‚ฐ์ด ํŠธ๋ฆฌ์˜ ๋†’์ด(height)์— ๋น„๋ก€ํ•˜๋Š” ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๊ฐ€์ง

- ์ด์ง„๊ฒ€์ƒ‰ํŠธ๋ฆฌ(Binary Search Tree), ๋ ˆ๋“œ-๋ธ”๋ž™ํŠธ๋ฆฌ(red-black tree), B-ํŠธ๋ฆฌ ๋“ฑ..

 

 

  • ์ด์ง„๊ฒ€์ƒ‰ํŠธ๋ฆฌ(BST : Binary Search Tree)

๊ฐ ๋…ธ๋“œ๋งˆ๋‹ค ์ตœ๋Œ€ ๋‘ ๋ช…์˜ ์ž์‹์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๊ณ , ๋ˆ„๊ฐ€ ์™ผ์ชฝ ์ž์‹์ด๊ณ  ์˜ค๋ฅธ์ชฝ ์ž์‹์ธ์ง€ ๋ช…์‹œ๋˜์–ด ์žˆ๋Š” ํŠธ๋ฆฌ

์ด์ง„๊ฒ€์ƒ‰ํŠธ๋ฆฌ(BST: Binary Search Tree)

 

 

์ด์ง„๊ฒ€์ƒ‰ํŠธ๋ฆฌ(BST: Binary Search Tree)

 

 

  • ์ˆœํ™˜์„ ์ด์šฉํ•ด ํ‘œํ˜„ํ•œ ์ด์ง„ํƒ์ƒ‰ ์˜์‚ฌ์ฝ”๋“œ
// SEARCH. x : ๋ฃจํŠธ๋…ธ๋“œ, k : ์ฐพ๋Š” ๊ฐ’
TREE-SEARCH (x, k){
	if x == null or k == key[x]	// key[x] : ๋…ธ๋“œ x์— ์ €์žฅ๋œ ๊ฐ’
    	then return x
    if k < key[x]
    	then return TREE-SEARCH(left[x], k)
        else return TREE-SEARCH(right[x], k)
}

์‹œ๊ฐ„๋ณต์žก๋„ : O(h), ์—ฌ๊ธฐ์—์„œ h๋Š” ํŠธ๋ฆฌ์˜ ๋†’์ด

 

 

 

  • ๋ฐ˜๋ณต๋ฌธ์„ ์ด์šฉํ•ด ํ‘œํ˜„ํ•œ ์ด์ง„ํƒ์ƒ‰ ์˜์‚ฌ์ฝ”๋“œ
INTERATIVE-TREE-SEARCH(x, k){
	while x != null and k != key[x]
    	do if k < key[x]
        	then x // left[x]
            else x // right[x]
    	return x
}

์‹œ๊ฐ„๋ณต์žก๋„ : O(h), ์—ฌ๊ธฐ์—์„œ h๋Š” ํŠธ๋ฆฌ์˜ ๋†’์ด

๋Œ“๊ธ€