-1
나는 이진 검색 트리를 통해 재귀 적으로 탐색하는 프로그램을 가지고있다. 그러나 특정 노드에 도달하면 부모 노드로 이동하여 두 노드 사이에 노드를 삽입하려고합니다. 그러면 부모 노드에 어떻게 접근합니까?바이너리 검색 트리에서 한 레벨 위로 이동하는 방법은 무엇입니까?
감사합니다.
편집 : 나는, 배열을 사용하여 트리를 만들어 그래서 예를 들면 :
tree = ['D', 'Does it have 4 legs?',
['D', 'Can you ride it?', ['I','horse'], ['I', 'dog']],
['D', 'Does it have hands?', ['I', 'monkey'],['I', 'bird']]]
트리를 통해 통과에 대한 나의 코드 :
def identify_thing(tree):
node_type = tree[0]
if node_type == "D":
(question, yes_tree, no_tree) = tree[1:]
yes_answer = get_yes_no(question)
if yes_answer:
identify_thing(yes_tree)
else:
identify_thing(no_tree)
elif node_type == "I":
name = tree[1]
question = "Is it a {}?".format(name)
yes_answer = get_yes_no(question)
if yes_answer:
print("{} Identified!".format(name))
else:
print("I don't know what it is.")
new_name = get_nonblank_str("What is it?")
new_question = get_nonblank_str("Give me a question where yes means
a '{}'" " and no means a '{}'".format(new_name, name))
# this is where I am trying to insert the code for updating the tree
이는 사용중인 "이진 검색 트리"의 특정 구현에 따라 다릅니다. 많은 가능성이 있습니다. 어느 쪽을 사용하고 있습니까? –
StackOverflow에 오신 것을 환영합니다. 도움말 설명서의 게시 지침을 읽고 따르십시오. [최소한의 완전하고 검증 가능한 예제] (http://stackoverflow.com/help/mcve)가 여기에 적용됩니다. MCVE 코드를 게시하고 문제를 정확하게 설명하기 전까지는 효과적으로 도움을 드릴 수 없습니다. StackOverflow는 코딩 또는 튜토리얼 서비스가 아닙니다. – Prune