Uploads Seng206 Week3
Uploads Seng206 Week3
(SENG206)
@yenalarslan1
https://www.linkedin.com/in/yenalarslan
[email protected]
Chomsky Hierarchy
Type 3 - Regular Grammars: In this group, each rule defines how a symbol
can be replaced with other symbols, but the rules are very simple and can
only involve a finite number of symbols.
Chomsky Hierarchy
An <expression> can be a <term>, or an <expression> followed by a plus or minus sign and another
<term>.
A <term> can be a <factor>, or a <term> followed by a multiplication or division sign and another
<factor>.
A <factor> can be a <number>, or an <expression> enclosed in parentheses.
A <number> consists of one or more <digit>s.
Recursive
The term "recursive" in programming refers to the
technique where a function calls itself directly or indirectly
to solve a problem. Recursive functions break down a
problem into smaller, more manageable sub-problems,
each of which is solved by a call to the same function.
def factorial(n):
# Base case: factorial of 0 or 1 is 1
if n == 0 or n == 1:
return 1
# Recursive case: n! = n * (n-1)!
else:
return n * factorial(n-1)
# Example usage
print(factorial(5)) # Output: 120
Recursive
Factorial Function in C#:
using System;
class Program
{
// Recursive Function to calculate Factorial of a number
static int Factorial(int n)
{
// Base case
if (n == 0)
{
return 1;
}
// Recursive case
return n * Factorial(n - 1);
}
// Driver Code
static void Main()
{
int n = 4;
function fibonacci(n) {
// Base cases
if (n === 0) return 0;
if (n === 1) return 1;
// Recursive case
return fibonacci(n-1) + fibonacci(n-2);
}
// Example usage
console.log(fibonacci(10)); // Output: 55
Example of BNF:
<if-statement> ::= "if" "(" <condition> ")" "{" <program> "}" | "if" "(" <condition> ")" "{" <program> "}" "else" "{" <program> "}"
<condition> ::= <expression> "<" <expression> | <expression> ">" <expression> | <expression> "==" <expression>
<alpha> ::= "a" | "b" | "c" | ... | "z" | "A" | "B" | "C" | ... | "Z"
<digit> ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
Example of BNF:
Variable Declarations (<var-declaration>): Starting with the keyword "var", followed by an identifier, and
terminated with a semicolon.
Assignments (<assignment>): Specifying a variable name, an equals sign, an expression representing the
new value, and ending with a semicolon.
If Statements (<if-statement>): Conditional execution based on the evaluation of a condition. An optional else
branch can execute an alternative program.
Expressions (<expression>, <term>, <factor>): Allowing arithmetic operations within expressions. Terms and
factors break down the expression into parts that can be evaluated according to the rules of arithmetic
precedence.
Conditions (<condition>): Comparing two expressions using less than, greater than, or equals operators.
Binary Trees
Inorder Traversal: D, B, A, E, C, F
Tree traversal
Postorder Traversal (Left, Right, Root):
1. Start with the left subtree of the root (1), which is the subtree rooted
at 2.
2. Go to the left subtree of 2, which is the subtree rooted at 4.
3. Visit 8 (left of 4), which is a leaf node. So, visit it first.
4. Then, visit 9 (right of 4), another leaf node.
5. Having visited both children of 4, now visit 4 itself.
6. With the left subtree of 2 processed, move to the right subtree of 2,
which is 5, a leaf node. Visit 5.
7. Having visited both children of 2 (the subtrees rooted at 4 and 5), now
visit 2.
8. With the entire left subtree of the root processed, move to the right
subtree of the root (1), which is the subtree rooted at 3.
9. Visit 6 (left of 3), a leaf node, so visit it.
10. Then, visit 7 (right of 3), another leaf node.
Postorder Traversal: 8, 9, 4, 5, 2, 6, 7, 3, 1.
11. Having visited both children of 3, now visit 3 itself.
12. Having visited both the left and right subtrees of the root, finally visit
the root node, 1.
Challenge