강의

멘토링

커뮤니티

Inflearn Community Q&A

kimpran's profile image
kimpran

asked

Data Structures and Algorithms Explained Simply with Pictures (Advanced)

Priority Queues and Heaps - Implementation 1 (Heap Insertion)

else if 질문 있습니다.

Resolved

Written on

·

351

1

힙 삽입에서

getInsertingParent(){
        if(this.lastInsertedNode.getParent() == null){
            return this.lastInsertedNode;
        }else{
            if(this.lastInsertedNode == this.lastInsertedNode.getParent().getLeftSubTree()){
                return this.lastInsertedNode.getParent();
            }else{
                let current = this.lastInsertedNode;
                let firstRightSibling = null;

                while(current.getParent().getParent() != null){
                    current = current.getParent();

                    firstRightSibling = this.getRightSibling(current);
                    if(firstRightSibling != null){
                        break;
                    }
                }

마지막으로 삽입된 노드가 루트노드일때를 if문으로 처리하시고 그다음 else 안에 서 if문으로 다시 처리하셨는데 이유가 있을까요? else if로 안쓰시고 else 안에서 if를 쓰신 이유가 궁금합니다..!!

알고리즘algorithm

Answer 2

1

kimpran님의 프로필 이미지
kimpran
Questioner

네!! 답변 감사합니다.

0

gamja5님의 프로필 이미지
gamja5
Instructor

가독성을 위해서 나눠서 작성했습니다!
로직만 같다면 편하신 걸 선택하시면 될 것 같습니다 ㅎㅎ

kimpran's profile image
kimpran

asked

Ask a question