142-data-structures Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

arrays

A
  • an area of contiguous memory
  • used to store variables (of the same type)
  • a set of data items of the same type grouped together using a single identifier
  • each of the data items is addressed by the variable name and a subscript
  • the first element is stored at index 0
  • 9369319 is stored at prime[0]
    Arrays hold multiple elements of data, but all elements must be of the same data type.
    An array is an ordered, finite set of elements of a single type.
    A 1D array is linear and is always taken as zero-indexed unless stated otherwise.
    Elements are selected using the syntax: oneDimensionalArray[x], where x is the position of the element.
    Arrays are a static data structure and cannot be changed in size once set up.
    Lists are different from arrays because the elements in a list are not necessarily contiguous.
    Arrays support only a single data type (homogeneous), and most other languages support arrays as unique data structures.
    Arrays are mutable and can be changed after they have been created.
    Arrays are ordered collections of items.
    random access - the ability to access a specific element directly given its index. This is possible in an array
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

records

A

A record is a row in a file that consists of fields, and is commonly used in databases.
Each field in a record can be identified using the syntax recordName.fieldName.
Records are made up of fields, which are variables that can have different data types.
A record must first be created by defining its structure and creating a variable from that structure.
The syntax for creating a variable from a record structure is: variableName : recordStructureName.
The attributes of a record can be accessed using the syntax variableName.attributeName.
Records are useful for collecting together related variables and data.
Steps for using a record data structure include defining the record structure, declaring a variable or array to use with the record structure, and assigning/retrieving data from the variable record.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

list

A

A list is a data structure that holds multiple items of different data types.
Items in a list can occur more than once and are ordered.
Unlike arrays, items in a list can be stored non-contiguously and can be of more than one data type.
Lists are mutable, meaning they can be changed after they have been created.
Items in a list can be added, removed, or replaced, making them a flexible and dynamic data structure.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

list operations

A

isEmpty= checks if list is empty
append()= adds a new value to end of list
remove()= removes value first time it appears in list
search= searches for value in list
length= returns length of list
index= returns position of item
insert(position, value) = insert value at a given position
pop()= returns and removes the last value in list
pop(position)- returns and removes the value in list at given position

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

linked list

A

A linked list is a dynamic data structure that holds an ordered sequence of items.
Each item is an object called a node with a data field and a pointer field to the next node.
The last node has a null pointer and the location of the first node is stored in a variable.
Linked lists are not fixed in size and can store data anywhere in memory.
The first item is called the head and the last item is called the tail.
Traversing a linked list involves following the pointers from one node to the next until the end is reached.
Linked lists provide a foundation for other structures like stacks, queues, graphs, and trees.
Doubly linked lists have an extra pointer to the previous node, and circular linked lists have the last node pointing to the first node.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the applications of a linked list?

A

Operating systems to manage process blocks in a ready state
Image viewers to switch between previous and next images
Music players to store tracks in a playlist
Web browsers to navigate backwards and forwards
Hash table collision resolution as an overflow
Maintaining a file allocation table of linked clusters on secondary storage

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Operations that can be performed on a linked list:

A

Add: adds a node to the linked list
Delete: removes a node from the linked list
Next: moves to the next item in the list
Previous: moves to the previous item in a doubly linked list
Traverse: a linear search through the linked list

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Manipulating a linked list:

A

Adding a node involves editing pointers and updating pointer fields
Removing a node involves updating pointer fields to bypass the deleted node
Linked lists waste memory when nodes are deleted but not removed from the list
Linked lists require more memory than arrays since they store pointers
Items in linked lists can only be traversed in sequence

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Adding an item to a linked list:

A

Check there is free memory for a new node
Create a new node and insert data into it
If the linked list is empty, the new node becomes the first item
If the new node should be placed before the first node, the new node becomes the first node and points to the second node
If the new node is to be placed inside the linked list, traverse the list to find the correct position and update pointers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Deleting an item from a linked list:

A

Check if the linked list is empty
If the first item is the item to delete, set the start pointer to point to the next node
If the item to delete is inside the linked list, traverse the list to find the item and update pointers to bypass it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

linked lists algorithm

A

class Node
//the node class
property next
//stores the next node in the chain
property data
//stores the data for this node
end class
class LinkedList
property head
//stores the node at the start of the chain
procedure printNodes()
//iterates through all the nodes and prints them
current = head
//this structure can be used for searching for a node as well
while (current!=null)
print(current.data);
current=current.next;
end while
end procedure
procedure AddLast(data)
//adds an item to the end of the list
if (head==null)
//if there is no head node then add at the start
head=new Node()
head.data=data;
head.next=null;
else
toAdd=newNode()
toAdd.data=data;
Node current=head;
while (current.next!=null)
//iterate through the nodes until a null one is found
current=current.next
end while
current.next=toAdd
//adds the node once the end has been found
end if
end procedure
end class
//the same method can be used to iterate through the nodes to find one to delete

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Data Structure Types

A

Static - structure size cannot change at runtime
Dynamic - structure size can change at runtime
Immutable - structure size and data cannot be changed at runtime
Mutable- structure size and data can be changed at runtime

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Are tuples a dynamic or static data structure?

A

Tuples can be either dynamic or static depending on their implementation. A tuple with a fixed number of items and immutable data is considered a static data structure, while a tuple with variable-sized data or a mutable list is considered dynamic. For example, score=(5,0,12) is a static immutable tuple, while score = (“John”, 5, scorelist[]) is a dynamic mutable tuple that can change size and data at runtime.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

tuple

A

A tuple is an ordered set of values of any type.
Tuples are like lists but are immutable; their elements cannot be added, removed or changed once a tuple has been created.
Tuples are initialised using regular brackets and elements are accessed in the same way as elements in an array.
Tuples are a fixed size set at the point of creation, making them an immutable data structure.
Tuples can store more than one data type and are an unordered collection of items.
Example code: animals = (‘cat’, ‘dog’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

graph algorithm

A

public class Program
{
public static void Main()
{
//create node list
List<string>node=new List<string>();
node.Add("A");
node.Add("B");
node.Add("C");
node.Add("D");
node.Add("E");</string></string>

//create list of edges. Each entry to the list is an instance of the Edge class
List<Edge>edges=new List<Edge>();
edges.Add(new Edge("E","A",7));
edges.Add(new Edge("E","B",4));
edges.Add(new Edge("B","C",9));
edges.Add(new Edge("C","D",2));
edges.Add(new Edge("D","B",3));</Edge></Edge>

//check if edge(B,C) exists
foreach(Edge e in edges){
if (e.start==”B” && e.end==”C”){
Console.WriteLine(“Edge exists”);
}
}
//find all nodes connected to B
foreach(Edge e in edges){
if (e.start==”B”){ //finds all the edges that start with B
Console.WriteLine(e.end);
}
if (e.end==”B”){ //finds all the edges that end with B
Console.WriteLine(e.start);
}
}
}
}
class Edge
{
public string start; //the Edge class stores the start node
public string end; //end node
public int weight; //and weight of the edge
public Edge(string s, string e,int w){ //constructor method
this.start=s;
this.end=e;
this.weight=w;
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Graph

A

A graph is a set of vertices/nodes connected by edges/arcs.
Graphs can be directed or undirected, and weighted or unweighted.
Graphs can be implemented using an edge class that stores start node, end node, and weight of each edge.
Nodes are stored in a list and edges are stored in a separate list.
Computers can process graphs using an adjacency matrix or an adjacency list.
Graphs allow each vertex to have more than two edges and point to any vertex in the data structure.
Edge values can represent distance, time, bandwidth, or other relationships between vertices.
Edge values are required for some algorithms using graph data structures like Dijkstra’s algorithm.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Graph applications

A

Mapping road networks for navigation systems
Storing social network data
Resource allocation in operating systems
Representing molecular structures and geometry.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Graph implementations

A

Graphs can be implemented using:

  • Objects or a dictionary called adjacency lists
    or
  • An array called an adjacency matrix, where rows and columns represent vertices and edges, and 1 represents an edge between two vertices.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

inked list implementations

A

Implementing linked lists using an array:

Uses a static array stored contiguously in memory.
Requires the use of an index register to locate items.
The items may be stored in a different order than in the linked list due to pointers.
Implementing linked lists using objects:

Any available memory address can be used to store data.
Each node points to the next in the structure, so the data does not need to be adjacent.
The memory footprint is not determined at compile time and can change dynamically at runtime.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

graph operations

A

Adjacent: returns whether there is an edge between two vertices
Neighbours: returns all the vertices between two vertices
Add: adds a new vertex to the graph
Remove: removes a vertex from the graph
Add edge: adds a new edge connecting one vertex to another
Remove edge: removes an edge connecting two vertices
Get vertex value: returns the data stored in a vertex
Set vertex value: sets the data for a vertex
Get edge value: returns the value of an edge between two vertices
Set edge value: sets the value of an edge between two vertices

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Add operations on a graph

A

No single algorithm for adding an item to a graph, as it depends on the graph being used and the item being added. Efficient algorithm needed to traverse the graph to find a specific node. Binary tree provides a specific algorithm for deleting an item.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Search operations that can be performed on a graph:

A

Depth first search: visits each vertex and explores each branch before backtracking.
Breadth first search: visits each neighbouring node before moving to the vertices at the next depth.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Traversal operations that can be performed on a graph:

A

Pre order traversal
In order traversal
Post order traversal

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

adjacency matrix

A

way of representing a graph where each vertex is assigned a numerical index and the presence or absence of an edge between vertices is represented in a matrix format. This matrix has rows and columns representing vertices, and a 1 or 0 in the intersection of each row and column indicating whether there is an edge between those two vertices. The adjacency matrix is convenient to work with because it provides quicker access times to check if there is an edge between two vertices. It is also easy to add new nodes to the graph, as adding a new row and column to the matrix is a simple task

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

adjacency list

A

adjacency list is a way of representing a graph where each vertex is stored as a node and each node maintains a list of its adjacent vertices. This means that each vertex has a list of all the vertices it is connected to. The adjacency list is more space efficient for large, sparse networks, where most vertices are not connected to each other. This is because it only stores information about the vertices that are actually connected, rather than creating a large matrix where most entries are 0. However, working with an adjacency list can take longer to check if two vertices are adjacent, as it requires searching through a list to find the desired vertex.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

Stack

A

Stacks are a LIFO (last in, first out) data structure
Stacks have a top pointer that keeps track of the top element
They can be implemented as either static or dynamic structures
Stack operations include push (adding to the top), pop (removing from the top), and peek (viewing the top element without removing it)
Stacks are commonly used for reversing actions, such as going back a page in a web browser or using an “undo” button
Stacks are essential for computer operations and are often implemented using a pointer to the top element.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

Stack overflow

A

any attempt to push an item onto a full stack

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

Stack underflow

A

any attempt to pop an item off of an empty stack

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

stack implementations

A

Stacks can be implemented using arrays or object-oriented techniques.
Stacks implemented using arrays are static data structures with a limited number of spaces to store items.
Object-oriented implementation allows stacks to grow and shrink dynamically, assuming there is enough memory.
The algorithm for pushing an item onto a stack using an object-oriented approach involves checking for stack overflow, creating a new node and inserting data into it, having the new node point to the previous node, and setting the stack pointer to point to the new node.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

stack applications

A

Stacks are used by processors to track the flow of programs, allowing subroutine calls to be nested, and for returning to the previous value of the program counter.
Local variables are held on the stack, and values are lost when a subroutine ends.
Interrupts can be handled with a stack.
Stacks are used for performing depth first searches on graph data structures.
Stacks can keep track of user inputs for undo operations and backtracking algorithms, such as pathfinding maze solutions.
Stacks are used for evaluating mathematical expressions without brackets using a shunting yard algorithm and reverse Polish notation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

stack operations

A

Push - adding an item to the top of the stack
Pop- removing an item from the top of the stack
Peek- returning the value from the top of the stack without removing it
Check size - size ()
Check if empty- isEmpty()
Return top element but don’t remove- peek()
Add to stack- push(element)
Remove top element from stack and return removed element - pop()
Check if full - isFull()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

stack algorithms - push pseudocode

A

stack=new array[10]
pointer=-1

procedure push (item)
pointer=pointer+1
//increment pointer
if pointer>=stack.length
//check not stack overflow
print(“stack full”)
else
stack[pointer]=item
//add item to stack
end if
end procedure

33
Q

stack push algorithm logic steps

A

Adding items to stacks using an array - performed with a push operation - we push the new item onto the top of the stack. 1. Check for stack overflow, output ab error if the stack is full. 2. Increment the stack pointer. 3. Insert the new data item at the location pointer to by the stack pointer

34
Q

stack algorithms - peek

A

function peek
return stack[pointer]//return the item at the pointer index
end function

35
Q

stack algorithms - pop

A

procedure pop
if pointer=-1
//check stack isn’t empty
print(“stack empty”)
else stack[pointer]=null
//delete item
pointer=pointer-1
//move pointer down
end if
emd procedure

36
Q

stack pop algorithm logic

A

To remove an item from a stack using a pointer:

Check for stack underflow by ensuring that the stack is not empty. If the stack is empty, output an error.
Copy/output the data item pointed to by the pointer.
Decrement the pointer to point to the previous item.
To remove an item from a stack using an object-oriented technique:

Check for stack underflow by ensuring that the stack pointer is not null. If the stack pointer is null, output an error.
Output the node pointed to by the stack pointer.
Set the stack pointer to the previous item.
Note: The pointer does not have to point to the top item in the stack, it can always point to the next available space.

37
Q

queue (dyanamic)

A

Queues are a type of linear data structure, following a first in, first out (FIFO) approach.
Items are added to the back of the queue and removed from the front of the queue.
Queues can be implemented using arrays or linked lists.
They are commonly used in systems such as printers, keyboards, and simulators.
Queues can have special features like priority queues, where certain items can jump the queue based on their priority.
Two pointers are used to keep track of the front and back of the queue.
The front pointer points to the first item, while the back pointer points to the last item (also called a tail pointer).
Queues can also have circular data structures, where the front and back pointers wrap around to the beginning of the queue.
It is possible to peek at the front item without deleting it.
Queues have a FIFO structure, meaning the first item added to the queue is the first one to be removed.

38
Q

queue overflow

A

an attempt to enQueue an item in a full queue

39
Q

queue underflow

A

dequeue an item from an empty queue

40
Q

How is a queue implemented?

A

Front and back pointers are used to implement a queue. It can be done using an array or object-oriented technique. In an array, items are added in the next available space and removed from the front of the queue. In object-oriented programming, nodes are used to represent items in the queue.

41
Q

Linear Queue

A

A linear queue is a data structure consisting of an array. Items are added and removed from the front of the queue. Two pointers are used to keep track of the front and back of the queue. As the queue removes items, there are addresses in the array which cannot be used again, making a linear queue an ineffective implementation of a queue.

42
Q

Circular Queue

A

A circular queue is a type of queue that uses a circular data structure. It is similar to a linear queue in that it is a FIFO structure, but it can loop back to the front of the array and store values there once the queue’s rear pointer is equal to the maximum size of the queue, provided that it is empty. Therefore, circular queues can use space in an array more effectively, although they are harder to implement.

43
Q

Circular Queues in Game Design

A

Circular queues are useful in game design when the number of sprites on the screen affects the frame rate. By openly spawning sprites up to the limit of the queue, a stable frame rate can be achieved.

44
Q

What are the applications of a queue

A

Process scheduling, transferring data between processors and printer spooling, performing breadth first searches on graph data structures

45
Q

queue operations

A

.enQueue(value) - adds new item to end of queue. Increments the back pointer
.deQueue() - removes item from front of queue. Increments the front pointer
.isEmpty()- checks if queue is empty by comparing the front and back pointer
.isFull()-checks if queue is full by comparing back pointer and queue size
size(): Check the number of elements in the queue
peek(): Return the front element without removing it

46
Q

queue algorithms - enqueue

A

queue=new array[10]
procedure enqueue(item)
rear=rear+1 mod n
//increments rear using mod (circular)
if rear=front
//check queue isn’t full
print(“Queue full”)
else
queue[rear]=item
//add item to rear
end if
end procedure

47
Q

Adding an item to a queue (enqueue) using a array

A
  1. check for queue overflow. Output an error if the queue is full. 2. Increment the back/tail pointer. 3. Insert the new data item at the location pointed to by the back/tail pointer
48
Q

queue algorithms- dequeue

A

function dequeue
if front=rear
//check queue isn’t empty
print(“queue empty”)
else
data=queue[front]//get item at front
front=front+1//increment front
return data//return
end if
end function

49
Q

Method for removing an item from a queue:

A

Check for queue underflow. If the queue is empty, output an error message.
Copy/output the data item pointed to by the front/head pointer.
Increment the front/head pointer to remove the item from the queue.

50
Q

Method for removing an item from a queue using object-oriented techniques:

A

Check for queue underflow. If the front pointer does not point to a node, output an error message.
Output the node pointed to by the front pointer.
Set the front pointer to the previous item in the queue to remove the item from the queue.

51
Q

Traversing a stack or queue:

A

Classic implementation of a stack or queue supports push/enqueue, pop/dequeue, and peek operations
There are no operations to expose elements in the middle of the stack or queue
To traverse the stack or queue, we can:
Use peek to look at the top/front item without altering it
Repeatedly pop/dequeue items to output the contents, following the same process as removing items one by one.

52
Q

trees

A

Trees are formed from nodes and edges, with no cycles and not directed.
Trees are useful as a data structure because they can be traversed in different ways.
- non linear dynamic data structure where data items can be thought of as occurring at different levels.
- there are links (pointers) between items at one level and their descendants at the next level
There are two types of traversal: depth first and breadth first.
Trees have a root node at the top, and nodes are connected with branches or arcs.
Nodes can be classified as parents, children, leaves, and a subtree is a parent and all of its children.
A tree consists of nodes and pointers, with each tree having a root node and nodes at the bottom called leaf nodes.

53
Q

Subtree

A

A set of nodes and edges from any single node down through all of its descendants

54
Q

Applications of a rooted tree:

A

Storing and managing file and folder structures in operating systems
Implementations of the A* pathfinding algorithm in game development and robotics
Storing and manipulating any form of hierarchical data that requires a parent node to have zero, one or more child nodes (e.g a family tree, or corporate structure)

55
Q

tree operations

A

Operations on a tree: add, delete, binary search, traversal (pre-order, in-order, post-order, breadth-first)
Add: adds a new node to the tree
Delete: removes a node from the tree
Binary search: searches for and returns the data stored in a node
Traversal: visiting all nodes in a tree in a specific order
Pre-order traversal: a type of depth-first search that visits the current node before its children
In-order traversal: a type of depth-first search that visits the current node between its left and right children
Post-order traversal: a type of depth-first search that visits the current node after its children
Breadth-first search: visits all nodes at the same level before moving to the next level

56
Q

trees algorithms - adding data

A

Adding data
start at root
repeat
compare new data with current data
if new data<current data,follow left pointer
else follow right pointer
until pointer is null
write new data
create (null) pointers for new data

57
Q

trees algorithm -deleting data

A

Deleting data
if number of children=0 then
delete node by setting parent pointer to null
else if number of childen=1 then
change pointer of parent to point at child
else
find next biggest value by traversing right one then left until a leaf node is found
remove next biggest value
change node to be next biggest value

58
Q

binary tree

A

A binary tree is a tree-like data structure in which each node has at most two children.
Binary trees are commonly used to implement binary search algorithms due to the ease of searching through the information.
Nodes in a binary tree are typically represented with a left and right pointer.
Binary trees can be implemented using two-dimensional arrays.
Binary trees are a special case of a graph, with nodes represented as vertices and pointers represented as edges.

59
Q

Adding an item to a binary tree

A

Check for available memory
Create a new node and insert data
If the binary tree is empty, start at the root node
Follow the left pointer if the new node should be placed before the current node
Follow the right pointer if the new node should be placed after the current node
Repeat until a leaf node is reached
Set the left pointer to the new node if it should be placed before the current node
Set the right pointer to the new node if it should be placed after the current node.

60
Q

Deleting a node from a binary tree

A

Set the previous node variable to be the same as the current node
If the item to be deleted is less than the current node, follow the left pointer and set the discovered node as the current node
If the item to be deleted is greater than the current node, follow the right pointer and set the discovered node as the current node
If the node to be deleted has no children, set the previous node’s left or right pointer to null
If the node to be deleted has one child, set the previous node’s left or right pointer to the current node’s child node
If the node to be deleted has two children, find the smallest leaf node in the right sub tree, change the current node to the smallest leaf node, and remove the smallest leaf node. If the right node exists and has no left sub tree, set the current node to be the current node’s right pointer and set the current node’s right pointer to null.

61
Q

the ways of traversing a binary tree

A

Three methods of traversing a binary tree: Pre-order, In-order, and Post-order
The outline method can be used to remember these traversal methods
The data contained in a binary tree will output differently depending on the traversal method used.

62
Q

binary tree implementations

A

Dictionaries: where each node is represented as a key, and the corresponding value is a list containing its left and right child nodes.

Arrays: where each node is represented as an element in an array, and each element contains the node’s data, left and right child indices. If a child node is missing, the pointer is set to null.

Objects: where each node is represented as an object with a data attribute and left and right pointer attributes. If a child node is missing, the pointer is set to null.

63
Q

binary tree applications

A

Database applications: where efficient searching and sorting is required without moving items. Binary trees can be used to implement indexes, query optimization, and other database operations.

Wireless networking and router tables: binary trees can be used to store and organize routing information in networks.

Operating system scheduling: binary trees can be used to implement scheduling algorithms to manage processes and tasks in an operating system.

Huffman coding: binary trees can be used to implement compression algorithms like JPEG and MP3 by creating optimized binary code tables.

Cryptography: binary trees can be used in cryptography, such as GMM trees, for secure key management and authentication.

64
Q

Depth First Search

A

A graph traversal algorithm that uses a stack data structure to visit nodes in a LIFO (last in, first out) order
and stack data structure and LIFO method to visit nodes.
- takes one route as far as possible
- maze-solving, topological sorting, and path-finding algorithms.
- Depth first goes through the furthest path of the leftmost node and if it can’t find X, it backtracks and find another path. When dead end, it pops off node and goes to last decision point to make a different decision.

65
Q

Depth First Search - Traversing a graph

A

A technique for traversing a graph using Depth First Search (DFS) that involves setting the root node as the current node, adding it to the list of visited nodes, visiting connected nodes, and outputting all the visited nodes.
To traverse a graph using DFS, start at the root node, add it to the visited list, and then visit all connected nodes. Continue this process until all nodes have been visited and output all visited nodes.

66
Q

Depth First Search - Iterative Approach

A

An iterative approach for traversing a graph using Depth First Search (DFS) that involves setting the root node as the current node, adding it to the list of visited nodes, visiting connected nodes, and outputting all the visited nodes.
The iterative approach for DFS involves using a stack to visit all connected nodes and outputting all visited nodes. This approach is useful when the graph is too large to be handled recursively.

67
Q

Depth First Search - Recursive Approach

A

A recursive approach for traversing a graph using Depth First Search (DFS) that involves setting a root vertex as the current vertex, outputting the vertex, adding it to the list of visited vertices, and following unvisited edges until all vertices have been visited.
The recursive approach for DFS involves calling the DFS function recursively for each unvisited edge until all vertices have been visited. This approach is useful for small graphs and can be implemented with less code.

68
Q

In-Order Traversal

A

A traversal algorithm that visits the left subtree, then the root node, and finally the right subtree in a binary tree structure.

69
Q

Advantages of In-Order Traversal

A

In-Order Traversal automatically sorts the contents of a binary tree without moving data, and can output the nodes in sequential order. This eliminates the need for an insertion sort.
In-Order Traversal is useful for traversing the nodes in a binary tree structure in sequential order without the need for sorting algorithms, and can be used in various applications such as database searching, data compression, and cryptography.

70
Q

Left-Node-Right Algorithm

A

A recursive algorithm used in In-Order Traversal, starting from the root node, it follows the left pointer and repeats the process recursively until there is no pointer to follow. It then outputs the node, follows the right pointer and repeats the process recursively until there is no pointer to follow.

71
Q

Reversing In-Order Traversal

A

To output the nodes in reverse order, one simply reverses the Left-Node-Right algorithm by following the right pointers before outputting the node, and then following the left pointers.

72
Q

Preorder traversal:

A

Preorder traversal starts at top and goes down left
Traverse root, far down left, one node right, repeat
Function preOrder(tree) writes root value and traverses left and right nodes
Traversal order: root node, left subtree, right subtree
Nodes traversed in order passed on left, starting at left of root
Preorder used in programming languages with operation before values (e.g. + a b)
Traversing binary tree using pre order creates copy or returns Polish notation expression
Variation of depth first search algorithm
Follows node-left-right order: start at root, output node, follow left recursively until no pointer, follow right recursively until no pointer.

73
Q

Post-order Traversal

A

Follows the order of left subtree, right subtree, and then root node
Nodes are traversed in the order in which you pass them on the right
Uses a depth first search algorithm and a stack to go as far down the tree as possible before backtracking
Used for deleting a binary tree and outputting post fix expressions
Follows the left-right-node algorithm: start at the root node, follow the left pointer recursively, follow the right pointer recursively, and then output the node.

74
Q

Breadth First Traversal

A

visits all the nodes on one level before moving on to another (also known as level order).
It starts from the left and visits all children of the start node before moving on to the nodes directly connected to each of those nodes in turn.
Breadth First Traversal uses a queue data structure and the first in first out method.
It is a node-based method of finding the shortest path through a graph.

Breadth first visits all nodes connected directly to start node and adds all the child nodes of those nodes to the queue/visits the child nodes of those nodes.

75
Q

Breadth First Traversal vs Depth First Traversal

A

Breadth First Traversal:

Uses the queue data structure
Finds the shortest path through an unweighted graph
Considers all neighbors first
Suitable for searching nodes closer to the source node
Not suitable for decision-making trees used in games or puzzles
Depth First Traversal:

Uses the stack data structure
May traverse through more edges to reach a destination node from a source
Suitable when there are solutions further away from a source
More suitable for gene or puzzle problems
Makes a decision then explores all paths through that decision
Stops if the decision leads to a win situation

76
Q

Breadth first traversal steps

A

Steps to traverse a graph using Breadth First Search are: set the root vertex as the current vertex, add the current vertex to the list of visited vertices, for every edge connected to the vertex enqueue the linked vertex if it is not on the visited list and add it to the visited list, dequeue and set the vertex removed as the current vertex, repeat until the queue is empty, and output all the visited vertices.

77
Q

stack uses

A

-
reversing the order of a set of data
[1]
-
temporarily storing the contents of registers while handling an interrupt
[1]
-
storing return addresses (during subroutine calls)
[1]
-
traversing a graph data structure
[1]
Accept tree / binary tree / maze.
-
passing parameters
[1]
-
a depth first search
[1]
-
to create postfix from infix expression
[1]
To create reverse Polish notation using the shunting yard algorithm

78
Q

Directed graph

A

Graph in which Order of vertices and pairs in the edge set matters