니는 루트를 제외한 모든 노드는 부모 노드가 있기 때문에 트리의 링크 수는 n-1 개이다.
스레드의 이용
ptr -> left_child = NULL 일 경우, ptr -> left_child 를 ptr의 중위행선자를 가리키도록 변경한다.
중위행선자(inorder predecessor) : 중위 순회에서 현재 노드 바로 앞에 나오는 노드
ptr -> right_child = NULL 일 경우, ptr -> right_child 를 ptr의 중위후속자를 가리키도록 변경한다.
중위후속자(inorder successor) : 중위 순회에서 현재 노드 바로 뒤에 나오는 노드
노드의 구조
struct thread_tree{
short int left_thread; # True or False
struct thread_tree *left_child; # left_child = true : 스레드
# left_child = false : 왼쪽 자식 노드
char data;
struct thread_tree *right_child; # right_thread 에 따라 결정된다.
short int right_thread; # true or false
}
💡 헤드노드(Head Node)
헤드노드의 역할
가장 왼쪽 노드의 Inorder predecessor
가장 오른쪽 노드의 Inorder successor
💡 스레드를 이용한 중위순회
ptr이 현재 노드를 가리킨다고 가정한다.
# 중위순회에서 ptr 다음 노드 = ptr -> right_child
if ptr -> right_thread == TRUE
otherwise
# ptr 의 right_child 로 이동한 후, left_child 를 따라 내려간다.
# >> left_thread == TRUE 인 노드를 만날 때까지 내려간다.
중위 후속자 발견 알고리즘(insucc)
struct thread_tree *insucc(struct thread_tree *ptr){
# 스레드 이진 트리에서 ptr이 가리키는 노드의 inorder success를 반환
struct thread_tree *temp = ptr -> right_child;
# right_child 가 자식 노드
if (!ptr -> right_child){
# 왼쪽 끝에 도달할 때까지 반복한다.
while(!temp -> left_child){
temp = temp -> left_child;
}
}
return temp;
}