Category Archives: Lesson

Trees Assignments

September 25th, 2015

Trees

  • A tree is a nonlinear structure in which elements are organized
    into a hierarchy.
  • A tree is composed of a set of nodes in which elements are stored and edges that connect one node to another.
  • Each node is at a particular level in the tree hierarchy.
  • The root of the tree is the only node at the top level of the tree.
  • There is only one root node in a tree.
  • The nodes at lower levels of the tree are the children of nodes at the previous level.
  • A node can have only one parent, but a node may have multiple children.
  • Nodes that have the same parent are called siblings.
  • The root node is the only node in a tree that does not have a parent.
  • A node that does not have any children is called a leaf.
  • The root is the entry point into a tree structure. We can follow a path through the tree from parent to child.
  • We can follow a path through the tree from parent to child.
  • A node is the ancestor of another node if it is above it on the path from the root.
  • The level of a node is also the length of the path from the root to the node.
  • This path length is determined by counting the number of edges that must be followed to get from the root to the node.
  • The height of a tree is the length of the longest path from the root to a leaf.

Tree Terminology Tree path and level

Tree Types

  • A tree that has no limit to the number of children a node may have is called a general tree.
  • A tree that limits each node to no more than n children is referred to as an n-ary tree.
  • A tree in which nodes may have at most two children is called a binary tree.
  • A tree is considered to be balanced if all of the leaves of the tree are on the same level or at least within one level of each other.
  • A balanced n-ary tree with m elements will have a height of log base n of m.
  • A balanced binary tree with n nodes will have a height of log base 2 of n.

Tree balanced

  • A complete tree is related to the balance of a tree.
  • A tree is complete if it is balanced and all of the leaves at the bottom level are on the left side of the tree.
  • Another way to define this concept is that a complete binary tree has 2^k nodes at every level k except the last, where the nodes must be leftmost.
  • An n-ary tree is considered full if all the leaves of the tree are at the same level and every node is either a leaf or has exactly n children.

Complete Trees

(a) and (c)–are complete
Only (c) in Figure full

Homework:
Visit edmodo.com to answer the following questions
1. What is a tree?
2. What is a node?
3. What is the root of the tree?
4. What is a leaf?
5. What is an internal node?
6. Define the height of a tree.
7. Define the level of a node.

Read Tree implementation with arrays.

Queue ADT


A Queue ADT
A queue is a linear collection whose elements are added on one end and removed from the other. Therefore, we say that queue elements are processed in a first in, first out (FIFO) manner. Elements are removed from a queue in the same order in which they are placed on the queue.

queue

queue operations

public interface QueueADT
{
// Adds one element to the rear of the queue
public void enqueue (T element);

// Removes and returns the element at the front of the queue
public T dequeue();

// Returns without removing the element at the front of the queue
public T first();

// Returns true if the queue contains no elements
public boolean isEmpty();

// Returns the number of elements in the queue
public int size();

// Returns a string representation of the queue
public String toString();
}

Assignment: Using the QueueADT interface, implement the Queue ADT according to the API below:

■ Queue elements are processed in a FIFO manner—the first element in is the first element out.
■ A queue is a convenient collection for storing a repeating code key.
■ Simulations are often implemented using queues to represent waiting lines.
■ A linked implementation of a queue is facilitated by references to the first and last elements of the linked list.
■ The enqueue and dequeue operations work on opposite ends of the collec- tion.
■ Because queue operations modify both ends of the collection, fixing one end at index 0 requires that elements be shifted.
■ The shifting of elements in a noncircular array implementation creates an O(n) complexity.
■ Treating arrays as circular eliminates the need to shift elements in an array queue implementation.

Assignments:
1. Create a system using a stack and a queue to test whether a given string is a palindrome (i.e., the characters read the same forward or backward).

traffic cartoon
2. Create a system to simulate vehicles at an intersection. Assume that there is one lane going in each of four directions, with stoplights facing each direction. Vary the arrival average of vehicles in each direction and the frequency of the light changes to view the “behavior” of the intersection.

traffic

Stacks and Queues – Stack Implementation

Data Structures: Stacks and Queues

stack-of-books

in-line

Stacks. A stack is a collection that is based on the last-in-first-out (LIFO) policy. By tradition, we name the stack insert method push() and the stack remove operation pop(). We also include a method to test whether the stack is empty, as indicated in the following API:
stack-api

Video Lectures :
Stacks and Queues
Clients

Assignment:
Implement the ArrayStackOfIntegers ADT for Integers using arrays.

Other members that you need to implement:

import java.util.Iterator;
import java.util.NoSuchElementException;

public class ArrayStackOfIntegers implements Iterable {
    private Integers[] items;  // holds the items
    private int n;             // number of items in stack
    public ArrayStackOfIntegers(int capacity) {
        items = (Integer[]) new Object[capacity];
    }

    public boolean isEmpty() {
        ----
    }

    public boolean isFull() {
        ----
    }

    public void push(Integer item) {
        ----
    }

    public Integer pop() {
        
    }

    public Iterator iterator() {
        return new ReverseArrayIterator();
    }

    // an iterator, doesn't implement remove() since it's optional
    private class ReverseArrayIterator implements Iterator {

    ----
    public void remove() 
    {}
    }
}


An example of Iterable

The files in this site are cleaned up a bit here.

Use the ArrayStackOfIntegers ADT  to work on these assignments:
ArrayStackOfIntegers ADT – Calculator
Design and implement a client to calculate the following postfix expression: 8 4 -3 * 1 5 + / *

 
ArrayStackOfIntegers ADT – Enhanced Calculator
 
StackCalculatorEnhanced

 

Turtle Graphics: Koch – Circle Approximation

 

Screen Shot 2015-09-07 at 9.38.05 PM   Screen Shot 2015-09-07 at 9.41.17 PM

Classwork:

Set up your working environment

1. Create a folder to hold your projects for this class, Algorithms.
2. Create a folder for ADTs.
3. Create a project in ADTs with the classes presented in the pdf above. Ensure that your project contains the files needed to test your classes. Check edmodo.com for due dates.
4. Write your own main method to test your classes. Ensure you have good documentation.
5. Submit your work to edmodo.com.

Guidelines to submit your work:

  1. Find the assignment’s post
  2. Copy and paste your work on the post.
  3. If the assignment is a program, you also have to attach the file to the post.
  4. The file has to have your initials followed an underscore and the program name.

Every program has to have a header:

  • Assignment description
  • Author’s name
  • Date
  • If the program doesn’t run successfully, write a short paragraph as part of the program header explaining the problem.

Input and output:

  • If the assignment requires input and output is generated, both should be included in your program as comments.
  • If the output is an image or animation, submit a screen shot.

Using Turtle ADT
turtle_draw papert
Seymour Aubrey Papert (February 29, 1928 – July 31, 2016)
was a South African-born American mathematician, computer scientist, and educator, who spent most of his career teaching and researching at MIT.[1] He was one of the pioneers of artificial intelligence, and of the constructionist movement in education. He was co-inventor, with Wally Feurzeig, of the Logo programming language.
Logo
Papert used Piaget’s work in his development of the Logo programming language while at MIT. He created Logo as a tool to improve the way children think and solve problems. A small mobile robot called the “Logo Turtle” was developed, and children were shown how to use it to solve simple problems in an environment of play. A main purpose of the Logo Foundation research group is to strengthen the ability to learn knowledge.[7] Papert insisted a simple language or program that children can learn—like Logo—can also have advanced functionality for expert users.
From Wikipedia

Assignment:
Regular n-gons. Ngon.java takes a command-line argument n and draws a regular n-gon using turtle graphics or the Std library. By taking n to a sufficiently large value, we obtain a good approximation to a circle. Show your images as n increases. Implement an algorithm to find the smallest value for n to get the best approximation to a circle.
Document clearly your proof.
NOTE: only show few images

Homework:
Set up your working environment in your own computer.
Assignment:
Recursive graphics. Koch.java takes a command-line argument n and draws a Koch curve of order n. A Koch curve of order order 0 is a line segment. To form a Koch curve of order n:
Draw a Koch curve of order n−1
Rotate 60° counterclockwise
Draw a Koch curve of order n−1
Rotate 120° clockwise
Draw a Koch curve of order n−1
Rotate 60° counterclockwise
Draw a Koch curve of order n−1
Below are the Koch curves of order 0, 1, 2, and 3.
As the order of the curve increases, how is the area between the curve and the “x-axis” affected? Modify the Koch ADT to include a method that implements the area computation to assist you in answering the question.
Document clearly your proof.

Koch Snowflake

 

Cryptography – Caesar Cypher – Third Day

September 11th, 2017

The Caesar Cipher

The key for the Caesar Cipher will be a number from 1 to 26. Unless you know the key (that is, know the number used to encrypt the message), you won’t be able to decrypt the secret code.

The Caesar Cipher was one of the earliest ciphers ever invented. In this cipher, you encrypt a message by taking each letter in the message (in cryptography, these letters are called symbols because they can be letters, numbers, or any other sign) and replacing it with a “shifted” letter. If you shift the letter A by one space, you get the letter B. If you shift the letter A by two spaces, you get the letter C. Figure 14-1 is a picture of some letters shifted over by three spaces.

 

To get each shifted letter, draw out a row of boxes with each letter of the alphabet. Then draw a second row of boxes under it, but start a certain number (this number is the key) of spaces over. After the letters at the end, wrap around back to the start of the boxes. Here is an example with the letters shifted by three spaces:


Invent with Python

Making paper cryptography paper tools

 


A virtual Cipher Wheel

Assignments: visit edmodo.com to submit your work
My Cipher – Clwk 9/11/2017 – Caesar Cipher Device

1. Look at the Caesar Cipher Device on the link below. Create your own cipher and a device that can be used to encrypt and decrypt your messages.

2. Write a java program to encrypt and decrypt messages using your new cipher. Use the university SdtDraw.java to draw the device you designed for your cipher. The drawing is a representation of the cipher.

1. The program should display 2 options:
e. Encrypt a message
d. Decrypt a message
x. Exit

2. Prompt the user for the “key”. Make sure your prompt for the “key” includes all the information necessary for the user to understand what is needed when the message is decoded.

NOTE: The program should continue to display the menu until the user exits the application.

BAD Ciphers: reversing the order, flipping the letters, reversing and flipping the letters of the message, and anything that is similar to the Caesar Cipher with a silly twist to it.

DOCUMENTATION:
Header:


/** 
Full assignment description
Your Name
Date
**/

If there is any obfuscated code snippet, you must include a comment!

Footer:


/**
Multiple Input/output sessions.
**/

NOTE: your program name: MyCipher_YI.java