-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adt/tree #2
base: main
Are you sure you want to change the base?
Adt/tree #2
Changes from all commits
857ac36
b833345
0e554df
073932f
a7b7b73
9a33bac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
from xmlrpc.client import Boolean | ||
|
||
|
||
class BinaryTreeNode: | ||
def __init__(self, data:any, left=None, right=None) -> None: | ||
self.data:any = data | ||
self.left:BinaryTreeNode = left | ||
self.right:BinaryTreeNode = right | ||
Comment on lines
+7
to
+8
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. :BinaryTreeNode는 어떤 의미인가요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 맞습니다! BinaryTreeNode는 이진트리에 들어갈 노드를 클래스로 제가 만든거에요. |
||
|
||
class BST: | ||
def __init__(self, root=None) -> None: | ||
if root: | ||
self.root = root | ||
self.len = 1 | ||
else: | ||
self.root = BinaryTreeNode(None) | ||
self.len = 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. len은 무슨 의미인가요? 문자열 길이 구할 때 사용하는 함수의 이름이라고 알고 있는데.. 비슷한 의미일까요? 노드의 개수를 카운트한다거나?! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 앗 요건 length의 줄임말입니다. (len 함수도 length의 줄임말입니다.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 줄임말을 열심히 익혀야겠어요 뭔가 줄임말을 써야 멋진 개발자 느낌이 나는 것 같아요🤣감사합니다!! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 사실 이런건 줄이는 것 보다는 풀어 쓰는게 더 좋다고 하더라고요ㅎㅎ 하지만 저는 귀찮기 때문에 줄였습니다 하하! |
||
|
||
def insert(self, node, child) -> None: | ||
if node.data == child.data: | ||
return | ||
|
||
if node.data < child.data: | ||
if node.right is None: | ||
node.right = child | ||
else: | ||
self.insert(node.right, child) | ||
|
||
elif node.data > child.data: | ||
if node.left is None: | ||
node.left = child | ||
else: | ||
self.insert(node.left, child) | ||
|
||
self.len += 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 재귀로 insert 함수가 호출되면서 len이 엄청나게 많이 증가하지 않을까 생각되는데요, 확인부탁드립니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 맞아요. 감사합니다. 하나만 추가하는건데 len만 엄청나게 많아질 것 같네요. 근데 정작 len을 쓰는 곳이 없었어서 발견을 못했나봐요ㅋㅋㅋ |
||
|
||
|
||
|
||
def find_node(self, node, data) -> None: | ||
# root node를 안넣으면 어떻게 되지? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 루트노드가 없으면 트리 자체가 못 만들어지지 않나여? 루트노드를 안 넣을 수도 있나요? 약간 dll에 head가 없는 상태인 그런 느낌일까요? 이런 것도 물어봐도 되나염..?😂 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 당연하죠! 좋은 질문 감사합니다~ 저도 궁금해서 실행해봤는데, root 노드가 아니라 다른 노드를 넣으면 그 노드를 root로 인식하고 그걸 기준으로 탐색을 하더라구요. 아예 없는 건 말이 안되는 것 같고, 해당 트리의 진짜 루트가 아니더라도 뭘 기준으로 찾을 건지 여부를 결정해서 넣으면 되는 것 같았어요. |
||
if node is None: | ||
return None | ||
|
||
if node.data == data: | ||
return node | ||
if node.data > data: | ||
return self.find_node(node.left, data) | ||
if node.data < data: | ||
return self.find_node(node.right, data) | ||
|
||
|
||
def find_min_node(self, node): | ||
if node is None: | ||
return None | ||
|
||
if node.left is None: | ||
return node | ||
else: | ||
return self.find_min_node(node.left) | ||
|
||
|
||
def remove(self, node, parent, target_data): | ||
removed = None | ||
|
||
if node is None: | ||
return None | ||
|
||
if (node.data > target_data): | ||
removed = self.remove(node.left, node, target_data) | ||
elif (node.data < target_data): | ||
removed = self.remove(node.right, node, target_data) | ||
else: | ||
removed = node | ||
|
||
if not node.left and not node.right : | ||
if parent.left == node: | ||
parent.left = None | ||
else: | ||
parent.right = None | ||
|
||
else: | ||
if node.left and node.right: | ||
min_node = self.find_min_node(node.right) | ||
removed = self.remove(node, None, min_node.data) | ||
node.data = min_node.data | ||
Comment on lines
+82
to
+85
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 문제의 코드가 이 부분이었는데, 83line에서 removed가 알고리즘 책 다른 페이지에서는 min_node로 되어 있었어요. 그래서 오타구나 싶었는데 결과는 |
||
else: | ||
temp = None | ||
if node.left: | ||
temp = node.left | ||
else: | ||
temp = node.right | ||
|
||
if parent.left == node: | ||
parent.left = temp | ||
else: | ||
parent.right = temp | ||
|
||
self.len -= 1 | ||
|
||
return removed | ||
|
||
|
||
|
||
def print(self, node): | ||
if node is None: | ||
return | ||
|
||
self.print(node.left) | ||
|
||
print(f"[ {node.data} ]") | ||
|
||
self.print(node.right) | ||
|
||
def to_list(self, node, result): | ||
if node is None: | ||
return | ||
|
||
self.to_list(node.left, result) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기서 result가 reference로 전달되나요? result를 사용해서 to_list를 호출하지만, 반환된 값을 사용하지는 않는 것 같아서요. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기서 반환된 값을 쓰지는 않지만, 같은 result에 append를 계속 한 걸 보면 reference로 전달된다고 생각합니다! |
||
result.append(node.data) | ||
self.to_list(node.right, result) | ||
|
||
return result | ||
|
||
def pretty_print(to_print:str) -> None: | ||
print() | ||
print(f" * * * {to_print} * * * ") | ||
print() | ||
|
||
def main(): | ||
root = BinaryTreeNode(50) | ||
bs_tree = BST(root=root) | ||
data_size = 5 | ||
for i in range(1, data_size): | ||
bs_tree.insert(root, BinaryTreeNode(50 - 10 * i)) | ||
bs_tree.insert(root, BinaryTreeNode(50 - 10 * i + 1)) | ||
bs_tree.insert(root, BinaryTreeNode(50 - 10 * i - 1)) | ||
bs_tree.insert(root, BinaryTreeNode(50 + 10 * i)) | ||
bs_tree.insert(root, BinaryTreeNode(50 + 10 * i + 1)) | ||
bs_tree.insert(root, BinaryTreeNode(50 + 10 * i - 1)) | ||
|
||
pretty_print("initial tree") | ||
|
||
result = bs_tree.to_list(root, []) | ||
print(result) | ||
print(40 in result) | ||
|
||
removed = bs_tree.remove(root, None, 40) | ||
print(f"removed is {removed.data}") | ||
pretty_print("after remove tree") | ||
|
||
result = bs_tree.to_list(root, []) | ||
print(result) | ||
print(40 not in result) | ||
|
||
#to_update = bs_tree.find_node(root, 21) | ||
#bs_tree.print(to_update) | ||
#print(to_update.data) | ||
#to_update.data = 99 | ||
#result = bs_tree.to_list(root, []) | ||
#print(result) | ||
|
||
pretty_print("print partial") | ||
to_print = bs_tree.find_node(root, 10) | ||
bs_tree.print(to_print) | ||
|
||
|
||
|
||
|
||
|
||
if __name__ == "__main__": | ||
main() | ||
|
||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
기본 타입인 bool 대신 Boolean을 사용하신 이유가 있을까요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
얘는 뭐지?? 발견해주셔서 감사해요ㅋㅋㅋㅋㅋㅋㅋ