User talk:Sgs001sxm

Topic 1:

Topic 2:

Topic 3:

Topic 4:

Topic 5: Abstract Data Structures
Types of Data Structures

Static:

- Number of elements determined at creation of data structure

- Cannot be changed later on

- Can be memory inefficient, because RAM is allocated based on predetermined number of elements, rather than actual elements, leaving possibility of unused memory space

- Iterated through using FOR loops

- Examples: Arrays

	Dynamic:

- No need to predetermine number of elements, any number of elements can exist in data structure at any time

- Each element exists at a particular memory address and is referred to as a node.

- Each node contains a “reference” to the memory address of the next element in the data structure. This reference is called a pointer.

- Usually more memory efficient than static data structures

- Iterated through using WHILE loops

- Examples: Collections, Stacks, Linked Lists

Recursion:  - Recursion is when a method calls itself until some terminating condition is met, known as the base case.

- Recursion can be used in situations where the next result of an operation depends on the current result.

- Some good examples including the Fibonacci sequence or a factorial function

- Each step until the base case is reached is called a recursive step.

- All recursive functions can be written iteratively (without calling itself)

Handling Recursion Problems

- Start with initial parameter(s)

- Write down all the return statements, until you get to the point where the parameter matches your base case.

- At this point, you can turn your function call into an actual number and work your way back upwards

Recursion Example 1

def factorial(num):

if num == 0:

return 1;

return num*factorial(num-1)

factorial(4)→

return 4*factorial(3) = return 24

return 3*factorial(2) = return 6

return 2*factorial(1) = return 2

return 1*factorial(0) = return 1

factorial(0) = 1

So, factorial(1) = 1*1 = 1,

factorial(2) = 2*1 = 2,

factorial(3) = 3*2 = 6,

factorial(4) = 24

Recursion Example 2

mistier(n)

if ( n == 0):

return 1

Else:

return 4 * mistier(n-1) + 2

mistier(5)→

return 4 * mistier(4) + 2 = return 1706

return 4 * mistier(3) + 2 = return 426

return 4 * mistier(2) + 2 = return 106

return 4 * mistier(1) + 2 = return 26

return 4 * mistier(0) + 2 = return 6

mistier(0) = 1

So, mistier(1) = 6, mistier(2) = 26,

mistier(3) = 106, mistier(4) = 426,

mistier(5) = 1706

Stacks:
- Dynamic Data Structure

- LIFO (Last in First Out)

- The last element to be “pushed” (inserted) into the stack is the first to be removed (“popped”)

- Used to keep track of function calls

Queues:

- Dynamic Data Structure

- FIFO (First in First Out)

- The first element to be “enqueued” (inserted) is the last to be “dequeued” (removed) like a real-life queue

'''

Linked Lists
''' Singly Linked List:

- Accessed using the head, which is a pointer to the first element of the linked list

- A pointer leads to a memory address in RAM

- Every element has a pointer to the next element and an information field, which contains the actual data

- Can only move one way through the linked list

- Stop at the last element, cannot progress any further

- The last pointer is called the tail, which leads nowhere

	Doubly Linked List

- Accessed using the head, which is a pointer to the first element

- Each element has pointers to both the next and previous element, so it’s possible to move both forwards and backwards through the linked list

- The first and last elements have one pointer that points to the next or previous element and the other leads nowhere

'Circular Linked List'' '''

- Accessed using the head, which is a pointer to the first element

- Can only move in one direction, but the last element has a pointer to the first element, so you constantly move in a “circle”

Adding Elements to Linked List

- Add elements by pointing existing pointers to new element and by pointing the new element to previous and/or subsequent elements depending on the type of linked list

Binary Trees (aka Binary Search Trees)
- There is a pointer to the first node, which is at the top and called the root node

- Each node points to two other child nodes, one of which has a higher value and one of which has a lower value than the parent node.

- Nodes that point to no other nodes are leaves.

- All nodes to the left of the parent node will have numerical values lower than the parent node and all nodes to the right will have higher values

- You can generate a binary tree by following this rule, adding values to the tree in the order given.

- The structure of a binary tree mimics a binary search. You can find data in less than half the time required with a data structure like an array.

Traversing Binary Trees

- 3 Ways: Preorder, Inorder, Postorder

- The order can be ascertained by drawing a flag or a line from each element to left (preorder), to the bottom (inorder), or the right (postorder)

Inserting and Deleting Values

- When inserting values, you can simply pretend that your new number is the next number in the initial sequence and add it to the appropriate position

- When deleting values, you must remove the value, and then reconfigure the binary tree where necessary so that you are abiding by the the order in which numbers were added by the binary tree, but so that each element has numbers less than it on the left and greater than it on the left.

Topic 6:

Topic 7:

OOP: