deo2kim
๋งž์™œํ‹€
deo2kim
์ „์ฒด ๋ฐฉ๋ฌธ์ž
์˜ค๋Š˜
์–ด์ œ
  • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ
    • CS
      • Algorithm
      • Data Structure
      • Network
      • DB
      • OS
    • Algorithm Problem
      • Python
      • JavaScript
    • Programming language
      • Python
      • JavaScript
    • Tool
      • Jquery
      • React
    • ๊ฐœ๋ฐœ
    • Infra

๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

  • ํ™ˆ
  • ํƒœ๊ทธ
  • ๋ฐฉ๋ช…๋ก

๊ณต์ง€์‚ฌํ•ญ

์ธ๊ธฐ ๊ธ€

์ตœ๊ทผ ๋Œ“๊ธ€

์ตœ๊ทผ ๊ธ€

ํ‹ฐ์Šคํ† ๋ฆฌ

๋ฐ˜์‘ํ˜•
hELLO ยท Designed By ์ •์ƒ์šฐ.
deo2kim

๋งž์™œํ‹€

[python] ์ด์ง„ ํƒ์ƒ‰ ํŠธ๋ฆฌ (binary Search tree) - 2. ํƒ์ƒ‰
CS/Data Structure

[python] ์ด์ง„ ํƒ์ƒ‰ ํŠธ๋ฆฌ (binary Search tree) - 2. ํƒ์ƒ‰

2020. 9. 3. 20:21
๋ฐ˜์‘ํ˜•

๐Ÿ“— ์ด์ง„ ํŠธ๋ฆฌ (binary Search tree)

๐Ÿ”ต ์ด์ง„ ํƒ์ƒ‰ ํŠธ๋ฆฌ ํƒ์ƒ‰

 

[python] ์ด์ง„ ํƒ์ƒ‰ ํŠธ๋ฆฌ (binary Search tree) - 1. ํด๋ž˜์Šค ์ •์˜, ์ดˆ๊ธฐํ™”, ๋…ธ๋“œ ์ถ”๊ฐ€

๐Ÿ“— ์ด์ง„ ํŠธ๋ฆฌ (binary Search tree) ๐Ÿ”ต ์ด์ง„ ํƒ์ƒ‰ ํŠธ๋ฆฌ ํด๋ž˜์Šค ์ •์˜ ๋ฐ ์ดˆ๊ธฐํ™”  ๋จผ์ € Node ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•ด ๋ณด์ž.  Node ํด๋ž˜์Šค๋Š” ๋…ธ๋“œ๊ฐ’์ธ  self.val ์™€ ์™ผ์ชฝ ๋…ธ๋“œ  self.left ์˜ค๋ฅธ์ชฝ ๋…ธ๋“œ  self.right ์˜..

deok2kim.tistory.com

๐Ÿ”บ ํด๋ž˜์Šค ์ •์˜ ๋ฐ ์ดˆ๊ธฐํ™”๋Š” ์•ž์—์„œ ๋‹ค๋ค˜๋‹ค.

 

 ์ด์ง„ ํƒ์ƒ‰ ํŠธ๋ฆฌ์— ์ฐพ๊ณ ์žํ•˜๋Š” ๊ฐ’์ด ์žˆ๋Š”์ง€๋ฅผ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๊ฐ’์ด ์žˆ์œผ๋ฉด True, ์—†์œผ๋ฉด False๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

๋งจ ์œ„์˜ ์ด๋ฏธ์ง€๋ฅผ ๋ณด๋ฉด ์›ํ•˜๋Š” ๊ฐ’์ธ 27์„ ์ฐพ๋Š” ๊ณผ์ •์ด๋‹ค. 

  • ๋จผ์ € ์ตœ์ƒ์œ„ ๋…ธ๋“œ์ธ 21๊ณผ 27์„ ๋น„๊ตํ•œ๋‹ค.
  • 27์ด ๋…ธ๋“œ๊ฐ’๋ณด๋‹ค ํฌ๋ฏ€๋กœ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ด๋™ํ•ด์„œ ํƒ์ƒ‰ํ•œ๋‹ค.
  • ๋‹ค์Œ ๋…ธ๋“œ๋ฅผ ๋ณด๋ฉด 28์ด๋‹ค. 27์ด ๋…ธ๋“œ๊ฐ’๋ณด๋‹ค ์ž‘์œผ๋ฏ€๋กœ ์™ผ์ชฝ์œผ๋กœ ์ด๋™ํ•ด์„œ ํƒ์ƒ‰ํ•œ๋‹ค.
  • ๋‹ค์Œ์€ 25์ด๋‹ค. 27์ด ๋…ธ๋“œ๊ฐ’๋ณด๋‹ค ํฌ๋ฏ€๋กœ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ด๋™ํ•œ๋‹ค.
  • 27์„ ์ฐพ์•˜์œผ๋ฏ€๋กœ ํƒ์ƒ‰ ์ข…๋ฃŒ.

๊ทธ๋ฆผ ์•„๋ž˜์˜ ์ •๋ ฌ๋œ ๋ฆฌ์ŠคํŠธ์˜ ์ˆœ์ฐจ์ ์ธ ํƒ์ƒ‰๊ณผ ๋น„๊ตํ•ด๋ณด๋ฉด ํ›จ์”ฌ ๋น ๋ฅธ๊ฑธ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

๋‹ค์Œ์€ ํƒ์ƒ‰ ์ฝ”๋“œ ์ด๋‹ค.

class Node:
    def __init__(self, item):

class BinaryTree:
    def __init__(self):

    def add(self, item):

    def __add_node(self, cur, item):

    # ์ฐพ๊ธฐ!!
    def search(self, item):
        if self.head.val is None:
            return False
        else:
            return self.__search_node(self.head, item)

    def __search_node(self, cur, item):
        print(cur.val, item)
        if cur.val == item:
            return True
        else:
            if cur.val >= item:
                if cur.left is not None:
                    return self.__search_node(cur.left, item)
                else:
                    return False
            else:
                if cur.right is not None:
                    return self.__search_node(cur.right, item)
                else:
                    return False
                    
                    

๐Ÿ”ต ์ „์ฒด ์ฝ”๋“œ

# ๋…ธ๋“œ ๋งŒ๋“ค๊ธฐ
#         A(val)
#        /      \
#   B(left)     C(right)
class Node:
    def __init__(self, item):
        self.val = item
        self.left = None
        self.right = None


# ์ด์ง„ํŠธ๋ฆฌ ๋งŒ๋“ค๊ธฐ
class BinaryTree:
    # ์ดˆ๊ธฐ๊ฐ’ head๋Š” None
    def __init__(self):
        self.head = Node(None)

        self.preorder_list=[]
        self.inorder_list=[]
        self.postorder_list=[]

    # ๊ฐ’ ์ถ”๊ฐ€ํ•˜๊ธฐ head๊ฐ€ ์—†์„ ๊ฒฝ์šฐ
    def add(self, item):
        if self.head.val is None:
            self.head.val = item

        # head๊ฐ€ ์žˆ์œผ๋ฉด ์™ผ์ชฝ๋ฐฐ์น˜ or ์˜ค๋ฅธ์ชฝ๋ฐฐ์น˜
        else:
            self.__add_node(self.head, item)

    # head๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ
    def __add_node(self, cur, item):
        print('๋ถ€๋ชจ:', cur.val, '์ž์‹:', item)
        # head ๊ฐ’์ด ํฌ๋ฉด ์™ผ์ชฝ์œผ๋กœ
        if cur.val >= item:
            if cur.left is not None:
                self.__add_node(cur.left, item)
            else:
                cur.left = Node(item)
        # head ๊ฐ’์ด ์ž‘์œผ๋ฉด ์˜ค๋ฅธ์ชฝ์œผ๋กœ
        else:
            if cur.right is not None:
                self.__add_node(cur.right, item)
            else:
                cur.right = Node(item)

    # ์ฐพ๊ธฐ!!
    def search(self, item):
        if self.head.val is None:
            return False
        else:
            return self.__search_node(self.head, item)

    def __search_node(self, cur, item):
        print(cur.val, item)
        if cur.val == item:
            return True
        else:
            if cur.val >= item:
                if cur.left is not None:
                    return self.__search_node(cur.left, item)
                else:
                    return False
            else:
                if cur.right is not None:
                    return self.__search_node(cur.right, item)
                else:
                    return False


lst = [50, 30, 24, 5, 28, 45, 98, 52, 60]
bt = BinaryTree()
for num in lst:
    bt.add(num)

print(bt.search(60))
๋ฐ˜์‘ํ˜•
์ €์ž‘์žํ‘œ์‹œ ๋น„์˜๋ฆฌ ๋ณ€๊ฒฝ๊ธˆ์ง€ (์ƒˆ์ฐฝ์—ด๋ฆผ)

'CS > Data Structure' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

์Šคํƒ(Stack)  (0) 2020.11.04
[python] ์ด์ง„ ํƒ์ƒ‰ ํŠธ๋ฆฌ (binary Search tree) - 4. ์‚ญ์ œ  (0) 2020.09.05
[python] ์ด์ง„ ํƒ์ƒ‰ ํŠธ๋ฆฌ (binary Search tree) - 3. ์ˆœํšŒ  (0) 2020.09.04
[python] ์ด์ง„ ํƒ์ƒ‰ ํŠธ๋ฆฌ (binary Search tree) - 1. ํด๋ž˜์Šค ์ •์˜, ์ดˆ๊ธฐํ™”, ๋…ธ๋“œ ์ถ”๊ฐ€  (0) 2020.09.02
[python] ์ด์ง„ ํŠธ๋ฆฌ (binary tree) - ๊ฐœ๋… ์ •๋ฆฌ  (0) 2020.09.01
    'CS/Data Structure' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
    • [python] ์ด์ง„ ํƒ์ƒ‰ ํŠธ๋ฆฌ (binary Search tree) - 4. ์‚ญ์ œ
    • [python] ์ด์ง„ ํƒ์ƒ‰ ํŠธ๋ฆฌ (binary Search tree) - 3. ์ˆœํšŒ
    • [python] ์ด์ง„ ํƒ์ƒ‰ ํŠธ๋ฆฌ (binary Search tree) - 1. ํด๋ž˜์Šค ์ •์˜, ์ดˆ๊ธฐํ™”, ๋…ธ๋“œ ์ถ”๊ฐ€
    • [python] ์ด์ง„ ํŠธ๋ฆฌ (binary tree) - ๊ฐœ๋… ์ •๋ฆฌ
    deo2kim
    deo2kim
    ์ฝ”๋”ฉ ๊ธฐ๋กํ•˜๊ธฐ

    ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”