티스토리 뷰

반응형

 

 

해커랭크 Day 26 챌린지를 시작해보자. 😊

 

오늘은 UnitTest와 Test case 에 대해 알아보았다.

 

또한 오늘은 hackerrank challenges day22에서 진행했던

Tree 코드가 필요하다.

 

우선 전체 소스코드를 살펴보자. 👇👇

(Day 22에 사용했던 소스코드도 포함되어 있다.)

 

 



public class EmptyBST<D extends Comparable> implements Tree<D> {

    public EmptyBST() {

    }

    @Override
    public boolean isEmpty() {
        return true;
    }

    @Override
    public int cardinality() {
        return 0;
    }

    @Override
    public boolean member(D elt) {
        return false;
    }

    @Override
    public NonEmptyBST<D> add(D elt) {
        return new NonEmptyBST<D>(elt);
    }
}

EmptyBST.java

 

 

 


class NonEmptyBST<D extends Comparable> implements Tree<D> {
    D data;
    Tree<D> left;
    Tree<D> right;


    public NonEmptyBST(D elt) {
        data = elt;
        left = new EmptyBST<D>();
        right = new EmptyBST<D>();
    }

    public NonEmptyBST(D elt, Tree<D> leftTree, Tree<D> rightTree) {
        data = elt;
        left = leftTree;
        right = rightTree;
    }

    public boolean isEmpty() {
        return false;
    }

    public int cardinality() {
        return 1 + left.cardinality() + right.cardinality();
    }

    public boolean member(D elt) {
        if(data == elt) {
            return true;
        } else {
            if(elt.compareTo(data) < 0) {
                return left.member(elt);
            } else {
                return right.member(elt);
            }
        }
    }

    public NonEmptyBST<D> add(D elt) {
        if(data == elt) {
            return this;
        } else {
            if(elt.compareTo(data) < 0) {
                return new NonEmptyBST(data, left.add(elt), right);
            } else {
                return new NonEmptyBST(data, left, right.add(elt));
            }
        }
    }
}

NonEmptyBST.java

 

 

 

public interface Tree<D extends Comparable> {
    public boolean isEmpty();

    public int cardinality();

    public boolean member(D elt);

    public NonEmptyBST<D> add(D elt);

}

Tree.java

 

 

 

 


public class Testers {
    public static void checkIsEmpty(Tree t) throws Exception {
        // if the tree t is an insetance of EmptyBST -> isEmpty -> true
        // if the tree t is an instance of NonEmptyBST --> t.isEmpty -> false
        if(t instanceof EmptyBST) {
            if(!t.isEmpty()) {
                throw new Exception("All is not good, the tree is an EmptyBST and it is non-empty");
            }
        }else if(t instanceof NonEmptyBST) {
            if(!t.isEmpty()) {
                throw new Exception("All is not good, the tree is a NonEmptyBST and it is empty");
            }
        }
    }
}

Testers.java

 

 

 


public class BinarySearchTree {
    public static void main(String[] args) throws Exception {
        EmptyBST e = new EmptyBST();
        NonEmptyBST n = new NonEmptyBST(5);
        Testers.checkIsEmpty(e);
        Testers.checkIsEmpty(n);

    }
}

BinarySearchTree.java

 

 

 

 

사용하는 파일은 많지만

이전 트리 소스 코드를 이용해서

EmptyBST와 NonEmptyBST를 생성하고

Testers 클래스를 통해

Tree가 비어있는지를 체크하는 예외처리 메서드를 생성하였다.

 

 


 

 

그럼 우리는 다음 포스팅에서

소스 코드 리뷰로 다시 만나자. 😊

반응형