comp 228 data structures

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

1/54

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.

55 Terms

1
New cards

· Big Omega

o Ω(f(n)) means f(n) is a lower bound on the time to execute an algorithm

(best)

2
New cards

· Big O

o O(f(n))means f(n) is an upper bound(worst)

3
New cards

Big Theta

o Θ(f(n)) means f(n) is the time to execute an algorithm -but also used for average

(average)

4
New cards

· O(1 ); bounded time

o The amount of work is not dependent on the size of the problem

5
New cards

· O(log2N); logarithmic time;

o Algorithms that successively cu the amount of data to be processed in half at each step typically fall into this category

6
New cards

· •O(N); linear time

o Adding together the elements of an array is O(N)

7
New cards

· •O(Nlog2N); NlogN time

o Good sorting algorithms, such as quicksort, heapsort, and merge sort have NlogN complexity

8
New cards

· •O(N2); quadratic time

o Some simple sorting algorithms are O(N2)algorithms

9
New cards

· •O(2N); exponential time

o These algorithms are extremely costly!

10
New cards

Recursion

Recursion is a technique that leads to elegant solutions to problems that are difficult to program using simple loops

Recursion Terms

11
New cards

Recursive method

A method that calls itself directly or indirectly

12
New cards

Direct recursion

Recursion in which a method directly calls itself

13
New cards

Indirect recursion

Recursion in which a chain of two or more calls returns to the method that originated the chain

14
New cards

Selection Sort

Find the smallest element in the data set and exchange it with the element in the first position, then find the second smallest element and exchange it with the element in the second position, ... continue in this way until the entire data set is sorted

SELECTION SORT IS O(N^2)

15
New cards

Bubble Sort

Smaller data values "bubble up" to the top of the array

There are N - 1 comparisons the first time, N- 2 comparisons the second time, and so on

Bubble Sort is O(N2)

16
New cards

Insertion Sort

Each successive element in the array is inserted into its proper place with respect to the already sorted elements

O(N^2), but when it is it's sorted it's N comparisons and 0 swaps

17
New cards

Merge Sort

If we can cut the array into two pieces, sort each segment, and then merge the two back together, we should end up sorting the entire array with a lot less work

Total cost of the merge operation is: O(Nlog2N)

Total cost of the sort is O(Nlog2N) + O(N)

18
New cards

Quick Sort

A divide-and-conquer algorithm

Inherently recursive

At each stage, the part of the array being sorted is divided into two "piles", with everything in the left pile less than everything in the right pile

19
New cards

Heap Sort

take the root (maximum) element off the heap, and put it into its place

reheap the remaining elements (next-largest element ends up in the root position)

repeat steps 1 and 2 until there are no more elements

20
New cards

BINARY SEARCH TREE

A binary tree in which the key value in any node is greater than or equal to the key value in its left child and any of its descendants - the nodes in the left subtree - and less than the key value in its right child and any of its descendants - the nodes in the right subtreE

21
New cards

Preorder traversal

Visit the root, visit the left subtree, visit the right subtree

(Triforce)

22
New cards

Inorder traversal

Visit the left subtree, visit the root, visit the right subtree

(Water)

23
New cards

Postorder traversal

Visit the left subtree, visit the right subtree, visit the root

(elegy of darkness)

24
New cards

TRAVERSAL IMPLEMENTATIONS

The application program passes the reset and getNext methods a parameter indicating which of the three traversal orders to use:

25
New cards

· The reset method

o public int reset(TraversalType orderType) {

// returns current number of nodes in the tree

switch (orderType) {

case INORDER :

inOrderQ = new ArrayQueue();

inOrder(root);

Break;

case PREORDER :

preOrderQ = new ArrayQueue();

preOrder(root);

Break;

case POSTORDER :

postOrderQ = new ArrayQueue();

postOrder(root);

break;

}

return size()

}

26
New cards

The inOrder Method

private void inOrder(BSTNode tree) {

if (tree != null) {

inOrder(tree.getLeft());

inOrderQ.enqueue(tree.getInfo());

inOrder(tree.getRight());

}

}

27
New cards

The getNext method

public T getNext (TraversalType orderType) {

/*

* preconditions

* - the BST is not empty

* - the BST has been reset for orderType

* - the BST has not been modified since the most recent reset

* - app code is responsible for not iterating beyond the end

* of the tree

*

* Returns the element at the current position on the BST for

* the specified traversal type and advances the value of the

* current position

*

*/

switch (orderType) {

case INORDER : return inOrderQ.dequeue();

case PREORDER : return preOrderQ.dequeue();

case POSTORDER: return postOrderQ.dequeue();

default: return null;

}

}

}

28
New cards

The SIZE Operation

The public facing Size method calls the private recursive method recSize , passing in the root of the entire tree

public int size() {

return recSize(root);

}

private int recSize(BSTNode tree) {

if (tree == null)

return 0;

Else

return recSize(tree.getLeft()) + recSize(tree.getRight()) + 1;

}

29
New cards

THE ADD OPERATION

Add invokes the recAdd recursive method, passing it the element to be added plus reference to the root of the tree

public void add (T element) {

root = recAdd(element, root);

}

30
New cards

The recAdd MEthod

if (element.compareTo(tree.getInfo()) <= 0) {

tree.setLeft(recAdd(element, tree.getLeft()));

}

else {

tree.setRight(recAdd(element, tree.getRight()));

}

}

return tree;

}

31
New cards

The Remove Operation

recRemove is invoked from the public facing remove method with arguments equal to the element to be removed and the subtree from which to remove it

32
New cards

How to remove a Leaf node

set the appropriate link of its parent to null

33
New cards

How to remove a node (with one child)

make the reference from the parent skip over the node and point instead to the removed node's child

34
New cards

How to remove a node with two children

replace the node's info with the info from another node in the tree so that the "left is less; right is more" property is retained - then remove this other node

35
New cards

The getPRedecessor METHOD

Given a tree's left subtree, we just keep moving right until the right child is null; then return the info reference of that node:

private T getPredecessor(BSTNode tree) {

while (tree.getRight() != null) {

tree = tree.getRight();

}

return tree.getInfo();

}

36
New cards

BALANCING A BINARY SEARCH TREE

Save the tree information in an array in order

Insert the information from the array back into the tree ... in the right order

37
New cards

TO ENSURE A BALANCED TREE

Even out as much as possible the number of descendants in each node's left and right subtrees: insert the middle item of the array insert the left half of the array insert the right half of the array

38
New cards

PRIORITY QUEUE

A priority queue is an ADT with the following accessing protocol: only the highest-priority element can be accessed

39
New cards

Heap

A binary tree implementation of a priority queue

40
New cards

Heap Enqueue

Add new item as the left-most leaf on the bottom level of the tree

41
New cards

Heap Dequeue

Root item is removed - item in rightmost leaf node on the bottom level is moved into the root

42
New cards

· Graph

o A data structure that consists of a set of nodes and a set of edges that relate the nodes to each other

43
New cards

· Vertex

o A node in a grape

44
New cards

· Edge

o A pair of vertices representing a connection between two nodes in a graph

45
New cards

· Undirected graph

o A graph in which the edges have no direction

46
New cards

· Directed graph (digraph)

o A graph in which each edge is directed from one vertex to another a graph G is defined as follows:

47
New cards

· Adjacent vertices

o Two vertices in a graph that are connected by an edge

48
New cards

· Path

o Two vertices in a graph that are connected by an edge

49
New cards

· Complete graph

o A graph in which every vertex is directly connected to every other vertex

50
New cards

· Weighted graph

o A graph in which each edge carries a value

51
New cards

· DEPTH-FIRST SEARCH

o Go down a path to its deepest point, backtracking when necessary. Use stacks

52
New cards

· BREADTH-FIRST SEARCH

o Visit all neighbors first, then systematically visit all of the neighbors' neighbors, and so on .. Use queues

53
New cards

GRAPH IMPLEMENTATIONS (Adjacency matrix)

§ N × N table that shows the existence, and weights, of all N edges in the graph

54
New cards

GRAPH IMPLEMENTATIONS (Adjacency list)

§ A linked list that identifies all the vertices to which a particular vertex is connected

55
New cards

· Dijkstra's Algorithm

an algorithm for finding the shortest paths between nodes in a graph,