Calculator and Linux .edu



Calculator and Linux

What is a tree??

LINK/EDGE

Element/NODE

Other parts of a tree

|Other parts of a tree |

|root |internal nodes |leafs |

| | | |

| | | |

| | | |

| | | |

| | | |

| | | |

| | | |

| | | |

| | | |

| | | |

|path |descendants | |

| | | |

| | | |

| | | |

| | | |

| | | |

| | | |

| | | |

| | | |

| | | |

| | | |

Relationships of a tree - Part 1

“A” – no parents

“A” - 2 children “K” and “Q”

“K” – Parent is “A” “Q” – Parent is “A”

“K” - 1 child “Z”

“Z” – Parent is “K”

“Z” – no children Z’s ancestors are “K” and “A”

Relationships of a tree - Part 2

None None

K Q

A A

Z None

K Parent

None Left Child

Parent

Right Child

Binary Tree Traversals

• the process of “visiting” or performing some operations on, each node of a tree is called TREE TRAVERSAL

• a traversal is to process each node ONCE

• There are 3 commonly used to traversals

o preorder

o postorder

o inorder

▪ they differ in the sequence in which the nodes are visited

• PREORDER/PREFIX

1. process root first

2. if (left child present, and not visited already) move to left, restart with Step 1 at new node

3. if (right child present, and not visited already) move to right, restart with Step 1 at new node

4. Else, go back up to parent, and start at step 3

▪ overall, the algorithm reaches the root first

• INORDER/INFIX

1. if (left child present, and not visited already) move to left, restart with Step 1 at new node

2. process root

3. if (right child present, and not visited already) move to right, restart with Step 1 at new node

4. Else go back up to parent, and start at step 3

• POSTORDER/POSTFIX

1. if (left child present, and not visited already) move to left, restart with Step 1 at new node

2. if (right child present, and not visited already) move to right, restart with Step 1 at new node

3. Else process root, go back to parent, and start at step 2

▪ overall, the algorithm reaches the root LAST

|Traversal Example |

| |PRE-ORDER |

| | |

| | |

| | |

| | |

| | |

| | |

| | |

| | |

| | |

| | |

| | |

| | |

| | |

| |INORDER |

| | |

| |POST-ORDER |

| | |

|Draw 3 Binary trees, and do the traversals |

|Tree with 9 nodes |PRE-ORDER |

| | |

| |INORDER |

| | |

| |POST-ORDER |

| | |

|Tree with 13 nodes |PRE-ORDER |

| | |

| |INORDER |

| | |

| |POST-ORDER |

| | |

|Tree with 18 nodes |PRE-ORDER |

| | |

| |INORDER |

| | |

| |POST-ORDER |

| | |

|Tree with 10 nodes |PRE-ORDER |

| | |

| |INORDER |

| | |

| |POST-ORDER |

| | |

|Tree with 7 nodes |PRE-ORDER |

| | |

| |INORDER |

| | |

| |POST-ORDER |

| | |

Expression Trees

• these are binary trees that when completed in order, represent a mathematical expression.

• all symbols, (+, -, /, …) are internal nodes

• all values are leaves

• the tree is a another presentation of the expression

o to convert back into a math equation, start from the very bottom and work up

|Expression Tree Example |

|Tree Representation |Math Equation |

|(Lewis & Chase 2005) |(5 – 3) * 4 + 9 |

| | |

| | |

| | |

| | |

| | |

| | |

| |5 x 4 + (3 + 2/1) |

| | |

| | |

| | |

| | |

| | |

// Build a few trees of your own with the given info

|Tree Representation |Math Equation |

| |8 * 2 / 3 + 8 – (5 – 3) |

| | |

| | |

| | |

| | |

| | |

| | |

| | |

| |(4 * 8) + 4 / (3 – 7) |

| | |

| | |

| | |

| | |

| | |

| | |

| | |

| |7 – 8 + 7 * (3 /6 * 6) |

| | |

| | |

| | |

| | |

| | |

| | |

| | |

Chase’s Calculator Quick Reference

|infixToPostfix.java |

|//************************************************************ |

|// |

|// infixToPostfix.Java Authors: Lewis, Chase, Coleman |

|// |

|// Provides an implementation of an infix to postfix |

|// converter for expressions |

|// |

|//************************************************************ |

|/** |

|Infix to Postfix Conversion: |

| |

|* Scan the input String using Scanner |

|* While there are more tokens |

|o If the next token is of length greater than 1, it is a multiple digit number and is added to the result |

|o Else if the next token is 1 digit number, it is added to the result |

|o Else if the next token is a right parenthesis, |

|* Pop elements off of the stack, adding them to the result until the top element of the stack is a matching left parenthesis |

|* Pop the left parenthesis off of the stack |

|o Else if the next token is an operator (+,-,*,/) |

|* Then compare the token to the top of the stack to determine precedence |

|* While the operator on the stack has precedence |

|* Pop the top element off of the stack and add it to the result |

|* Push the current operator on the stack |

|* While there are elements remaining on the stack |

|o Pop the top element off of the stack and add it to the result |

|* the result is a postfix expression in reverse order so reverse it and return it |

| |

|*/ |

| |

|import jss2.*; |

| |

|import java.util.Scanner; |

| |

|public class infixToPostfix |

|{ |

| |

|/********************************************************** |

|Constructor |

|**********************************************************/ |

|public infixToPostfix() |

|{ |

| |

|} |

| |

|/********************************************************** |

|Returns a postfix expression of this infix string |

|as a list. |

|@param infix infix expression |

|**********************************************************/ |

|public ArrayUnorderedList convert(String infix) |

|{ |

|ArrayUnorderedList tokenList = new ArrayUnorderedList(); |

|ArrayStack postStack = new ArrayStack(); |

| |

|int toPush=0,operand1=0, operand2=0; |

|boolean precedence=true; |

|char tempChar; |

|String tempToken; |

|String input; |

|input = infix; |

|Scanner s = new Scanner(input); |

| |

|for(int scan = 0; scan < input.length(); scan++) |

|{ |

|while(s.hasNext()) |

|{ |

|tempToken = s.next(); |

|tempToken = tempToken.toString(); |

| |

|//the only valid taken of length greater than 1 is a |

|//multiple digit number, thus if the token is of |

|//length greater than 1 add it to the result |

| |

|if(tempToken.length()>1) |

|{ |

|tokenList.addToFront(tempToken); |

|} |

|else if(tempToken.length() ==1) |

|{ |

|//if the token if of length 1 and is a digit, add |

|//it to the result |

| |

|tempChar = tempToken.charAt(0); |

|if(tempChar >= '0' && tempChar 1 |

|}//end for |

|}//end while |

| |

|//place whatever token remain on the stack on the result list |

| |

|while( !postStack.isEmpty() ) |

|{ |

|tokenList.addToFront(postStack.pop()); |

|} |

| |

|s.close(); |

| |

|int counter = tokenList.size(); |

|ArrayUnorderedList reverseorder = new ArrayUnorderedList(); |

|for (int i=0; i 0) |

|{ |

|tempToken = tokenList.removeFirst(); |

| |

|//operator of length greater than 1 |

| |

|if(tempToken.length()>1) |

|{ |

|inStack.push(new Integer(Integer.parseInt(tempToken))); |

| |

|} |

|else if(tempToken.length()==1) |

|{ |

|tempChar = tempToken.charAt(0); |

| |

|//if operator |

| |

|if(tempChar >= '0' && tempChar “Accessories” -> “Text Editor” |

|Type in program “HelloWorld.java” |

|IN THE TERMINAL compile HelloWorld.java type: |

| |

|javac HelloWorld.java |

| |

|IN THE TERMINAL run HelloWorld (if no errors) type: |

| |

|java HelloWorld |

|[pic] |

| |

|Notice a VERY small “Hello” does appear. |

Compiling using Eclipse

• Much like JCreator, PFE, but nicer

• In Eclipse, you have to create a PROJECT first, then add files TO IT.

o When you run the program for the first time, hit “X” on the tab that contains the blue screen

[pic]

• Creating a Project

o File -> New -> Project

o Give the Project a name, notice IT CAN HAVE SPACES!!

[pic]

• Creating a Java File

o first MAKE SURE THE PROJECT is HIGHLIGHTED on left

o File -> New -> Class

o Select a name, DO NOT PUT “.java” behind the name

[pic]

• Compiling

o ONLY NEED TO COMPILE FROM THE DRIVER!!!

o WILL DO THE REST FOR YOU!!

o Find “Run” on the Menu bar, then “Run As”, “Java Application”

Submitting work

Submitting any work goes to a folder ONLY a professor can access much like uploading a file on the toolkit

Instructions:

Open Terminal Window

type: ssh rucs

type: vt100

if you get a menu type: 9

type: cd workspace

type: ls

type: cd projectname (this may NOT be needed)

type: submit itec220-0? file.java

Remote Linux, SSHING to RUCS

• If you do not have a Linux machine at home, you can still go into the platform here at school

• must use a SSH program, NOT FTP

|SSH Important Screens |

|[pic] |[pic] |

• For “Host Name” use: rucs.radford.edu

• The rest is self explanatory

• All command still work.

• Can only compile with command line (old fashioned way) only using SSH

Tab is your friend

• When in the Terminal Window, the TAB can be valuable

• if there is a LONG filename or directory, AFTER you have typed a few letters it, hit TAB and it will try to match what you are looking for.

|Before Tab |

|[pic] |

|After Tab |

|[pic] |

Taking a ScreenShot

• Top most bar, right click and “Add to Panel”

• Scroll through the list and find “Take Screenshot”

• Notice a camera will appear at the top of the screen

• Now you can use (smaller window) or (full size)

Sticky Notes!! Very useful!!

• Top most bar, right click and “Add to Panel”

• Scroll through the list and find “Sticky Notes”

• Notice a notepad will appear at the top of the screen

-----------------------

Z

Q

K

A

V

F

P

G

E

+

9

*

4

-

3

5

+

*

+

5

4

3

/

1

2

A

Q

K

F

Z

Z

F

Q

K

A

Z

F

Q

K

A

Z

F

Q

K

A

Z

F

Q

K

A

Z

F

Q

K

A

A

Q

K

Z

A

Q

K

Z

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download