-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b8956e8
commit 64ae360
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
class Node: | ||
def __init__(self, key): | ||
self.left = None | ||
self.right = None | ||
self.val = key | ||
|
||
class BST: | ||
def __init__(self): | ||
self.root = None | ||
def insert(self, key): | ||
#루트 노드가 없는 경우 새로운 노드를 루트 노드로 추가 | ||
if not self.root: | ||
self.root = Node(key) | ||
else: | ||
curr = self.root | ||
while True: | ||
if key < curr.val: | ||
if curr.left: | ||
curr = curr.left | ||
else: | ||
curr.left = Node(key) | ||
break | ||
else: | ||
if curr.right: | ||
curr = curr.right | ||
else: | ||
curr.right = Node(key) | ||
break | ||
def search(self, key): | ||
curr = self.root | ||
while curr and curr.val != key: | ||
if key < curr.val: | ||
curr = curr.left | ||
else: | ||
curr = curr.right | ||
return curr | ||
|
||
|
||
def solution(lst, search_lst): | ||
answer = [] | ||
bst = BST() | ||
for node in lst: | ||
bst.insert(node) | ||
|
||
for key in search_lst: | ||
result = bst.search(key) #Node객체가 반환된다 | ||
if result != None and result.val == key: | ||
answer.append(True) | ||
else: | ||
answer.append(False) | ||
return answer | ||
|
||
lst = [5, 3, 8, 4, 2, 1, 7, 10] | ||
search_lst = [1, 2, 5, 6] | ||
#solution(lst, search_lst) | ||
print(solution(lst, search_lst)) |