COP3530 Final Review

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

What is the height of a tree?

# of nodes in longest path from root to leaf if (tree has only root) height = 1 else height = 1 + max(height(children))

binary heaps

- Complete binary trees - Each node < children in MIN heap (where we prioritize smallest element) - Each node > children in MAX heap (where we prioritize largest element) - Only root can be removed via ExtractMin(), ExtractMax()

how to make a max heap

1. Iterate through the tree with an In Order traversal and store the values 2. Pass through the tree with a Post Order traversal 3. For each node you pass in the Post Order, replace it with the stored node of the In Order traversal at the same index

how to make a min heap

1. Iterate through the tree with an In Order traversal and store the values 2. Pass through the tree with a Pre Order traversal 3. For each node you pass in the Pre Order, replace it with the stored node of the In Order traversal at the same index

What are the different tree types?

1. N-ary Tree 2. Full Binary Tree 3. Complete Binary Tree 4. Perfect Binary Tree 5. Binary Search Tree

Two characteristics for all heaps

1. it is a complete binary tree, so heaps are nearly always implemented using the array representation for complete binary trees 2. values in a heap are partially ordered, meaning there is a relationship between the value stored at any node and the values of its children

How can we design a priority queue data structure to be really fast?

1. use an unsorted array 2. use a sorted array 3. use a sorted list 4. use a binary heap!

With an array representation of a binary heap, how do you get the right child position of a node at position p?

2p + 2

With an array representation of a binary heap, how do you get the left child position of a node at position p?

2p+1

What is the time complexity of deleting an element from a min heap?

O(logn)

What is the time complexity of inserting an element into a heap?

O(logn)

T or F: A full tree is not necessarily complete

True

True or false: A perfect tree is ALWAYS complete and full

True

What is heap sort?

Turn array into heap

How do we go through lists?

Using iterators for( list<int>::iterator it = myList.begin(); it != myList.end(); it++) {cout << *it << endl;} // accesses element iterator is pointing to

Do binary heaps need to be complete trees?

Yes

Is there a necessary relationship between the value of a node and that of its sibling in either the min heap or the max heap?

Yes

What is a data structure?

a specialized format for organizing and storing data ex. Lists, stacks, and queues

n-ary tree

a tree that limits each node to no more than n children i.e. (Binary trees can have AT MOST 2 children)

Min heap

a tree that maintains the simple property that a node's key is less than or equal to the node's children's keys. Because the root has a value less than or equal to its children, which in turn have values less than or equal to their children, the root stores the minimum of all values in the tree.

a SINGLE stack can be used to handle which of the following? A. undo feature B. Balancing parenthesis C. Task scheduling D. Evaluating expressions

a, b, d

PushBack(data)

adds a new node to the end of the linked list

What does it mean for a tree to be complete?

all levels are completely filled except possibly the last level. The last level has all nodes as far left as possible

Binary Search Tree

all values in any node's left subtree are less than node, all values in any node's right subtree are greater than node.

What is heap sort and its time complexity?

an algorithm that sorts an array O(N * log(N)) 1. Insert n items (from array) into a heap == building a heap in place, and then inserting into it 2. Remove n items from heap and place in array building a heap in place = O(N)

WHICH OF THE FOLLOWING STATEMENTS ABOUT LINKED LISTS AND ARRAYS ARE TRUE? A) both data structures store elements sequentially (contiguously) in memory B) both data structures use iterators C) Both are linear data types D) Linked lists are more efficient if you need random access

b, c

Perfect and Complete trees with n nodes have height

ceil(log2/logn)

what is "heapify up" and when is it used?

done after inserting to correct the heap ■ Corrects the path between the inserted node and root

Performance of array-based queue

enqueue(element): O(1) - dequeue(): O(1) - size(): O(1) - isEmpty(): O(1)

time complexity for inserting element and finding the element with the highest priority and removing it for sorted array

extractMin-O(1) //element with highest priority is at position 0. Simply remove it insert - O(n) // find position where element should go, //insert it

time complexity for inserting element and finding the element with the highest priority and remove it for unsorted array

extractMin-O(n); //find the element with the highest priority and remove it insert - O(1); //just add it to the end

With an array representation of a binary heap, how do you get the position of the parent of a node at position p?

floor((p-1)/2)

How can you turn an array into a heap?

for (i = size/2; i>0;i--) heapifyDown(i)

why are insert and pop operations both log(n)?

heapify()

How is the depth/level of a tree measured?

if (node n is the root) level(n) = 0 else level(n) = level(parent) + 1

How do we solve the "rightward shift" problem?

implement a queue using a "circular array"

time complexity for inserting element and finding the element with the highest priority and removing it for a sorted list

insert - O(n) //find position where element should go and insert it extractMin-O(1) //element with highest priority is at position 0. Simply remove it

what is "heapify down" and when is it used?

is done after deleting to correct the heap ■ Moves last element to root ■ Corrects a path from the new root to a leaf

formulas to find child for a 1-indexed array

left child: 2i right child: 2i + 1 parent: i/2* (floor the result)

formulas to find child for a 0-indexed array

left child: 2i + 1 right child: 2i + 2 parent: (i-1)/2* (floor the result)

inorder traversal

left, root, right *prints nodes in SORTED order

With an array representation, in what order are binary heaps stored?

level order

height for a binary heap

log(n)

Does a heap implement partial or total order?

partial. BST implements total

TopBack()

peeks at the node at the end of the linked list (if no tail, we need to traverse the whole list)

Characteristics of a stack implementation

push(element) - pop() - top() - size() - isEmpty()

Performance of array-based stack and list-based stack

push(element): O(1) - pop(): O(1) - top(): O(1) - size(): O(1) - isEmpty(): O(1)

Operations of a list ADT

read/write element, add/remove element, find element, count, traverse list

Pre-Order Traversal

root, left, right *prints the root first

Characteristics of a singly linked list (no tail)

stored non-contiguously in memory, does not allow random access

Compiler error

syntax error

PopBack()

takes off the node at the end of the linked list

inorder predecessor

the left subtree's rightmost child

inorder successor

the right subtree's leftmost child

what is heapify used for?

to turn any tree/array into a heap

Subtree

tree whose root is a child of that node

true or false: only the root can be removed in a Pop operation

true

What is the worst case for tree traversal?

O(n) (where n is the # of nodes that there are)

Worst case for BST insertion

O(n). The "splindly" tree requires O(n) for find, insert after, and delete since it has to iterate through all of the elements The "bushy" tree is a complete tree w/ height of ceil(log2/logn). Finding, deleting, and inserting after is O(log n)

Drawbacks of a singly linked list (no tail)

TopBack, PushBack, PopBack and AddBefore are expensive

What is the time complexity of heap sort?

O(nlogn)

For doubly linked list with tail, these operations become O(1)

PopBack() and AddBefore(Node, data)

For singly linked list with a tail, these operations become O(1)

PushBack() and TopBack()

Performance of a singly linked list (w/ tail)

PushFront: O(1) PopFront: O(1), TopFront: O(1) Find: O(n) *PushBack: O(1) -PopBack: O(n) *TopBack: O(1) Erase: O(n) Empty: O(1) -AddBefore(Node, Key): O(n) AddAfter(Node, Key): O(1)

Performance of a singly linked list (no tail)

PushFront: O(1) PopFront: O(1), TopFront: O(1) Find: O(n) PushBack: O(n) PopBack: O(n) TopBack: O(n) Erase: O(n) Empty: O(1) AddBefore(Node, Key): O(n) AddAfter(Node, Key): O(1)

What is the algorithm for deleting a node in a heap?

Remove the root node, take the last element of the heap and make it the root node. While the new root has children and is greater than both it's children, swap with the smaller child.

Characteristics of singly linked list (w/ tail)

Stores similar elements, elements are linked in memory but stored non-contiguously, doesn't allow for random access

Benefits of an array-based list

Constant access time, arr[i] = arr + (i*sizeOf(type))

What is the algorithm for making a max heap from a BST?

Create array of elements with inorder traversal of BST. Pass through the BST with post order traversal, replacing every element with the node from the inorder traversal at the same index.

What is the algorithm for making a min heap from a BST?

Create array of elements with inorder traversal of BST. Pass through the BST with pre order traversal, replacing every element with the node from the inorder traversal at the same index.

2 types of BST traversals

Depth-First Search (DFS) and Breadth-First Search (BFS)

T or F: A complete tree is always full

FALSE

true or false: doubly linked lists allow random access in the container in constant time

FALSE

true or false: linked lists are more efficient than arrays if you have to access elements with O(1) time.

FALSE

T or False: A complete and full tree is always perfect

False

True or false: Doubly linked lists do not require more memory

False

Characteristics of a list implementation for an array

Finite size, stores similar elements, contiguous, allows for random access

Which of the following container(s) are list ADT implementations in C++?

Forward list, array, vector

For singly linked list, these operations are O(1)

Front(), isEmpty(), and AddAfter

Perfect Binary Tree

Full binary tree of height H with exactly (2^h) -1 nodes. The height of a tree w/ n nodes is ceil(logn/log2). a type of binary tree in which every internal node has exactly two child nodes and all the leaf nodes are at the same level

For a max-heap, is each node greater than or less than its children?

Greater

Complete Binary Tree

In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible.

time complexity for inserting element and finding the element with the highest priority and removing it for a binary heap

Insert element - O(log(n)) ExtractMin() - O(log(n))

What are the steps for heap sort no building in place?

Insert n items into heap, remove n items from heap and place into array

What is the algorithm for inserting a node into a min heap?

Insert new item at the bottom of the heap. While new item is not the root and new item is smaller than its parent, swap new item with parent.

For a min-heap, is each node less than or greater than its children?

Less

Is stack[i] allowed?

NO. ABSOLUTELY NOT. There is NO RANDOM ACCESS (we access through top index)

Can you remove a non root node in a binary heap?

No

Is every binary tree a BST

No. All BST's are Binary Tree however only some Binary Tree's may be also BST.

What are leaves?

Node with no children (external nodes)

What are trees?

Non-linear ordered data structure that is rooted, directed, and has no cycles

Max heap

A binary tree that maintains the simple property that a node's key is greater than or equal to the node's children's keys. Because the root has a value greater than or equal to its children, which in turn have values greater than or equal to their children, the root stores the maximum of all values in the tree.

What's a priority queue?

A queue in which all elements inserted have some priority - Elements with highest priority are removed first

What is an ADT?

Abstract Data Type, a class of objects whose logical behavior is defined by data and operations

Performance for an array-based list

Adding an element to the beginning: O(N) Removing an element from the beginning: O(N) Removing an element from the end: O(1) Adding an element to the end: O(1) Adding in the middle: O(N) Removing in the middle: O(N)

Benefits of a singly linked list (no tail)

Adding/Removing in front is easier, O(1)

Full Binary Tree

All nodes have either 0 children, OR 2 children (the leaf nodes)

Runtime error

An error that does not occur until the program has started to execute but that prevents the program from continuing.

What is big o of deleting an element from a doubly linked list w/ tail in the worst case in terms of big o notation?

O(n)

What is the time complexity for a building a heap inplace?

O(n)


संबंधित स्टडी सेट्स

Knewton Alta - Chapter 1 Sampling and Data

View Set

ACC 1820 - Managerial Accounting - Chapter 5 Book questions

View Set

Chapter 6: Variable Costing and Segment

View Set

Cardiovascular Midterm Study Set

View Set

Strength and Conditioning Chapter 6: Bioenergetics

View Set

READING 44. PORTFOLIO RISK AND RETURN: PART II

View Set