Week 3: Exercises

1. Binary Tree

Write a class BinaryTree that holds a binary tree of integers. Your class should have these members:

BinaryTree()
Create a BinaryTree that is initially empty.
void insert(int i)
Insert a value into the tree if it is not already present.
bool contains(int i)
Return true if the tree contains the value i, otherwise false.
int[] toArray()
Return an array holding all the values in the binary tree, in ascending order.

2. Hands of Cards

Consider this class:

enum Suit { Clubs, Diamonds, Hearts, Spades };

class Card {
    public int rank;    // 1 = Ace, 2 .. 10, 11 = Jack, 12 = Queen, 13 = King 
    public Suit suit;

    public Card(int rank, Suit suit) {
        this.rank = rank;
        this.suit = suit;
    }

}

a) Add a method string describe() that returns a string description such as "7 of Diamonds" or "Jack of Hearts".

b) Write a class Deck with these members: