2.3.2 algorithms for main data structures

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/12

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

13 Terms

1
New cards

stacks

  • first in, last out (FILO) data structure

  • they are often implemented as an array and use a single pointer which keeps track of the top of the stack (called the top pointer)

  • this points to the element which is currently at the top of the stack

  • the top pointer is initialised at -1; this is because the first element in the stack is in position 0, and having the top initialised at 0 would suggest there is an element in the stack, when in fact the stack is empty

2
New cards

stack operations

  • size - checks the size

  • isEmpty - checks if the stack is empty

  • peek - return the top element without removing it

  • push - add an element to the top of the stack

  • pop - remove and return the top element from the stack

3
New cards

queue

  • first in, first out (FIFO) data structure

  • unlike stacks, queues make use of two pointers: front and back / head and tail

  • while front holds the position of the first element, back stores the next available space

4
New cards

queue operations

  • size - checks the size

  • isEmpty - checks if the stack is empty

  • peek - return the top element without removing it

  • enqueue - add an element to the queue

  • dequeue - remove and return element from the front of the queue

5
New cards

linked list

  • data structure that provided a foundation upon which other structures can be built

  • composed of nodes, each of which contains data has a pointer to the next item in the list

  • circular linked list - last node points to first node

  • items dont need to be stored contiguously due to pointers

6
New cards

trees

  • formed from nodes and edges, which cannot contain cycles and aren’t directed

  • trees are useful as a data structure because they can be traversed

  • used for storing and managing file and folder structures

<ul><li><p>formed from nodes and edges, which cannot contain cycles and aren’t directed</p></li><li><p>trees are useful as a data structure because they can be traversed</p></li><li><p>used for storing and managing file and folder structures</p></li></ul><p></p>
7
New cards

binary tree

similar to a standard tree but each node can only have 0, 1 or 2 pointers

8
New cards

adding to a binary tree

  1. check if there is free memory

  2. create a new node

  3. if tree is empty, new node becomes the root, create a start pointer to it

  4. if not, starting at the root node, check if the new value should be placed before or after the current node

  5. before = left, after = right

  6. continue until a leaf node is reached

  7. find out if the new value comes before or after the leaf

  8. set new pointer to the new value

9
New cards

removing from a binary tree

  1. find node we want to delete

  2. set that node as the current node

  3. while the current node exists

    1. set the previous node the same as the current node

    2. check if item to be deleted is less or greater than the current node

    3. less = follow left pointer, right = follow right pointer

    4. back to stage 3

  4. if the node being deleted is a leaf

    1. if its less than, previous nodes pointer = null

    2. if greater than, right pointer = null

  5. if node being deleted has 1 child

    1. if less than, set the previous nodes left pointer to the current nodes left child

    2. if greater than, set right pointer to the current nodes right child

  6. if the node being deleted has 2 children

  7. replace it with the smallest value in the right sub-tree

10
New cards

pre-order traversal

  • node left right

  • dot on left, start at root

  • start at root

  • output node

  • follow left pointer and repeat step 2 until there is no pointer to follow

  • follow right pointer and repeat step 2 until there is no pointer to follow

<ul><li><p>node left right</p></li><li><p>dot on left, start at root</p></li><li><p>start at root</p></li><li><p>output node</p></li><li><p>follow left pointer and repeat step 2 until there is no pointer to follow</p></li><li><p>follow right pointer and repeat step 2 until there is no pointer to follow</p></li></ul><p></p>
11
New cards

in-order traversal

  • left node right

  • dot on bottom, start on most left node

  • start at toot node

  • follow left pointer and repeat from step 2 until there is no pointer to follow

  • output the node

  • follow right pointer and repeat step 2 until there is no pointer to follow

<ul><li><p>left node right</p></li><li><p>dot on bottom,  start on most left node</p></li><li><p>start at toot node</p></li><li><p>follow left pointer and repeat from step 2 until there is no pointer to follow</p></li><li><p>output the node</p></li><li><p>follow right pointer and repeat step 2 until there is no pointer to follow</p></li></ul><p></p>
12
New cards

post order traversal

  • left right node

  • dot on right, start at furthest left node

  • start at root node

  • follow left pointer and repeat from step 2 until there is no pointer to follow

  • follow right pointer and repeat step 2 until there is no pointer to follow

  • output the node

<ul><li><p>left right node</p></li><li><p>dot on right, start at furthest left node</p></li><li><p>start at root node</p></li><li><p>follow left pointer and repeat from step 2 until there is no pointer to follow</p></li><li><p>follow right pointer and repeat step 2 until there is no pointer to follow</p></li><li><p>output the node</p></li></ul><p></p>
13
New cards

breadth first

  • start from root, visit all the roots children starting from the left child

  • the algorithm then visits all nodes directly connected to each of those nodes in turn, continuing until every node has been visited

  • layer by layer

<ul><li><p>start from root, visit all the roots children starting from the left child</p></li><li><p>the algorithm then visits all nodes directly connected to each of those nodes in turn, continuing until every node has been visited</p></li><li><p>layer by layer</p></li></ul><p></p>