AOJ ALDS1_8_C Binary Search Tree III
備忘録
問題
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_8_C&lang=jp
回答
import sys, os, math, bisect, itertools, collections, heapq, queue, copy, array # from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall # from decimal import Decimal # from collections import defaultdict, deque sys.setrecursionlimit(10000000) ii = lambda: int(sys.stdin.buffer.readline().rstrip()) il = lambda: list(map(int, sys.stdin.buffer.readline().split())) fl = lambda: list(map(float, sys.stdin.buffer.readline().split())) iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)] iss = lambda: sys.stdin.buffer.readline().decode().rstrip() sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split())) isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)] lcm = lambda x, y: (x * y) // math.gcd(x, y) MOD = 10 ** 9 + 7 MAX = float('inf') class BinaryNode: def __init__(self, key, left=None, right=None): self.val = key self.left = left self.right = right def insert(root, node): if root is None: root = node else: if root.val < node.val: if root.right is None: root.right = node else: insert(root.right, node) else: if root.left is None: root.left = node else: insert(root.left, node) def preorder(root): ret = [] if root is None: return ret ret.append(root.val) ret.extend(preorder(root.left)) ret.extend(preorder(root.right)) return ret def inorder(root): ret = [] if root is None: return ret ret.extend(inorder(root.left)) ret.append(root.val) ret.extend(inorder(root.right)) return ret def find(node, key): while node is not None and key != node.val: if key < node.val: node = node.left else: node = node.right return node def get_min_node(node): current = node while current.left is not None: current = current.left return current def get_min_node(node): current = node while current.left is not None: current = current.left return current def delete(root, key): if root is None: return root if key < root.val: root.left = delete(root.left, key) elif key > root.val: root.right = delete(root.right, key) else: if root.left is None: temp = root.right root = None return temp elif root.right is None: temp = root.left root = None return temp temp = get_min_node(root.right) root.val = temp.val root.right = delete(root.right, temp.val) return root def main(): if os.getenv("LOCAL"): sys.stdin = open("input.txt", "r") N = ii() s, key = sl() root = BinaryNode(int(key)) for n in range(N - 1): input = sl() if input[0] == 'print': print("", *inorder(root)) print("", *preorder(root)) elif input[0] == 'find': node = find(root, int(input[1])) print('yes' if node is not None else 'no') elif input[0] == 'delete': root = delete(root, int(input[1])) else: insert(root, BinaryNode(int(input[1]))) if __name__ == '__main__': main()
考え方
螺旋本より、二分探索木の問題。
二分探索木から特定のkey
を持つノードに対して、削除を行う。
基本的には下記を写経した。
Binary Search Tree | Set 2 (Delete) - GeeksforGeeks
削除処理は、ルートのノードからkey
を比較して、
削除対象のノードの特定を行う。
ノードを特定した後は、問題に指示通りに、
どちらか片方向しかノードを持たない場合、両端にノードを持っている場合で削除後のノードの結合処理を分ける。
注意しなければならないのは、両端にノードを持っている場合、
削除後に自身の位置に置くノードについて。
削除対象の左ノードより大きく、右ノードより小さいkey
を持つノードを持ってくる必要がある。
そのため、自身の右ノードから最も小さいノード(左ノードの最下層)を探索する必要がある。