• 카테고리

    질문 & 답변
  • 세부 분야

    알고리즘 · 자료구조

  • 해결 여부

    해결됨

else if 질문 있습니다.

23.02.15 11:23 작성 조회수 188

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를 쓰신 이유가 궁금합니다..!!

답변 2

·

답변을 작성해보세요.

1

김상민님의 프로필

김상민

질문자

2023.02.15

네!! 답변 감사합니다.

0

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